Skip to content

Commit 81376e7

Browse files
committed
Document shorthand usage
1 parent bc6a7a3 commit 81376e7

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

examples/02-shorthand/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Parsing GraphQL IDL shorthand
2+
3+
Same as the Hello world example but shows how to build GraphQL schema from shorthand
4+
and wire up some resolvers
5+
6+
### Run locally
7+
```
8+
php -S localhost:8080 ./graphql.php
9+
```
10+
11+
### Try query
12+
```
13+
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
14+
```
15+
16+
### Try mutation
17+
```
18+
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
19+
```

examples/02-shorthand/graphql.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
// Test this using following command
3+
// php -S localhost:8080 ./graphql.php &
4+
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
5+
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
6+
require_once __DIR__ . '/../../vendor/autoload.php';
7+
8+
use GraphQL\GraphQL;
9+
use GraphQL\Utils\BuildSchema;
10+
11+
try {
12+
13+
$schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
14+
$rootValue = include __DIR__ . '/rootvalue.php';
15+
16+
$rawInput = file_get_contents('php://input');
17+
$input = json_decode($rawInput, true);
18+
$query = $input['query'];
19+
$variableValues = isset($input['variables']) ? $input['variables'] : null;
20+
21+
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
22+
} catch (\Exception $e) {
23+
$result = [
24+
'error' => [
25+
'message' => $e->getMessage()
26+
]
27+
];
28+
}
29+
header('Content-Type: application/json; charset=UTF-8');
30+
echo json_encode($result);
31+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
interface Resolver {
4+
public function resolve($root, $args, $context);
5+
}
6+
7+
class Addition implements Resolver
8+
{
9+
public function resolve($root, $args, $context)
10+
{
11+
return $args['x'] + $args['y'];
12+
}
13+
}
14+
15+
class Echoer implements Resolver
16+
{
17+
public function resolve($root, $args, $context)
18+
{
19+
return $root['prefix'].$args['message'];
20+
}
21+
}
22+
23+
return [
24+
'sum' => function($root, $args, $context) {
25+
$sum = new Addition();
26+
27+
return $sum->resolve($root, $args, $context);
28+
},
29+
'echo' => function($root, $args, $context) {
30+
$echo = new Echoer();
31+
32+
return $echo->resolve($root, $args, $context);
33+
},
34+
'prefix' => 'You said: ',
35+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
schema {
2+
query: Query
3+
mutation: Calc
4+
}
5+
6+
type Calc {
7+
sum(x: Int, y: Int): Int
8+
}
9+
10+
type Query {
11+
echo(message: String): String
12+
}
13+

0 commit comments

Comments
 (0)