From 81376e7c34c4b48834dabc7f4dacf2c274515da8 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 7 Jul 2017 20:26:01 +0100 Subject: [PATCH] Document shorthand usage --- examples/02-shorthand/README.md | 19 +++++++++++++++ examples/02-shorthand/graphql.php | 31 ++++++++++++++++++++++++ examples/02-shorthand/rootvalue.php | 35 +++++++++++++++++++++++++++ examples/02-shorthand/schema.graphqls | 13 ++++++++++ 4 files changed, 98 insertions(+) create mode 100644 examples/02-shorthand/README.md create mode 100644 examples/02-shorthand/graphql.php create mode 100644 examples/02-shorthand/rootvalue.php create mode 100644 examples/02-shorthand/schema.graphqls 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 +} +