Skip to content

Repository files navigation

GraphQL Upload

Build StatusTotal DownloadsLatest Stable VersionLicense

A PSR-15 middleware to support file uploads in GraphQL. It implements the multipart request specification for webonyx/graphql-php.

Quick start

Install the library via composer:

composer require ecodev/graphql-upload

Configure as middleware

In Laminas Mezzio, it would typically be in config/routes.php something like:

useApplication\Action\GraphQLAction;
useMezzio\Helper\BodyParams\BodyParamsMiddleware;
useGraphQL\Upload\UploadMiddleware;
$app->post('/graphql', [
BodyParamsMiddleware::class, UploadMiddleware::class, // This is the magic
GraphQLAction::class,
], 'graphql');

Other frameworks

This lib is an implementation of PSR-15, so it can be used with any framework supporting PSR-15. For specific configuration instructions, refer to your framework documentation.

If your framework does not support PSR-15 middleware, you will probably need some kind of bridge. Again, refer to your framework for specific instructions.

Usage in schema

Then you can start using in your mutations like so:

<?phpuseGraphQL\Type\Definition\ObjectType;
useGraphQL\Type\Definition\Type;
useGraphQL\Type\Schema;
useGraphQL\Upload\UploadType;
usePsr\Http\Message\UploadedFileInterface;
// Build your Schema$schema = newSchema([
'query' => newObjectType([
'name' => 'Query',
'fields' => [],
]),
'mutation' => newObjectType([
'name' => 'Mutation',
'fields' => [
'testUpload' => [
'type' => Type::string(),
'args' => [
'text' => Type::string(),
'file' => newUploadType(),
],
'resolve' => function ($root, array$args): string {
/** @var UploadedFileInterface $file */$file = $args['file'];
// Do something with the file$file->moveTo('some/folder/in/my/project');
return'Uploaded file was ' . $file->getClientFilename() . ' (' . $file->getClientMediaType() . ') with description: ' . $args['text'];
},
],
],
]),
]);

Limitations

  • It only works with PSR-7 requests. If you were not using PSR-7 yet, laminas-diactoros is one of many implementation that could be used to create PSR-7 requests.

About

A PSR-15 middleware to support file uploads in GraphQL

Topics

Resources

Stars

90 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages