phps-cans/psr7-middleware-graphql

本包包含一个处理graphql请求的http-interop中间件实现

v0.2.0 2018-12-06 19:00 UTC

This package is not auto-updated.

Last update: 2024-09-23 06:39:49 UTC


README

本包目前处于开发阶段。本包使用graphql官方包的集成包来处理Graphql请求。

如果请求的content-typeapplication/graphql或者配置的URL被访问(默认为 /graphql),则执行此中间件。此中间件使用StandardServer来处理请求。您可以根据需要创建模式和服务器。此中间件期望JSON已经解码(例如,使用Psr7Middlewares\Middleware\Payload)。

使用简单

为了方便使用此包,我们建议您使用

这样,zend-stratigility 服务器就可以直接使用,JSON 主体将自动解析,中间件将被管道化,StandardServer 和 Schema 将自动创建。

使用 ServiceProvider

我们建议使用 stratigility-harmony来自动配置您的 stratigility 服务器。

此包默认提供ServiceProvider(src/ServiceProvider/DefaultServiceProvider.php)。它期望在容器中注册名为 GraphQL\Server\StandardServerStandardServer。如果您使用 Middleware List,它将使用常量 MiddlewareOrder::ROUTER_EARLY 更新队列。

使用任何 http-interop 兼容的中间件管道

此示例基于 zend-stratigility 中间件管道

use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\Server;
use PsCs\Psr7\Middleware\Graphql\WebonyxGraphqlMiddleware;
use GraphQL\Server\StandardServer;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\Type;
use Psr7Middlewares\Middleware\Payload;
use Zend\Stratigility\Middleware\NotFoundHandler;
use Zend\Diactoros\Response;
use Zend\Stratigility\NoopFinalHandler;

// Create fields
$field = FieldDefinition::create([
            "name" => "billPerYear",
            "type" => Type::string(),
            'args'    => [
                'id' => Type::nonNull(Type::id())
            ],
            "resolve" => function($rootValue, $args) {
                return "success on ".$args["id"];
            }

        ]);
//create the schema
$schema = new Schema([
            "query" => new ObjectType([
                'name'   => 'Query',
                'fields' => [
                    $field
                ]
            ])
        ]);
$defaultUri = '/graphql'; 
$debug = false;
// create the standardServer of webonyx
$standardServer = new StandardServer(["schema" => $schema]);
// let instantiate our php server
$pipe = new MiddlewarePipe();
// Register the middleware which decode JSON body
$pipe->pipe(new \Psr7Middlewares\Middleware\Payload());
/* Instantiate and register our middleware
Params are:
- $standardServer : webonyx's graphql server: [`StandardServer`](http://webonyx.github.io/graphql-php/executing-queries/#using-server) 
- $defaultUri = This middleware will be executed for each request matching the default URI and for each request having the content-type set to "application/graphql"
- $debug = IF false, minimal error will be reported (as specified in [handling error](http://webonyx.github.io/graphql-php/error-handling/). The value of $debug must be the same as specified in [`$debug`](http://webonyx.github.io/graphql-php/error-handling/#debugging-tools)

**/
$pipe->pipe(new WebonyxGraphqlMiddleware($standardServer, $defaultUri, $debug)); 
// Add the notFoundHandler
$pipe->pipe(new NotFoundHandler(new Response()));
// Instantiate our server
$server = Server::createServer($pipe, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
// let tell to the server that we are ready
$server->listen(new NoopFinalHandler());

请随时报告任何问题。

待办事项

  • 编写单元测试
  • 允许格式错误