Skip to content

Commit 2c7893d

Browse files
author
Pascal de Vink
committed
Add a first example on how to use this library
1 parent 39190ba commit 2c7893d

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

examples/shorthand/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Parsing GraphQL IDL shorthand
2+
3+
Shows how to build GraphQL schema from shorthand, wire up some resolvers, and add federation.
4+
5+
### Run locally
6+
```
7+
php -S localhost:8080 ./graphql.php
8+
```
9+
10+
### Try query
11+
```
12+
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
13+
```
14+
15+
### Try mutation
16+
```
17+
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
18+
```
19+
20+
### Try service sdl
21+
```
22+
curl http://localhost:8080 -d '{"query": "query { _service { sdl } }" }'
23+
```
24+
25+
### Try entities
26+
curl http://localhost:8080 -d '{"query":"query { _entities(representations: [{ __typename:\"User\", id:\"2\"}]) { __typename ... on User { id } } }","variables":{"_representations":[{"__typename":"User","id":"2"}]}}'

examples/shorthand/composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "pascaldevink/webonyx-implementation",
3+
"require": {
4+
"webonyx/graphql-php": "^0.13.8",
5+
"pascaldevink/graphql-federation": "^0.1.0"
6+
},
7+
"autoload": {
8+
"psr-4": {
9+
"App\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Pascal de Vink",
15+
"email": "pascal@ticketswap.com"
16+
}
17+
]
18+
}

examples/shorthand/graphql.php

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

examples/shorthand/rootvalue.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
interface Resolver {
4+
public function resolve($rootValue, $args, $context);
5+
}
6+
7+
class Addition implements Resolver
8+
{
9+
public function resolve($rootValue, $args, $context)
10+
{
11+
return $args['x'] + $args['y'];
12+
}
13+
}
14+
15+
class Echoer implements Resolver
16+
{
17+
public function resolve($rootValue, $args, $context)
18+
{
19+
return $rootValue['prefix'].$args['message'];
20+
}
21+
}
22+
23+
return [
24+
'sum' => function($rootValue, $args, $context) {
25+
$sum = new Addition();
26+
27+
return $sum->resolve($rootValue, $args, $context);
28+
},
29+
'echo' => function($rootValue, $args, $context) {
30+
$echo = new Echoer();
31+
32+
return $echo->resolve($rootValue, $args, $context);
33+
},
34+
'getUser' => function($rootValue, $args, $context) {
35+
return [
36+
'id' => 1,
37+
];
38+
},
39+
'prefix' => 'You said: ',
40+
];

examples/shorthand/schema.graphqls

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
schema {
2+
query: Query
3+
mutation: Mutation
4+
}
5+
6+
type Mutation {
7+
sum(x: Int, y: Int): Int
8+
}
9+
10+
type Query {
11+
echo(message: String): String
12+
# getUser: User
13+
}
14+
15+
type User @key(fields: "id") {
16+
id: ID!
17+
}

0 commit comments

Comments
 (0)