|
1 | | -<?php |
2 | | -declare(strict_types=1); |
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
3 | 3 | // Run local test server |
4 | 4 | // php graphql.php |
5 | 5 |
|
6 | 6 | // Try query |
7 | | -// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8010 |
| 7 | +// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080 |
8 | 8 |
|
9 | 9 | // Try mutation |
10 | | -// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8010 |
| 10 | +// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080 |
11 | 11 |
|
12 | 12 | require_once __DIR__ . '/../../vendor/autoload.php'; |
13 | 13 |
|
|
61 | 61 | 'query' => $queryType, |
62 | 62 | 'mutation' => $mutationType, |
63 | 63 | ]); |
| 64 | + |
64 | 65 | $react = new ReactPromiseAdapter(); |
65 | 66 | $server = new HttpServer(function (ServerRequestInterface $request) use ($schema, $react) { |
66 | | - $rawInput = (string) $request->getBody(); |
| 67 | + $rawInput = $request->getBody()->__toString(); |
| 68 | + |
67 | 69 | $input = json_decode($rawInput, true); |
68 | 70 | $query = $input['query']; |
69 | 71 | $variableValues = $input['variables'] ?? null; |
70 | | - $rootValue = ['prefix' => 'You said: ']; |
71 | | - $promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues); |
72 | 72 |
|
73 | | - return $promise->then(function (ExecutionResult $result) { |
74 | | - $output = json_encode($result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE)); |
| 73 | + $rootValue = ['prefix' => 'You said: ']; |
75 | 74 |
|
76 | | - return new Response( |
| 75 | + return GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues) |
| 76 | + ->then(fn (ExecutionResult $result): Response => new Response( |
77 | 77 | 200, |
78 | 78 | [ |
79 | | - 'Content-Type' => 'text/json', |
| 79 | + 'Content-Type' => 'application/json', |
80 | 80 | ], |
81 | | - ($output !== false) ? $output : '' |
82 | | - ); |
83 | | - }); |
| 81 | + json_encode($result->toArray(), JSON_THROW_ON_ERROR) |
| 82 | + )); |
84 | 83 | }); |
85 | | -$socket = new SocketServer('127.0.0.1:8010'); |
| 84 | + |
| 85 | +$socket = new SocketServer('127.0.0.1:8080'); |
86 | 86 | $server->listen($socket); |
87 | 87 | echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress() ?? '') . PHP_EOL; |
0 commit comments