|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +// Run local test server |
| 4 | +// php graphql.php |
| 5 | + |
| 6 | +// Try query |
| 7 | +// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8010 |
| 8 | + |
| 9 | +// Try mutation |
| 10 | +// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8010 |
| 11 | + |
| 12 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 13 | + |
| 14 | +use GraphQL\Error\DebugFlag; |
| 15 | +use GraphQL\Executor\ExecutionResult; |
| 16 | +use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter; |
| 17 | +use GraphQL\GraphQL; |
| 18 | +use GraphQL\Type\Definition\ObjectType; |
| 19 | +use GraphQL\Type\Definition\Type; |
| 20 | +use GraphQL\Type\Schema; |
| 21 | +use Psr\Http\Message\ServerRequestInterface; |
| 22 | +use React\Http\HttpServer; |
| 23 | +use React\Http\Message\Response; |
| 24 | +use React\Socket\SocketServer; |
| 25 | + |
| 26 | +$queryType = new ObjectType([ |
| 27 | + 'name' => 'Query', |
| 28 | + 'fields' => [ |
| 29 | + 'echo' => [ |
| 30 | + 'type' => Type::string(), |
| 31 | + 'args' => [ |
| 32 | + 'message' => ['type' => Type::string()], |
| 33 | + ], |
| 34 | + 'resolve' => function ($rootValue, array $args) { |
| 35 | + $deferred = new \React\Promise\Deferred(); |
| 36 | + $promise = $deferred->promise(); |
| 37 | + $promise = $promise = $promise->then(static fn (): string => $rootValue['prefix'] . $args['message']); |
| 38 | + $deferred->resolve(); |
| 39 | + |
| 40 | + return $promise; |
| 41 | + }, |
| 42 | + ], |
| 43 | + ], |
| 44 | +]); |
| 45 | +$mutationType = new ObjectType([ |
| 46 | + 'name' => 'Mutation', |
| 47 | + 'fields' => [ |
| 48 | + 'sum' => [ |
| 49 | + 'type' => Type::int(), |
| 50 | + 'args' => [ |
| 51 | + 'x' => ['type' => Type::int()], |
| 52 | + 'y' => ['type' => Type::int()], |
| 53 | + ], |
| 54 | + 'resolve' => static fn ($calc, array $args): int => $args['x'] + $args['y'], |
| 55 | + ], |
| 56 | + ], |
| 57 | +]); |
| 58 | +// See docs on schema options: |
| 59 | +// https://webonyx.github.io/graphql-php/schema-definition/#configuration-options |
| 60 | +$schema = new Schema([ |
| 61 | + 'query' => $queryType, |
| 62 | + 'mutation' => $mutationType, |
| 63 | +]); |
| 64 | +$react = new ReactPromiseAdapter(); |
| 65 | +$server = new HttpServer(function (ServerRequestInterface $request) use ($schema, $react) { |
| 66 | + $rawInput = (string) $request->getBody(); |
| 67 | + $input = json_decode($rawInput, true); |
| 68 | + $query = $input['query']; |
| 69 | + $variableValues = $input['variables'] ?? null; |
| 70 | + $rootValue = ['prefix' => 'You said: ']; |
| 71 | + $promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues); |
| 72 | + |
| 73 | + return $promise->then(function (ExecutionResult $result) { |
| 74 | + $output = json_encode($result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE)); |
| 75 | + |
| 76 | + return new Response( |
| 77 | + 200, |
| 78 | + [ |
| 79 | + 'Content-Type' => 'text/json', |
| 80 | + ], |
| 81 | + ($output !== false) ? $output : '' |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
| 85 | +$socket = new SocketServer('127.0.0.1:8010'); |
| 86 | +$server->listen($socket); |
| 87 | +echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress() ?? '') . PHP_EOL; |
0 commit comments