mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 14:26:08 +03:00
Document shorthand usage
This commit is contained in:
parent
bc6a7a3d1d
commit
81376e7c34
19
examples/02-shorthand/README.md
Normal file
19
examples/02-shorthand/README.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Parsing GraphQL IDL shorthand
|
||||||
|
|
||||||
|
Same as the Hello world example but shows how to build GraphQL schema from shorthand
|
||||||
|
and wire up some resolvers
|
||||||
|
|
||||||
|
### Run locally
|
||||||
|
```
|
||||||
|
php -S localhost:8080 ./graphql.php
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try query
|
||||||
|
```
|
||||||
|
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try mutation
|
||||||
|
```
|
||||||
|
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
|
||||||
|
```
|
31
examples/02-shorthand/graphql.php
Normal file
31
examples/02-shorthand/graphql.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
// Test this using following command
|
||||||
|
// php -S localhost:8080 ./graphql.php &
|
||||||
|
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
|
||||||
|
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
|
||||||
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||||
|
|
||||||
|
use GraphQL\GraphQL;
|
||||||
|
use GraphQL\Utils\BuildSchema;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
|
||||||
|
$rootValue = include __DIR__ . '/rootvalue.php';
|
||||||
|
|
||||||
|
$rawInput = file_get_contents('php://input');
|
||||||
|
$input = json_decode($rawInput, true);
|
||||||
|
$query = $input['query'];
|
||||||
|
$variableValues = isset($input['variables']) ? $input['variables'] : null;
|
||||||
|
|
||||||
|
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$result = [
|
||||||
|
'error' => [
|
||||||
|
'message' => $e->getMessage()
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
header('Content-Type: application/json; charset=UTF-8');
|
||||||
|
echo json_encode($result);
|
||||||
|
|
35
examples/02-shorthand/rootvalue.php
Normal file
35
examples/02-shorthand/rootvalue.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
interface Resolver {
|
||||||
|
public function resolve($root, $args, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Addition implements Resolver
|
||||||
|
{
|
||||||
|
public function resolve($root, $args, $context)
|
||||||
|
{
|
||||||
|
return $args['x'] + $args['y'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Echoer implements Resolver
|
||||||
|
{
|
||||||
|
public function resolve($root, $args, $context)
|
||||||
|
{
|
||||||
|
return $root['prefix'].$args['message'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'sum' => function($root, $args, $context) {
|
||||||
|
$sum = new Addition();
|
||||||
|
|
||||||
|
return $sum->resolve($root, $args, $context);
|
||||||
|
},
|
||||||
|
'echo' => function($root, $args, $context) {
|
||||||
|
$echo = new Echoer();
|
||||||
|
|
||||||
|
return $echo->resolve($root, $args, $context);
|
||||||
|
},
|
||||||
|
'prefix' => 'You said: ',
|
||||||
|
];
|
13
examples/02-shorthand/schema.graphqls
Normal file
13
examples/02-shorthand/schema.graphqls
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
schema {
|
||||||
|
query: Query
|
||||||
|
mutation: Calc
|
||||||
|
}
|
||||||
|
|
||||||
|
type Calc {
|
||||||
|
sum(x: Int, y: Int): Int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
echo(message: String): String
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user