Skip to content

Commit a123043

Browse files
authored
Update examples/02-shorthand (#868)
1 parent d9338c2 commit a123043

File tree

7 files changed

+50
-82
lines changed

7 files changed

+50
-82
lines changed

examples/02-shorthand/README.md renamed to examples/02-schema-definition-language/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Parsing GraphQL IDL shorthand
1+
# Schema Definition Language
22

3-
Same as the Hello world example but shows how to build GraphQL schema from shorthand
4-
and wire up some resolvers
3+
Same as the Hello world example, but shows how to use schema definition language
4+
and wire up some resolvers as plain functions.
55

66
### Run locally
77
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// Test this using the following command:
6+
// php -S localhost:8080 graphql.php
7+
8+
// Try the following example queries:
9+
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
10+
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
11+
12+
require_once __DIR__ . '/../../vendor/autoload.php';
13+
14+
use GraphQL\GraphQL;
15+
use GraphQL\Utils\BuildSchema;
16+
17+
try {
18+
$schema = BuildSchema::build(/** @lang GraphQL */ '
19+
type Query {
20+
echo(message: String!): String!
21+
sum(x: Int!, y: Int!): Int!
22+
}
23+
');
24+
$rootValue = [
25+
'echo' => static fn (array $rootValue, array $args): string => $rootValue['prefix'] . $args['message'],
26+
'sum' => static fn (array $rootValue, array $args): int => $args['x'] + $args['y'],
27+
'prefix' => 'You said: ',
28+
];
29+
30+
$rawInput = file_get_contents('php://input');
31+
$input = json_decode($rawInput, true);
32+
$query = $input['query'];
33+
$variableValues = $input['variables'] ?? null;
34+
35+
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
36+
} catch (Throwable $e) {
37+
$result = [
38+
'error' => [
39+
'message' => $e->getMessage(),
40+
],
41+
];
42+
}
43+
44+
header('Content-Type: application/json; charset=UTF-8');
45+
echo json_encode($result);

examples/02-shorthand/graphql.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/02-shorthand/rootvalue.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/02-shorthand/schema.graphqls

Lines changed: 0 additions & 13 deletions
This file was deleted.

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<file>benchmarks</file>
1313
<file>examples/00-hello-world</file>
1414
<file>examples/01-blog</file>
15+
<file>examples/02-schema-definition-language</file>
1516
<file>src</file>
1617
<file>tests</file>
1718

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- %currentWorkingDirectory%/benchmarks
88
- %currentWorkingDirectory%/examples/00-hello-world
99
- %currentWorkingDirectory%/examples/01-blog
10+
- %currentWorkingDirectory%/examples/02-schema-definition-language
1011
- %currentWorkingDirectory%/src
1112
- %currentWorkingDirectory%/tests
1213

0 commit comments

Comments
 (0)