| 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.
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:
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!"}
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)
Most helpful comment
Here an example using relay mutation:
@ooflorent done! :trollface: