在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Ecodev/graphql-upload开源软件地址(OpenSource Url):https://github.com/Ecodev/graphql-upload开源编程语言(OpenSource Language):PHP 97.5%开源软件介绍(OpenSource Introduction):GraphQL UploadA PSR-15 middleware to support file uploads in GraphQL. It implements the multipart request specification for webonyx/graphql-php. Quick startInstall the library via composer: composer require ecodev/graphql-upload Configure as middlewareIn Laminas Mezzio, it would typically be in 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 frameworksThis 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. Or else, you could use the direct usage below for manual integration. Direct usageIf you don't use middleware, it can be called directly like so: <?php
use GraphQL\Server\StandardServer;
use GraphQL\Upload\UploadMiddleware;
use Laminas\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);
$server->getHelper()->sendResponse($result); Usage in schemaThen 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',
]),
'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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论