diff --git a/examples/02-shorthand/README.md b/examples/02-shorthand/README.md new file mode 100644 index 0000000..dd7cfca --- /dev/null +++ b/examples/02-shorthand/README.md @@ -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) }" }' +``` diff --git a/examples/02-shorthand/graphql.php b/examples/02-shorthand/graphql.php new file mode 100644 index 0000000..7e07ffa --- /dev/null +++ b/examples/02-shorthand/graphql.php @@ -0,0 +1,31 @@ + [ + 'message' => $e->getMessage() + ] + ]; +} +header('Content-Type: application/json; charset=UTF-8'); +echo json_encode($result); + diff --git a/examples/02-shorthand/rootvalue.php b/examples/02-shorthand/rootvalue.php new file mode 100644 index 0000000..97e0a82 --- /dev/null +++ b/examples/02-shorthand/rootvalue.php @@ -0,0 +1,35 @@ + 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: ', +]; diff --git a/examples/02-shorthand/schema.graphqls b/examples/02-shorthand/schema.graphqls new file mode 100644 index 0000000..a8f11d2 --- /dev/null +++ b/examples/02-shorthand/schema.graphqls @@ -0,0 +1,13 @@ +schema { + query: Query + mutation: Calc +} + +type Calc { + sum(x: Int, y: Int): Int +} + +type Query { + echo(message: String): String +} +