### Installing GraphQL Upload with Composer Source: https://github.com/ecodev/graphql-upload/blob/master/README.md This command installs the `ecodev/graphql-upload` library using Composer, the PHP dependency manager. It adds the package to your project's `composer.json` and downloads its dependencies, making the middleware available for use. ```sh composer require ecodev/graphql-upload ``` -------------------------------- ### Using GraphQL Upload Directly with PSR-7 Request Source: https://github.com/ecodev/graphql-upload/blob/master/README.md This PHP example illustrates how to manually integrate `UploadMiddleware` without a PSR-15 framework. It shows the steps to create a PSR-7 request, process it with `UploadMiddleware` to handle file uploads, and then execute it using `StandardServer` from `webonyx/graphql-php`. ```php 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); $server->getHelper()->sendResponse($result); ``` -------------------------------- ### Configuring GraphQL Upload as PSR-15 Middleware in Laminas Mezzio Source: https://github.com/ecodev/graphql-upload/blob/master/README.md This PHP snippet demonstrates how to integrate `UploadMiddleware` into a Laminas Mezzio application's routing configuration. It shows the middleware chain for a `/graphql` POST endpoint, ensuring that `UploadMiddleware` processes file uploads before the main `GraphQLAction` handles the request. ```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'); ``` -------------------------------- ### Defining GraphQL Upload Type in Schema Mutation Source: https://github.com/ecodev/graphql-upload/blob/master/README.md This PHP snippet demonstrates how to define an `UploadType` in a GraphQL schema's mutation. It shows a `testUpload` mutation that accepts a `text` argument and a `file` argument of type `UploadType`, and how to access and move the `UploadedFileInterface` within the resolver function. ```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']; }, ], ], ]), ]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.