From 5ca69c6ec8a5f4b57426b2533db05edc0a8911c3 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Wed, 22 Mar 2017 10:53:00 +0900 Subject: [PATCH] Make Hello World compatible with GraphiQL This allow newcomers to follow documentation to get started and then keep exploring with GraphiQL Closes #87 --- docs/getting-started.md | 7 +++++-- examples/00-hello-world/README.md | 4 ++-- examples/00-hello-world/graphql.php | 5 ++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index d88a9d1..44f1940 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -82,10 +82,13 @@ $schema = new Schema([ ]); $rawInput = file_get_contents('php://input'); +$input = json_decode($rawInput, true); +$query = $input['query']; +$variableValues = isset($input['variables']) ? $input['variables'] : null; try { $rootValue = ['prefix' => 'You said: ']; - $result = GraphQL::execute($schema, $rawInput, $rootValue); + $result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues); } catch (\Exception $e) { $result = [ 'error' => [ @@ -100,7 +103,7 @@ echo json_encode($result); Our example is ready. Try it by running: ```sh php -S localhost:8000 graphql.php -curl http://localhost:8000 -d "query { echo(message: \"Hello World\") }" +curl http://localhost:8000 -d '{"query": "query { echo(message: \"Hello World\") }" }' ``` Check out the full [source code](https://github.com/webonyx/graphql-php/blob/master/examples/00-hello-world) of this example. diff --git a/examples/00-hello-world/README.md b/examples/00-hello-world/README.md index d4dded2..98a7393 100644 --- a/examples/00-hello-world/README.md +++ b/examples/00-hello-world/README.md @@ -9,10 +9,10 @@ php -S localhost:8080 ./graphql.php ### Try query ``` -curl http://localhost:8080 -d "query { echo(message: \"Hello World\") }" +curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }' ``` ### Try mutation ``` -curl http://localhost:8080 -d "mutation { sum(x: 2, y: 2) }" +curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }' ``` diff --git a/examples/00-hello-world/graphql.php b/examples/00-hello-world/graphql.php index 450089e..cb9e84f 100644 --- a/examples/00-hello-world/graphql.php +++ b/examples/00-hello-world/graphql.php @@ -48,9 +48,12 @@ try { ]); $rawInput = file_get_contents('php://input'); + $input = json_decode($rawInput, true); + $query = $input['query']; + $variableValues = isset($input['variables']) ? $input['variables'] : null; $rootValue = ['prefix' => 'You said: ']; - $result = GraphQL::execute($schema, $rawInput, $rootValue); + $result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues); } catch (\Exception $e) { $result = [ 'error' => [