Skip to content

Commit ffe76c5

Browse files
helferleebyron
authored andcommitted
Add sanity checks for schema to allow only a single query, mutation, subscription in schema
1 parent a981043 commit ffe76c5

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,68 @@ type Hello {
446446
.to.throw('Must provide schema definition with query type.');
447447
});
448448

449+
it('Allows only a single query type', () => {
450+
const body = `
451+
schema {
452+
query: Hello
453+
query: Yellow
454+
}
455+
456+
type Hello {
457+
bar: Bar
458+
}
459+
460+
type Yellow {
461+
isColor: Boolean
462+
}
463+
`;
464+
const doc = parse(body);
465+
expect(() => buildASTSchema(doc))
466+
.to.throw('Must provide only one query type in schema.');
467+
});
468+
469+
it('Allows only a single mutation type', () => {
470+
const body = `
471+
schema {
472+
query: Hello
473+
mutation: Hello
474+
mutation: Yellow
475+
}
476+
477+
type Hello {
478+
bar: Bar
479+
}
480+
481+
type Yellow {
482+
isColor: Boolean
483+
}
484+
`;
485+
const doc = parse(body);
486+
expect(() => buildASTSchema(doc))
487+
.to.throw('Must provide only one mutation type in schema.');
488+
});
489+
490+
it('Allows only a single subscription type', () => {
491+
const body = `
492+
schema {
493+
query: Hello
494+
subscription: Hello
495+
subscription: Yellow
496+
}
497+
498+
type Hello {
499+
bar: Bar
500+
}
501+
502+
type Yellow {
503+
isColor: Boolean
504+
}
505+
`;
506+
const doc = parse(body);
507+
expect(() => buildASTSchema(doc))
508+
.to.throw('Must provide only one subscription type in schema.');
509+
});
510+
449511
it('Unknown type referenced', () => {
450512
const body = `
451513
schema {

src/utilities/buildASTSchema.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ function getNamedTypeAST(typeAST: Type): NamedType {
111111
}
112112

113113
/**
114-
* This takes the ast of a schema document produced by parseSchema in
115-
* src/language/schema/parser.js.
114+
* This takes the ast of a schema document produced by the parse function in
115+
* src/language/parser.js.
116116
*
117117
* Given that AST it constructs a GraphQLSchema. As constructed
118118
* they are not particularly useful for non-introspection queries
@@ -160,10 +160,19 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
160160
schemaDef.operationTypes.forEach(operationType => {
161161
const typeName = operationType.type.name.value;
162162
if (operationType.operation === 'query') {
163+
if (queryTypeName) {
164+
throw new Error('Must provide only one query type in schema.');
165+
}
163166
queryTypeName = typeName;
164167
} else if (operationType.operation === 'mutation') {
168+
if (mutationTypeName) {
169+
throw new Error('Must provide only one mutation type in schema.');
170+
}
165171
mutationTypeName = typeName;
166172
} else if (operationType.operation === 'subscription') {
173+
if (subscriptionTypeName) {
174+
throw new Error('Must provide only one subscription type in schema.');
175+
}
167176
subscriptionTypeName = typeName;
168177
}
169178
});

0 commit comments

Comments
 (0)