diff --git a/composer.json b/composer.json index 65b4ff548..cc12c7835 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "psr/http-message": "^1", "react/http": "^1.6", "react/promise": "^2.9", + "amphp/http-server": "^2.1", "rector/rector": "^0.15.18", "symfony/polyfill-php81": "^1.23", "symfony/var-exporter": "^5 || ^6", @@ -35,7 +36,8 @@ }, "suggest": { "psr/http-message": "To use standard GraphQL server", - "react/promise": "To leverage async resolving on React PHP platform" + "react/promise": "To leverage async resolving on React PHP platform", + "amphp/http-server": "To leverage async resolving with webserver on AMPHP platform" }, "autoload": { "psr-4": { diff --git a/examples/04-async-php/amphp/event-loop/graphql.php b/examples/04-async-php/amphp/event-loop/graphql.php index de85b3934..70bab2c37 100644 --- a/examples/04-async-php/amphp/event-loop/graphql.php +++ b/examples/04-async-php/amphp/event-loop/graphql.php @@ -13,7 +13,13 @@ $schema = require_once __DIR__ . '/../schema.php'; Loop::run(function () use ($schema): void { - $input = json_decode(file_get_contents('php://input'), true); + $content = file_get_contents('php://input'); + $input = ''; + + if ($content !== false) { + $input = json_decode($content, true); + } + $promise = GraphQL::promiseToExecute( new AmpPromiseAdapter(), $schema, diff --git a/examples/04-async-php/amphp/http-server/graphql.php b/examples/04-async-php/amphp/http-server/graphql.php index 9838126f5..beaa1a4a8 100644 --- a/examples/04-async-php/amphp/http-server/graphql.php +++ b/examples/04-async-php/amphp/http-server/graphql.php @@ -27,14 +27,14 @@ $query = $input['query']; $variableValues = $input['variables'] ?? null; $promise = GraphQL::promiseToExecute(new AmpPromiseAdapter(), $schema, $query, [], null, $variableValues); - $promise = $promise->then(fn(ExecutionResult $result): Response => - new Response( + $promise = $promise->then(function(ExecutionResult $result): Response { + $data = json_encode($result->toArray()); + return new Response( 200, ['Content-Type' => 'application/json'], - json_encode($result->toArray()) - ) - ); - + $data !== false ? $data : null + ); + }); return $promise->adoptedPromise; }), new Psr\Log\NullLogger);