An open API service indexing awesome lists of open source software.

https://github.com/ecodev/graphql-upload

A PSR-15 middleware to support file uploads in GraphQL
https://github.com/ecodev/graphql-upload

graphql graphql-php hacktoberfest middleware multipart upload

Last synced: 4 months ago
JSON representation

A PSR-15 middleware to support file uploads in GraphQL

Awesome Lists containing this project

README

          

# GraphQL Upload

[![Build Status](https://github.com/ecodev/graphql-upload/workflows/main/badge.svg)](https://github.com/ecodev/graphql-upload/actions)
[![Code Quality](https://scrutinizer-ci.com/g/Ecodev/graphql-upload/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Ecodev/graphql-upload/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/Ecodev/graphql-upload/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Ecodev/graphql-upload/?branch=master)
[![Total Downloads](https://poser.pugx.org/ecodev/graphql-upload/downloads.png)](https://packagist.org/packages/ecodev/graphql-upload)
[![Latest Stable Version](https://poser.pugx.org/ecodev/graphql-upload/v/stable.png)](https://packagist.org/packages/ecodev/graphql-upload)
[![License](https://poser.pugx.org/ecodev/graphql-upload/license.png)](https://packagist.org/packages/ecodev/graphql-upload)
[![Join the chat at https://gitter.im/Ecodev/graphql-upload](https://badges.gitter.im/Ecodev/graphql-upload.svg)](https://gitter.im/Ecodev/graphql-upload)

A [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware to support file uploads in GraphQL. It implements
[the multipart request specification](https://github.com/jaydenseric/graphql-multipart-request-spec)
for [webonyx/graphql-php](https://github.com/webonyx/graphql-php).

## Quick start

Install the library via composer:

```sh
composer require ecodev/graphql-upload
```

### Configure as middleware

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

```php
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
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](https://github.com/laminas/laminas-diactoros) is one of many
implementation that could be used to create PSR-7 requests.