phps-cans/psr7-middleware-graphql

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

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

This package is not auto-updated.

Last update: 2024-12-16 07:43:08 UTC


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