Skip to content

Commit cf6eddf

Browse files
authored
Update standard server example (#870)
* Update standard server example * Unify curl
1 parent a123043 commit cf6eddf

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

examples/00-hello-world/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Hello world
2+
23
Clean and simple single-file example of main GraphQL concepts originally proposed and
3-
implemented by [Leo Cavalcante](https://github.com/leocavalcante)
4+
implemented by [Leo Cavalcante](https://github.com/leocavalcante).
45

56
### Run locally
67
```
@@ -9,10 +10,10 @@ php -S localhost:8080 ./graphql.php
910

1011
### Try query
1112
```
12-
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
13+
curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080
1314
```
1415

1516
### Try mutation
1617
```
17-
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
18+
curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080
1819
```

examples/00-hello-world/graphql.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
declare(strict_types=1);
44

5-
// Test this using following command
6-
// php -S localhost:8080 ./graphql.php &
5+
// Test this using the following command:
6+
// php -S localhost:8080 graphql.php
7+
8+
// Try the following example queries:
79
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
810
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
11+
912
require_once __DIR__ . '/../../vendor/autoload.php';
1013

1114
use GraphQL\GraphQL;

examples/02-schema-definition-language/README.md

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

1111
### Try query
1212
```
13-
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
13+
curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080
1414
```
1515

1616
### Try mutation
1717
```
18-
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
18+
curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080
1919
```

examples/03-server/README.md renamed to examples/03-standard-server/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Hello world
2-
Same example as 00-hello-world, but uses
3-
[Standard Http Server](https://webonyx.github.io/graphql-php/executing-queries/#using-server)
4-
instead of manual parsing of incoming data.
2+
3+
Same as the Hello World example, but uses the [Standard Http Server](https://webonyx.github.io/graphql-php/executing-queries/#using-server)
4+
instead of manually parsing the incoming data.
55

66
### Run locally
77
```

examples/03-server/graphql.php renamed to examples/03-standard-server/graphql.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<?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:
49
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
510
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
11+
612
require_once __DIR__ . '/../../vendor/autoload.php';
713

14+
use GraphQL\Server\StandardServer;
815
use GraphQL\Type\Definition\ObjectType;
916
use GraphQL\Type\Definition\Type;
1017
use GraphQL\Type\Schema;
11-
use GraphQL\Server\StandardServer;
1218

1319
try {
1420
$queryType = new ObjectType([
@@ -19,9 +25,9 @@
1925
'args' => [
2026
'message' => ['type' => Type::string()],
2127
],
22-
'resolve' => function ($rootValue, $args) {
28+
'resolve' => static function ($rootValue, array $args): string {
2329
return $rootValue['prefix'] . $args['message'];
24-
}
30+
},
2531
],
2632
],
2733
]);
@@ -35,7 +41,7 @@
3541
'x' => ['type' => Type::int()],
3642
'y' => ['type' => Type::int()],
3743
],
38-
'resolve' => function ($calc, $args) {
44+
'resolve' => static function ($calc, array $args): int {
3945
return $args['x'] + $args['y'];
4046
},
4147
],
@@ -51,11 +57,9 @@
5157

5258
// See docs on server options:
5359
// 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]);
5761

5862
$server->handleRequest();
59-
} catch (\Exception $e) {
63+
} catch (Throwable $e) {
6064
StandardServer::send500Error($e);
6165
}

phpcs.xml.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<arg value="nps" />
1111

1212
<file>benchmarks</file>
13-
<file>examples/00-hello-world</file>
14-
<file>examples/01-blog</file>
15-
<file>examples/02-schema-definition-language</file>
13+
<file>examples</file>
1614
<file>src</file>
1715
<file>tests</file>
1816

phpstan.neon.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ parameters:
55

66
paths:
77
- %currentWorkingDirectory%/benchmarks
8-
- %currentWorkingDirectory%/examples/00-hello-world
9-
- %currentWorkingDirectory%/examples/01-blog
10-
- %currentWorkingDirectory%/examples/02-schema-definition-language
8+
- %currentWorkingDirectory%/examples
119
- %currentWorkingDirectory%/src
1210
- %currentWorkingDirectory%/tests
1311

0 commit comments

Comments
 (0)