This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A middleware to support file uploads in GraphQL

3.0.0 2018-06-13 07:54 UTC

This package is not auto-updated.

Last update: 2021-10-09 02:32:26 UTC


Build Status Code Quality Code Coverage Total Downloads Latest Stable Version License Join the chat at

A 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 Zend Expressive, it would typically be in config/routes.php something like:

use Application\Action\GraphQLAction;
use Zend\Expressive\Helper\BodyParams\BodyParamsMiddleware;
use GraphQL\Upload\UploadMiddleware;

$app->post('/graphql', [
    UploadMiddleware::class, // This is the magic
], 'graphql');

Direct usage

Or if you don't use middleware, it can be called directly like so:


use GraphQL\Server\StandardServer;
use GraphQL\Upload\UploadMiddleware;
use Zend\Diactoros\ServerRequestFactory;

// Create request (or get it from a framework)
$request = ServerRequestFactory::fromGlobals();
$request = $request->withParsedBody(json_decode($request->getBody()->getContents(), true));

// Process uploaded files
$uploadMiddleware = new UploadMiddleware();
$request = $uploadMiddleware->processRequest($request);

// Execute request and emits response
$server = new StandardServer(/* your config here */);
$result = $server->executePsrRequest($request);

Usage in schema

Then you can start using in your mutations like so:


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',
    '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

                    return 'Uploaded file was ' . $file->getClientFilename() . ' (' . $file->getClientMediaType() . ') with description: ' . $args['text'];


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