|
1 | 1 | <?php |
2 | | -// Test this using following command |
3 | | -// php -S localhost:8080 ./graphql.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: |
4 | 9 | // curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }' |
5 | 10 | // curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }' |
| 11 | + |
6 | 12 | require_once __DIR__ . '/../../vendor/autoload.php'; |
7 | 13 |
|
| 14 | +use GraphQL\Server\StandardServer; |
8 | 15 | use GraphQL\Type\Definition\ObjectType; |
9 | 16 | use GraphQL\Type\Definition\Type; |
10 | 17 | use GraphQL\Type\Schema; |
11 | | -use GraphQL\Server\StandardServer; |
12 | 18 |
|
13 | 19 | try { |
14 | 20 | $queryType = new ObjectType([ |
|
19 | 25 | 'args' => [ |
20 | 26 | 'message' => ['type' => Type::string()], |
21 | 27 | ], |
22 | | - 'resolve' => function ($rootValue, $args) { |
| 28 | + 'resolve' => static function ($rootValue, array $args): string { |
23 | 29 | return $rootValue['prefix'] . $args['message']; |
24 | | - } |
| 30 | + }, |
25 | 31 | ], |
26 | 32 | ], |
27 | 33 | ]); |
|
35 | 41 | 'x' => ['type' => Type::int()], |
36 | 42 | 'y' => ['type' => Type::int()], |
37 | 43 | ], |
38 | | - 'resolve' => function ($calc, $args) { |
| 44 | + 'resolve' => static function ($calc, array $args): int { |
39 | 45 | return $args['x'] + $args['y']; |
40 | 46 | }, |
41 | 47 | ], |
|
51 | 57 |
|
52 | 58 | // See docs on server options: |
53 | 59 | // https://webonyx.github.io/graphql-php/executing-queries/#server-configuration-options |
54 | | - $server = new StandardServer([ |
55 | | - 'schema' => $schema |
56 | | - ]); |
| 60 | + $server = new StandardServer(['schema' => $schema]); |
57 | 61 |
|
58 | 62 | $server->handleRequest(); |
59 | | -} catch (\Exception $e) { |
| 63 | +} catch (Throwable $e) { |
60 | 64 | StandardServer::send500Error($e); |
61 | 65 | } |
0 commit comments