Make Hello World compatible with GraphiQL

This allow newcomers to follow documentation to get started and then
keep exploring with GraphiQL

Closes #87
This commit is contained in:
Adrien Crivelli 2017-03-22 10:53:00 +09:00
parent ac5c518cbe
commit 5ca69c6ec8
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
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' => [