Skip to content

Commit

Permalink
1.optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisdowson committed Jun 25, 2022
1 parent c0104d6 commit e2856b2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 67 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,8 +11,7 @@
"require": {
"php": "^7.4 || ^8",
"ext-json": "*",
"ext-mbstring": "*",
"react/http": "^1.6"
"ext-mbstring": "*"
},
"require-dev": {
"amphp/amp": "^2.6",
Expand All @@ -28,6 +27,7 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.17.0",
"psr/http-message": "^1",
"react/http": "^1.6",
"react/promise": "^2.9",
"symfony/polyfill-php81": "^1.23",
"symfony/var-exporter": "^5.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/04-hello-world-with-reactphp/README.md
@@ -1,6 +1,6 @@
## Hello World Example With Reactphp

This is a Simple yet full-featured example with reactphp.
This is a basic example using [ReactPHP](https://reactphp.org).

### Install reactphp

Expand Down
126 changes: 62 additions & 64 deletions examples/04-hello-world-with-reactphp/graphql.php
Expand Up @@ -4,82 +4,80 @@
// php graphql.php

// Try query
// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080
// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8010

// Try mutation
// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080
// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8010

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

use GraphQL\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter;
use React\Http\HttpServer;
use Psr\Http\Message\ServerRequestInterface;
use React\Socket\SocketServer;
use GraphQL\Executor\ExecutionResult;
use React\Http\Message\Response;

$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'echo' => [
'type' => Type::string(),
'args' => [
'message' => ['type' => Type::string()],
],
'resolve' => function ($rootValue, array $args) {
$deferred = new \React\Promise\Deferred();
$promise = $deferred->promise();
$promise = $promise->then(function () use ($rootValue, $args) {
return $rootValue['prefix'] . $args['message'];
});
$deferred->resolve();

return $promise;
},
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'echo' => [
'type' => Type::string(),
'args' => [
'message' => ['type' => Type::string()],
],
'resolve' => function ($rootValue, array $args) {
$deferred = new \React\Promise\Deferred();
$promise = $deferred->promise();
$promise = $promise = $promise->then(static fn (): string => $rootValue['prefix'] . $args['message']);
$deferred->resolve();
return $promise;
},
],
]);

$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => [
'sum' => [
'type' => Type::int(),
'args' => [
'x' => ['type' => Type::int()],
'y' => ['type' => Type::int()],
],
'resolve' => static fn ($calc, array $args): int => $args['x'] + $args['y'],
],
]);
$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => [
'sum' => [
'type' => Type::int(),
'args' => [
'x' => ['type' => Type::int()],
'y' => ['type' => Type::int()],
],
'resolve' => static fn ($calc, array $args): int => $args['x'] + $args['y'],
],
]);

// See docs on schema options:
// https://webonyx.github.io/graphql-php/schema-definition/#configuration-options
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
]);

$react = new \GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter();
$server = new \React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) use ($schema, $react) {
$rawInput = (string) $request->getBody();
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = $input['variables'] ?? null;
$rootValue = ['prefix' => 'You said: '];
$promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues);

return $promise->then(function (GraphQL\Executor\ExecutionResult $result) {
$output = $result->toArray(1);

return new \React\Http\Message\Response(
200,
[
'Content-Type' => 'text/json',
],
(json_encode($output) !== false) ? json_encode($output) : ''
);
});
],
]);
// See docs on schema options:
// https://webonyx.github.io/graphql-php/schema-definition/#configuration-options
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
]);
$react = new ReactPromiseAdapter();
$server = new HttpServer(function (ServerRequestInterface $request) use ($schema, $react) {
$rawInput = (string) $request->getBody();
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = $input['variables'] ?? null;
$rootValue = ['prefix' => 'You said: '];
$promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues);
return $promise->then(function (ExecutionResult $result) {
$output = json_encode($result->toArray(1));
return new Response(
200,
[
'Content-Type' => 'text/json',
],
($output !== false) ? $output : ''
);
});
$socket = new \React\Socket\SocketServer('127.0.0.1:8010');
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress() ?? '') . PHP_EOL;
});
$socket = new SocketServer('127.0.0.1:8010');
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress() ?? '') . PHP_EOL;

0 comments on commit e2856b2

Please sign in to comment.