Skip to content

Commit 1aa3ed7

Browse files
reused
1 parent 0a654cc commit 1aa3ed7

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

src/type/__tests__/validation-test.ts

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ describe('Type System: A Schema must have Object root types', () => {
341341
input SomeInputObject {
342342
test: String
343343
}
344+
input SomeInputObject2 {
345+
test: String
346+
}
347+
input SomeInputObject3 {
348+
test: String
349+
}
344350
`);
345351

346352
schema = extendSchema(
@@ -356,7 +362,7 @@ describe('Type System: A Schema must have Object root types', () => {
356362
schema,
357363
parse(`
358364
extend schema {
359-
mutation: SomeInputObject
365+
mutation: SomeInputObject2
360366
}
361367
`),
362368
);
@@ -365,7 +371,7 @@ describe('Type System: A Schema must have Object root types', () => {
365371
schema,
366372
parse(`
367373
extend schema {
368-
subscription: SomeInputObject
374+
subscription: SomeInputObject3
369375
}
370376
`),
371377
);
@@ -378,12 +384,12 @@ describe('Type System: A Schema must have Object root types', () => {
378384
},
379385
{
380386
message:
381-
'Mutation root type must be Object type if provided, it cannot be SomeInputObject.',
387+
'Mutation root type must be Object type if provided, it cannot be SomeInputObject2.',
382388
locations: [{ line: 3, column: 21 }],
383389
},
384390
{
385391
message:
386-
'Subscription root type must be Object type if provided, it cannot be SomeInputObject.',
392+
'Subscription root type must be Object type if provided, it cannot be SomeInputObject3.',
387393
locations: [{ line: 3, column: 25 }],
388394
},
389395
]);
@@ -427,6 +433,73 @@ describe('Type System: A Schema must have Object root types', () => {
427433
});
428434
});
429435

436+
describe('Type System: Root types must all be different if provided', () => {
437+
it('rejects a Schema where the same type is used for the "query" and "mutation" root types', () => {
438+
const schema = buildSchema(`
439+
type SomeObject {
440+
f: SomeObject
441+
}
442+
443+
schema {
444+
query: SomeObject
445+
mutation: SomeObject
446+
}
447+
`);
448+
expectJSON(validateSchema(schema)).toDeepEqual([
449+
{
450+
message:
451+
'All root types must be different, SomeObject is already used for "query" and cannot also be used for "mutation".',
452+
locations: [{ line: 8, column: 19 }],
453+
},
454+
]);
455+
});
456+
457+
it('rejects a Schema where the same type is used for the "query" and "subscription" root types', () => {
458+
const schema = buildSchema(`
459+
type SomeObject {
460+
f: SomeObject
461+
}
462+
463+
schema {
464+
query: SomeObject
465+
subscription: SomeObject
466+
}
467+
`);
468+
expectJSON(validateSchema(schema)).toDeepEqual([
469+
{
470+
message:
471+
'All root types must be different, SomeObject is already used for "query" and cannot also be used for "subscription".',
472+
locations: [{ line: 8, column: 23 }],
473+
},
474+
]);
475+
});
476+
477+
it('rejects a Schema where the same type is used for the "mutation" and "subscription" root types', () => {
478+
const schema = buildSchema(`
479+
type SomeObject {
480+
f: SomeObject
481+
}
482+
483+
type Query {
484+
f: SomeObject
485+
}
486+
487+
schema {
488+
query: Query
489+
mutation: SomeObject
490+
subscription: SomeObject
491+
}
492+
`);
493+
expectJSON(validateSchema(schema)).toDeepEqual([
494+
{
495+
message:
496+
'All root types must be different, SomeObject is already used for "mutation" and cannot also be used for "subscription".',
497+
locations: [{ line: 13, column: 23 }],
498+
},
499+
]);
500+
});
501+
});
502+
430503
describe('Type System: Objects must have fields', () => {
431504
it('accepts an Object type with fields object', () => {
432505
const schema = buildSchema(`

src/type/validate.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,44 @@ function validateRootTypes(context: SchemaValidationContext): void {
144144
(subscriptionType as any).astNode,
145145
);
146146
}
147+
148+
if (queryType && mutationType && queryType.name === mutationType.name) {
149+
context.reportError(
150+
'All root types must be different, ' +
151+
queryType.name +
152+
' is already used for "query" and cannot also be used for "mutation".',
153+
getOperationTypeNode(schema, OperationTypeNode.MUTATION) ??
154+
mutationType.astNode,
155+
);
156+
}
157+
158+
if (
159+
queryType &&
160+
subscriptionType &&
161+
queryType.name === subscriptionType.name
162+
) {
163+
context.reportError(
164+
'All root types must be different, ' +
165+
queryType.name +
166+
' is already used for "query" and cannot also be used for "subscription".',
167+
getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ??
168+
subscriptionType.astNode,
169+
);
170+
}
171+
172+
if (
173+
mutationType &&
174+
subscriptionType &&
175+
mutationType.name === subscriptionType.name
176+
) {
177+
context.reportError(
178+
'All root types must be different, ' +
179+
mutationType.name +
180+
' is already used for "mutation" and cannot also be used for "subscription".',
181+
getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ??
182+
subscriptionType.astNode,
183+
);
184+
}
147185
}
148186

149187
function getOperationTypeNode(

0 commit comments

Comments
 (0)