From 62b003643779246403c0fd71d7e7486d94c50835 Mon Sep 17 00:00:00 2001 From: Torsten Blindert Date: Wed, 5 Dec 2018 18:55:14 +0100 Subject: [PATCH 1/3] BUGFIX: expect ->getType() to throw --- src/Utils/SchemaExtender.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Utils/SchemaExtender.php b/src/Utils/SchemaExtender.php index fab006e..110e750 100644 --- a/src/Utils/SchemaExtender.php +++ b/src/Utils/SchemaExtender.php @@ -513,7 +513,14 @@ class SchemaExtender $schemaExtensions[] = $def; } elseif ($def instanceof TypeDefinitionNode) { $typeName = isset($def->name) ? $def->name->value : null; - if ($schema->getType($typeName)) { + + try { + $type = $schema->getType($typeName); + } catch (Error $error) { + $type = null; + } + + if ($type) { throw new Error('Type "' . $typeName . '" already exists in the schema. It cannot also be defined in this type definition.', [$def]); } $typeDefinitionMap[$typeName] = $def; From d5fddfd504d438d92a32be28546a615cc49f3c6f Mon Sep 17 00:00:00 2001 From: Torsten Blindert Date: Wed, 5 Dec 2018 19:13:32 +0100 Subject: [PATCH 2/3] TASK: Added test --- tests/Utils/SchemaExtenderTest.php | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/Utils/SchemaExtenderTest.php b/tests/Utils/SchemaExtenderTest.php index 8965fa7..be07d1c 100644 --- a/tests/Utils/SchemaExtenderTest.php +++ b/tests/Utils/SchemaExtenderTest.php @@ -24,6 +24,7 @@ use GraphQL\Type\Definition\ScalarType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Schema; +use GraphQL\Utils\BuildSchema; use GraphQL\Utils\SchemaExtender; use GraphQL\Utils\SchemaPrinter; use PHPUnit\Framework\TestCase; @@ -1962,4 +1963,48 @@ extend type Query { $result = GraphQL::executeQuery($extendedSchema, $query); self::assertSame(['data' => ['hello' => 'Hello World!']], $result->toArray()); } + + + /** + * @see https://github.com/webonyx/graphql-php/issues/180 + */ + public function testShouldBeAbleToIntroduceNewTypesThroughExtension() + { + $sdl = ' + type Query { + defaultValue: String + } + type Foo { + value: Int + } + '; + + $documentNode = Parser::parse($sdl); + $schema = BuildSchema::build($documentNode); + + $extensionSdl = ' + type Bar { + foo: Foo + } + '; + + $extendedDocumentNode = Parser::parse($extensionSdl); + $extendedSchema = SchemaExtender::extend($schema, $extendedDocumentNode); + + $expected = ' + type Bar { + foo: Foo + } + + type Foo { + value: Int + } + + type Query { + defaultValue: String + } + '; + + static::assertEquals($this->dedent($expected), SchemaPrinter::doPrint($extendedSchema)); + } } From 9609d2ac84623291c76c4c72b61235799ad07f5e Mon Sep 17 00:00:00 2001 From: Torsten Blindert Date: Wed, 5 Dec 2018 19:23:07 +0100 Subject: [PATCH 3/3] TASK: Code style --- tests/Utils/SchemaExtenderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Utils/SchemaExtenderTest.php b/tests/Utils/SchemaExtenderTest.php index be07d1c..bc9842d 100644 --- a/tests/Utils/SchemaExtenderTest.php +++ b/tests/Utils/SchemaExtenderTest.php @@ -1980,7 +1980,7 @@ extend type Query { '; $documentNode = Parser::parse($sdl); - $schema = BuildSchema::build($documentNode); + $schema = BuildSchema::build($documentNode); $extensionSdl = ' type Bar { @@ -1989,7 +1989,7 @@ extend type Query { '; $extendedDocumentNode = Parser::parse($extensionSdl); - $extendedSchema = SchemaExtender::extend($schema, $extendedDocumentNode); + $extendedSchema = SchemaExtender::extend($schema, $extendedDocumentNode); $expected = ' type Bar {