Skip to content

Commit cb40df2

Browse files
authored
Merge pull request #101 from PowerKiKi/patch-1
Make Hello World compatible with GraphiQL
2 parents ac5c518 + 5ca69c6 commit cb40df2

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

docs/getting-started.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ $schema = new Schema([
8282
]);
8383

8484
$rawInput = file_get_contents('php://input');
85+
$input = json_decode($rawInput, true);
86+
$query = $input['query'];
87+
$variableValues = isset($input['variables']) ? $input['variables'] : null;
8588

8689
try {
8790
$rootValue = ['prefix' => 'You said: '];
88-
$result = GraphQL::execute($schema, $rawInput, $rootValue);
91+
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
8992
} catch (\Exception $e) {
9093
$result = [
9194
'error' => [
@@ -100,7 +103,7 @@ echo json_encode($result);
100103
Our example is ready. Try it by running:
101104
```sh
102105
php -S localhost:8000 graphql.php
103-
curl http://localhost:8000 -d "query { echo(message: \"Hello World\") }"
106+
curl http://localhost:8000 -d '{"query": "query { echo(message: \"Hello World\") }" }'
104107
```
105108

106109
Check out the full [source code](https://github.com/webonyx/graphql-php/blob/master/examples/00-hello-world) of this example.

examples/00-hello-world/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ php -S localhost:8080 ./graphql.php
99

1010
### Try query
1111
```
12-
curl http://localhost:8080 -d "query { echo(message: \"Hello World\") }"
12+
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
1313
```
1414

1515
### Try mutation
1616
```
17-
curl http://localhost:8080 -d "mutation { sum(x: 2, y: 2) }"
17+
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
1818
```

examples/00-hello-world/graphql.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@
4848
]);
4949

5050
$rawInput = file_get_contents('php://input');
51+
$input = json_decode($rawInput, true);
52+
$query = $input['query'];
53+
$variableValues = isset($input['variables']) ? $input['variables'] : null;
5154

5255
$rootValue = ['prefix' => 'You said: '];
53-
$result = GraphQL::execute($schema, $rawInput, $rootValue);
56+
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
5457
} catch (\Exception $e) {
5558
$result = [
5659
'error' => [

0 commit comments

Comments
 (0)