youshido/graphql-files-bundle

实现图像和文件上传到GraphQL API的库

安装次数: 4,549

依赖者: 0

建议者: 0

安全: 0

星标: 4

关注者: 1

分支: 0

类型:symfony-bundle

v0.0.1 2017-08-07 19:26 UTC

This package is not auto-updated.

Last update: 2024-09-10 20:50:27 UTC


README

Symfony扩展包,用于轻松实现图像和文件到您的GraphQL API(包含GraphQL实现及其文档的扩展包在此处:这里)。扩展包提供了UploadImageMutation

mutation {
  uploadImage(field: "file") {
    id
    url
    fileName
    mimeType
    extension
    size
    resized(width: 100, height: 100, mode: INSET) {
      url
    }
  }
}

该变更假设请求的内容类型为multipart/form-data,并将图像数据包含在作为参数传递的字段field中。上传文件变更

mutation {
  uploadFile(field: "file") {
    id
    url
    fileName
    mimeType
    extension
    size
  }
}

此外,扩展包还提供了ImageField,可用于您的API,如下所示

{
  me {
    id
    firstName
    lastName
    image { // image field from bundle
      url
      resized(width: 100, height: 100, mode: INSET) {
        url
      }
    }
  }
}

或者您可以直接将参数添加到图像字段,以便更方便地使用。

{
  me {
    id
    firstName
    lastName
    small: image(width: 100, height: 100, mode: INSET) { // resized directly
      url
    }    
    medium: image(width: 500, height: 300, mode: OUTBOUND) { // different mode
      url
    }    
    fullSize: image {
      url
    }
  }
}

如何使用

1. 安装

composer require youshido/graphql-files-bundle

2. 配置

2.1 在您的AppKernel.php中启用扩展包

$bundles[] = new Youshido\GraphQLFilesBundle\GraphQLFilesBundle()

2.2 在routing.yml中添加新的路由

graphql_file.image_resizer:
    resource: "@GraphQLFilesBundle/Resources/config/routing.yml"

2.3 在config.yml中配置扩展包

这是完整的配置,默认情况下不需要

graph_ql_files:
    image_driver: gd     #imagine driver, can be gd, imagick or gmagick
    storage: local       #or s3
    platform: orm        #or odm
    local:                                     #config for local storage
        web_root: "%kernel.root_dir%/../web"
        path_prefix: "uploads"
    s3:                                        #config for s3 storage 
        client: ~                              #s3 client service
        bucket: ~                               
        directory: ''
    models:
        image_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\ImageValidationModel
        file_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\FileValidationModel
        orm:
            image: Youshido\GraphQLFilesBundle\Entity\Image
            file: Youshido\GraphQLFilesBundle\Entity\File
        odm:
            image: Youshido\GraphQLFilesBundle\Document\Image
            file: Youshido\GraphQLFilesBundle\Document\File

3. 设置GraphQL模式

3.1 将UploadImageMutation添加到您的MutationType

<?php

use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadBase64ImageField;
use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadImageField;
use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadFileField;

class MutationType extends AbstractObjectType
{

    public function build($config)
    {

        $config->addFields([
            // images
            new UploadBase64ImageField(),
            new UploadImageField(),
            
            // files
            new UploadFileField(),

            // other mutations
        ]);

    }
}

3.2 将图像字段添加到您的类型

use Youshido\GraphQLFilesBundle\GraphQL\Field\ImageField;

class YourType extends AbstractObjectType
{

    public function build($config)
    {
        $config->addFields([
            // your type fields
        
            new ImageField()
        ]);
    }
}