Skip to content

Commit 7ae9950

Browse files
authored
Merge pull request #744 from turadg/prevent-type-dupes
Forbid duplicate type definitions
2 parents 3c0a536 + e964095 commit 7ae9950

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,4 +802,22 @@ fragment Foo on Type { field }
802802
.to.throw('Specified query type "Foo" not found in document.');
803803
});
804804

805+
it('Forbids duplicate type definitions', () => {
806+
const body = `
807+
schema {
808+
query: Repeated
809+
}
810+
811+
type Repeated {
812+
id: Int
813+
}
814+
815+
type Repeated {
816+
id: String
817+
}
818+
`;
819+
const doc = parse(body);
820+
expect(() => buildASTSchema(doc))
821+
.to.throw('Type "Repeated" was defined more than once.');
822+
});
805823
});

src/utilities/buildASTSchema.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
160160
case ENUM_TYPE_DEFINITION:
161161
case UNION_TYPE_DEFINITION:
162162
case INPUT_OBJECT_TYPE_DEFINITION:
163+
const typeName = d.name.value;
164+
if (nodeMap[typeName]) {
165+
throw new Error(`Type "${typeName}" was defined more than once.`);
166+
}
163167
typeDefs.push(d);
164-
nodeMap[d.name.value] = d;
168+
nodeMap[typeName] = d;
165169
break;
166170
case DIRECTIVE_DEFINITION:
167171
directiveDefs.push(d);

0 commit comments

Comments
 (0)