Skip to content

Commit e4af811

Browse files
committed
Improve ReactPHP example
1 parent 7bdef3b commit e4af811

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

examples/04-hello-world-with-reactphp/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
## Hello World Example With Reactphp
1+
## Hello World Example With ReactPHP
22

33
This is a basic example using [ReactPHP](https://reactphp.org).
44

5-
### Install reactphp
5+
### Dependencies
66

7-
run the command under the project root directory
7+
When you want to use this in your project, you need to install the
8+
following dependencies:
89

910
```
10-
composer require react/promise
11-
composer require react/http
11+
composer require react/promise react/http
1212
```
1313

1414
### Run locally

examples/04-hello-world-with-reactphp/graphql.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<?php
2-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
2+
33
// Run local test server
44
// php graphql.php
55

66
// 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
88

99
// 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
1111

1212
require_once __DIR__ . '/../../vendor/autoload.php';
1313

@@ -61,27 +61,27 @@
6161
'query' => $queryType,
6262
'mutation' => $mutationType,
6363
]);
64+
6465
$react = new ReactPromiseAdapter();
6566
$server = new HttpServer(function (ServerRequestInterface $request) use ($schema, $react) {
66-
$rawInput = (string) $request->getBody();
67+
$rawInput = $request->getBody()->__toString();
68+
6769
$input = json_decode($rawInput, true);
6870
$query = $input['query'];
6971
$variableValues = $input['variables'] ?? null;
70-
$rootValue = ['prefix' => 'You said: '];
71-
$promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues);
7272

73-
return $promise->then(function (ExecutionResult $result) {
74-
$output = json_encode($result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
73+
$rootValue = ['prefix' => 'You said: '];
7574

76-
return new Response(
75+
return GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues)
76+
->then(fn (ExecutionResult $result): Response => new Response(
7777
200,
7878
[
79-
'Content-Type' => 'text/json',
79+
'Content-Type' => 'application/json',
8080
],
81-
($output !== false) ? $output : ''
82-
);
83-
});
81+
json_encode($result->toArray(), JSON_THROW_ON_ERROR)
82+
));
8483
});
85-
$socket = new SocketServer('127.0.0.1:8010');
84+
85+
$socket = new SocketServer('127.0.0.1:8080');
8686
$server->listen($socket);
8787
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress() ?? '') . PHP_EOL;

0 commit comments

Comments
 (0)