Skip to content

Commit 7bdef3b

Browse files
authored
Add example with ReactPHP (#1250)
1 parent cedab08 commit 7bdef3b

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"phpstan/phpstan-strict-rules": "1.4.4",
2727
"phpunit/phpunit": "^9.5",
2828
"psr/http-message": "^1",
29-
"react/promise": "^2",
29+
"react/http": "^1.6",
30+
"react/promise": "^2.9",
3031
"symfony/polyfill-php81": "^1.23",
3132
"symfony/var-exporter": "^5 || ^6",
3233
"thecodingmachine/safe": "^1.3"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Hello World Example With Reactphp
2+
3+
This is a basic example using [ReactPHP](https://reactphp.org).
4+
5+
### Install reactphp
6+
7+
run the command under the project root directory
8+
9+
```
10+
composer require react/promise
11+
composer require react/http
12+
```
13+
14+
### Run locally
15+
16+
```
17+
php graphql.php
18+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)