mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
Merge pull request #417 from bytorsten/extension-fix
BUGFIX: ExtendSchema should be able to introduce new types
This commit is contained in:
commit
ead704022c
@ -513,7 +513,14 @@ class SchemaExtender
|
|||||||
$schemaExtensions[] = $def;
|
$schemaExtensions[] = $def;
|
||||||
} elseif ($def instanceof TypeDefinitionNode) {
|
} elseif ($def instanceof TypeDefinitionNode) {
|
||||||
$typeName = isset($def->name) ? $def->name->value : null;
|
$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]);
|
throw new Error('Type "' . $typeName . '" already exists in the schema. It cannot also be defined in this type definition.', [$def]);
|
||||||
}
|
}
|
||||||
$typeDefinitionMap[$typeName] = $def;
|
$typeDefinitionMap[$typeName] = $def;
|
||||||
|
@ -24,6 +24,7 @@ use GraphQL\Type\Definition\ScalarType;
|
|||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
use GraphQL\Type\Definition\UnionType;
|
use GraphQL\Type\Definition\UnionType;
|
||||||
use GraphQL\Type\Schema;
|
use GraphQL\Type\Schema;
|
||||||
|
use GraphQL\Utils\BuildSchema;
|
||||||
use GraphQL\Utils\SchemaExtender;
|
use GraphQL\Utils\SchemaExtender;
|
||||||
use GraphQL\Utils\SchemaPrinter;
|
use GraphQL\Utils\SchemaPrinter;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
@ -1962,4 +1963,48 @@ extend type Query {
|
|||||||
$result = GraphQL::executeQuery($extendedSchema, $query);
|
$result = GraphQL::executeQuery($extendedSchema, $query);
|
||||||
self::assertSame(['data' => ['hello' => 'Hello World!']], $result->toArray());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user