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 +} + 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()); + } } /**