Graphqlbundle: File Upload

Created on 31 Mar 2017  路  5Comments  路  Source: overblog/GraphQLBundle

| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | yes
| BC Break report? | no
| RFC? | no
| Version/Branch | 0.8.0

Is there a way to upload files through graphql?
Does the bundle manage multipart request?

Here is a related proposal for Apollo: https://github.com/apollographql/apollo/issues/65.

documentation needed good first issue

Most helpful comment

Here an example using relay mutation:

  • Schema
RootMutation:
    type: object
    config:
        fields:
            uploadFile:
                builder: "Relay::Mutation"
                builderConfig:
                    inputType: UploadFileInput
                    payloadType: UploadFilePayload
                    mutateAndGetPayload: "@=mutation('upload_file', [request, value['title']])"

UploadFilePayload:
    type: relay-mutation-payload
    config:
        fields:
            files: {type: "[String!]!" }

UploadFileInput:
    type: relay-mutation-input
    config:
        fields:
            title: {type: "String!"}
  • Mutation
    public function uploadFile(\Symfony\Component\HttpFoundation\Request $request, $title)
    {
        /** @var \Symfony\Component\HttpFoundation\FileBag; $requestFiles */
        $requestFiles = $request->files;
        if (!$requestFiles->has('myFile')) {
            throw new \Overblog\GraphQLBundle\Error\UserError(sprintf('File "%s" is required.', 'myFile')));
        }
        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $requestFiles->get('myFile');
        // here do some work on your uploaded file
        return [$title];
    }

@ooflorent done! :trollface:

All 5 comments

Hi, yes it is possible, you must use request to get your file in your mutation resolver, you can inject it using the object request in the expression language...

@mcg-web Could you provide an example? :trollface:

Here an example using relay mutation:

  • Schema
RootMutation:
    type: object
    config:
        fields:
            uploadFile:
                builder: "Relay::Mutation"
                builderConfig:
                    inputType: UploadFileInput
                    payloadType: UploadFilePayload
                    mutateAndGetPayload: "@=mutation('upload_file', [request, value['title']])"

UploadFilePayload:
    type: relay-mutation-payload
    config:
        fields:
            files: {type: "[String!]!" }

UploadFileInput:
    type: relay-mutation-input
    config:
        fields:
            title: {type: "String!"}
  • Mutation
    public function uploadFile(\Symfony\Component\HttpFoundation\Request $request, $title)
    {
        /** @var \Symfony\Component\HttpFoundation\FileBag; $requestFiles */
        $requestFiles = $request->files;
        if (!$requestFiles->has('myFile')) {
            throw new \Overblog\GraphQLBundle\Error\UserError(sprintf('File "%s" is required.', 'myFile')));
        }
        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $requestFiles->get('myFile');
        // here do some work on your uploaded file
        return [$title];
    }

@ooflorent done! :trollface:

done here

The link was not available anymore, here is the updated link of the documentation: https://github.com/overblog/GraphQLBundle/blob/master/docs/definitions/upload-files.md

(in particular, see this link for usage with apollo-upload-client)

Was this page helpful?
0 / 5 - 0 ratings