定制 phps-cans/psr7-middleware-graphql 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phps-cans/psr7-middleware-graphql

Composer 安装命令:

composer require phps-cans/psr7-middleware-graphql

包简介

This package contains a http-interop middleware implementation to handle graphql request

README 文档

README

This package is currently under development. This package use the official package of graphql's integration to handle Graphql Request.

This middleware is executed if the content-type of the request is application/graphql or if the configured URL is reached (by default it is /graphql) This middleware use the StandardServer to handle request. It is free to you to create the schema and the server. This middleware expect that JSON has already been decoded (by example with Psr7Middlewares\Middleware\Payload)

Easy use:

To be able to use this package easily we recommend you to use:

This way, the zend-stratigility server is ready to use, JSON body are automatically parsed, middleware are piped, the StandardServer and Schema are automatically created.

Using ServiceProvider

We recommend to use stratigility-harmony to automatically configure your stratigility's server.

This package provide a ServiceProvider by default (src/ServiceProvider/DefaultServiceProvider.php). It expect the StandardServer to be registered in the container under the name GraphQL\Server\StandardServer. If you use Middleware List, it update the queue using the constant MiddlewareOrder::ROUTER_EARLY.

using any http-interop compilant Middleware pipe

This example is based on zend-stratigility middleware pipe:

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());

Feel free to report any issues.

TODO

  • Write unit testing
  • Allow formmating errors

统计信息

  • 总下载量: 28.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 5
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固