t3n/graphql-upload

此包的最新版本(2.1.0)没有提供许可证信息。

t3n/graphql 的上传处理扩展

安装次数: 52,694

依赖项: 1

建议者: 0

安全性: 0

星标: 2

关注者: 3

分叉: 3

开放问题: 0

类型:neos-package

2.1.0 2022-04-05 12:17 UTC

This package is auto-updated.

Last update: 2024-09-05 17:32:17 UTC


README

t3n/graphql 的侧车包,它引入了 Upload 标量,并允许您在模式中处理文件上传。

简单安装包

composer require t3n/graphql-upload

配置

此包包含所有必需的部分。但是,您必须像这样将 typeDefs 和 Resolver 添加到您的模式中

t3n:
  GraphQL:
    endpoints:
      'your-endpoint': #use your endpoint variable here
        schemas:
          upload:
            typeDefs: 'resource://t3n.GraphQL.Upload/Private/GraphQL/schema.upload.graphql'
            resolvers:
              Upload: 't3n\GraphQL\Upload\Resolver\Type\UploadResolver'

这是您需要做的所有事情。一旦配置完毕,您就可以在您的应用程序中使用 Upload 标量。

用法

要使用 Upload 标量,您可能想将其添加到类似这样的突变中作为参数

type Mutation {
    uploadFile(file: Upload): String
}

此包将自行处理上传并将 Neos\Http\Factories\FlowUploadedFile 传递给您的 MutationResolver。在您的解析方法中,您可能需要导入资源

class MutationResolver implements ResolverInterface
{
    /**
     * @Flow\Inject
     *
     * @var ResourceManager
     */
    protected $resourceManager;

    public function uploadFile($_, $variables): string
    {
        /** @var FlowUploadedFile $file */
        $file = $variables['file'];

        $resource = $this->resourceManager->importResource($file->getStream()->detach());
        $resource->setFilename($file->getClientFilename());
        $resource->setMediaType($file->getClientMediaType());

        return $file->getClientFilename();
    }
}

一些注意事项

要实际使用文件上传,您的客户端前端必须在表单中使用 multipart/form-data。此包已与使用 https://github.com/jaydenseric/apollo-upload-client 的 react 应用程序进行了测试