ecodev / graphql-upload
A middleware to support file uploads in GraphQL
Installs: 1 857 303
Dependents: 12
Suggesters: 1
Security: 0
Stars: 90
Watchers: 12
Forks: 19
Open Issues: 1
Requires
- php: ^8.2
- ext-json: *
- laminas/laminas-diactoros: ^3.6
- psr/http-server-middleware: ^1.0
- webonyx/graphql-php: ^15.0
Requires (Dev)
- friendsofphp/php-cs-fixer: @stable
- phpstan/phpstan: @stable
- phpunit/phpunit: @stable
- dev-master
- 8.0.0
- 7.0.0
- 6.1.5
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.0
- 5.0.0
- 4.1.0
- 4.0.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/laminas/laminas-diactoros-3.0.0
- dev-actions
- dev-dependabot/add-v2-config-file
- dev-dependabot/composer/friendsofphp/php-cs-fixer-2.18.2
This package is auto-updated.
Last update: 2025-08-28 14:17:12 UTC
README
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:
use Application\Action\GraphQLAction; use Mezzio\Helper\BodyParams\BodyParamsMiddleware; use GraphQL\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:
<?php use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; use GraphQL\Upload\UploadType; use Psr\Http\Message\UploadedFileInterface; // Build your Schema $schema = new Schema([ 'query' => new ObjectType([ 'name' => 'Query', 'fields' => [], ]), 'mutation' => new ObjectType([ 'name' => 'Mutation', 'fields' => [ 'testUpload' => [ 'type' => Type::string(), 'args' => [ 'text' => Type::string(), 'file' => new UploadType(), ], '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.