Merge pull request #101 from PowerKiKi/patch-1

Make Hello World compatible with GraphiQL
This commit is contained in:
Vladimir Razuvaev 2017-03-22 13:41:59 +07:00 committed by GitHub
commit cb40df220e
3 changed files with 11 additions and 5 deletions

View File

@ -82,10 +82,13 @@ $schema = new Schema([
]); ]);
$rawInput = file_get_contents('php://input'); $rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
try { try {
$rootValue = ['prefix' => 'You said: ']; $rootValue = ['prefix' => 'You said: '];
$result = GraphQL::execute($schema, $rawInput, $rootValue); $result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
} catch (\Exception $e) { } catch (\Exception $e) {
$result = [ $result = [
'error' => [ 'error' => [
@ -100,7 +103,7 @@ echo json_encode($result);
Our example is ready. Try it by running: Our example is ready. Try it by running:
```sh ```sh
php -S localhost:8000 graphql.php 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. Check out the full [source code](https://github.com/webonyx/graphql-php/blob/master/examples/00-hello-world) of this example.

View File

@ -9,10 +9,10 @@ php -S localhost:8080 ./graphql.php
### Try query ### 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 ### 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) }" }'
``` ```

View File

@ -48,9 +48,12 @@ try {
]); ]);
$rawInput = file_get_contents('php://input'); $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: ']; $rootValue = ['prefix' => 'You said: '];
$result = GraphQL::execute($schema, $rawInput, $rootValue); $result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
} catch (\Exception $e) { } catch (\Exception $e) {
$result = [ $result = [
'error' => [ 'error' => [