From 81376e7c34c4b48834dabc7f4dacf2c274515da8 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 7 Jul 2017 20:26:01 +0100 Subject: [PATCH 1/2] 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 +} + From 8fe26a1a21e3a96e470b5712bc9d392239e8308f Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 18 Jul 2017 00:25:45 +0700 Subject: [PATCH 2/2] String and ID types should not try to convert non-scalar values to string (#121) --- src/Type/Definition/IDType.php | 8 ++++++++ src/Type/Definition/StringType.php | 8 ++++++++ tests/Type/ScalarSerializationTest.php | 15 +++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Type/Definition/IDType.php b/src/Type/Definition/IDType.php index b45cfeb..89f4ff2 100644 --- a/src/Type/Definition/IDType.php +++ b/src/Type/Definition/IDType.php @@ -1,8 +1,10 @@ assertSame('-1.1', $stringType->serialize(-1.1)); $this->assertSame('true', $stringType->serialize(true)); $this->assertSame('false', $stringType->serialize(false)); + $this->assertSame('null', $stringType->serialize(null)); + + try { + $stringType->serialize([]); + $this->fail('Expected exception was not thrown'); + } catch (UserError $e) { + $this->assertEquals('String cannot represent non scalar value: array', $e->getMessage()); + } + + try { + $stringType->serialize(new \stdClass()); + $this->fail('Expected exception was not thrown'); + } catch (UserError $e) { + $this->assertEquals('String cannot represent non scalar value: instance of stdClass', $e->getMessage()); + } } /**