Forbid duplicate type definitions

This commit is contained in:
Vladimir Razuvaev 2017-07-04 13:59:46 +07:00
parent c3db8de9e7
commit 76e182e616
2 changed files with 35 additions and 1 deletions

View File

@ -112,8 +112,12 @@ class BuildSchema
case NodeKind::ENUM_TYPE_DEFINITION:
case NodeKind::UNION_TYPE_DEFINITION:
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
$typeName = $d->name->value;
if (!empty($this->nodeMap[$typeName])) {
throw new Error("Type \"$typeName\" was defined more than once.");
}
$typeDefs[] = $d;
$this->nodeMap[$d->name->value] = $d;
$this->nodeMap[$typeName] = $d;
break;
case NodeKind::DIRECTIVE_DEFINITION:
$directiveDefs[] = $d;
@ -453,6 +457,12 @@ class BuildSchema
]);
}
/**
* Given a collection of directives, returns the string value for the
* deprecation reason.
*
* @param $directives
*/
private function getDeprecationReason($directives)
{
$deprecatedAST = $directives ? Utils::find(

View File

@ -896,4 +896,28 @@ fragment Foo on Type { field }
$doc = Parser::parse($body);
BuildSchema::buildAST($doc);
}
/**
* @it Forbids duplicate type definitions
*/
public function testForbidsDuplicateTypeDefinitions()
{
$body = '
schema {
query: Repeated
}
type Repeated {
id: Int
}
type Repeated {
id: String
}
';
$doc = Parser::parse($body);
$this->setExpectedException('GraphQL\Error\Error', 'Type "Repeated" was defined more than once.');
BuildSchema::buildAST($doc);
}
}