Skip to content

Commit 3217802

Browse files
Generate error per duplicate schema def + Extend tests (#1451)
1 parent 92d8edd commit 3217802

File tree

5 files changed

+274
-107
lines changed

5 files changed

+274
-107
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -809,26 +809,6 @@ describe('Schema Builder', () => {
809809
});
810810

811811
describe('Failures', () => {
812-
it('Allows only a single schema definition', () => {
813-
const body = dedent`
814-
schema {
815-
query: Hello
816-
}
817-
818-
schema {
819-
query: Hello
820-
}
821-
822-
type Hello {
823-
bar: Bar
824-
}
825-
`;
826-
const doc = parse(body);
827-
expect(() => buildASTSchema(doc)).to.throw(
828-
'Must provide only one schema definition.',
829-
);
830-
});
831-
832812
it('Allows only a single query type', () => {
833813
const body = dedent`
834814
schema {
@@ -837,7 +817,7 @@ describe('Failures', () => {
837817
}
838818
839819
type Hello {
840-
bar: Bar
820+
bar: String
841821
}
842822
843823
type Yellow {
@@ -859,7 +839,7 @@ describe('Failures', () => {
859839
}
860840
861841
type Hello {
862-
bar: Bar
842+
bar: String
863843
}
864844
865845
type Yellow {
@@ -881,7 +861,7 @@ describe('Failures', () => {
881861
}
882862
883863
type Hello {
884-
bar: Bar
864+
bar: String
885865
}
886866
887867
type Yellow {

src/utilities/__tests__/extendSchema-test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,21 +1288,6 @@ describe('extendSchema', () => {
12881288
expect(schema.getMutationType()).to.equal(null);
12891289
});
12901290

1291-
it('does not allow overriding schema within an extension', () => {
1292-
const sdl = `
1293-
schema {
1294-
mutation: Mutation
1295-
}
1296-
1297-
type Mutation {
1298-
doSomething: String
1299-
}
1300-
`;
1301-
expect(() => extendTestSchema(sdl)).to.throw(
1302-
'Cannot define a new schema within a schema extension.',
1303-
);
1304-
});
1305-
13061291
it('adds schema definition missing in the original schema', () => {
13071292
let schema = new GraphQLSchema({
13081293
directives: [FooDirective],

src/validation/__tests__/KnownDirectives-test.js

Lines changed: 102 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -191,46 +191,92 @@ describe('Validate: Known directives', () => {
191191
`).to.deep.equal([]);
192192
});
193193

194-
it('with well placed directives', () => {
194+
it('with directive defined in schema extension', () => {
195+
const schema = buildSchema(`
196+
type Query {
197+
foo: String
198+
}
199+
`);
195200
expectSDLErrors(
196201
`
197-
type MyObj implements MyInterface @onObject {
198-
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
199-
}
202+
directive @test on OBJECT
200203
201-
extend type MyObj @onObject
204+
extend type Query @test
205+
`,
206+
schema,
207+
).to.deep.equal([]);
208+
});
202209

203-
scalar MyScalar @onScalar
210+
it('with directive used in schema extension', () => {
211+
const schema = buildSchema(`
212+
directive @test on OBJECT
204213
205-
extend scalar MyScalar @onScalar
214+
type Query {
215+
foo: String
216+
}
217+
`);
218+
expectSDLErrors(
219+
`
220+
extend type Query @test
221+
`,
222+
schema,
223+
).to.deep.equal([]);
224+
});
206225

207-
interface MyInterface @onInterface {
208-
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
226+
it('with unknown directive in schema extension', () => {
227+
const schema = buildSchema(`
228+
type Query {
229+
foo: String
209230
}
231+
`);
232+
expectSDLErrors(
233+
`
234+
extend type Query @unknown
235+
`,
236+
schema,
237+
).to.deep.equal([unknownDirective('unknown', 2, 29)]);
238+
});
210239

211-
extend interface MyInterface @onInterface
240+
it('with well placed directives', () => {
241+
expectSDLErrors(
242+
`
243+
type MyObj implements MyInterface @onObject {
244+
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
245+
}
212246
213-
union MyUnion @onUnion = MyObj | Other
247+
extend type MyObj @onObject
214248
215-
extend union MyUnion @onUnion
249+
scalar MyScalar @onScalar
216250
217-
enum MyEnum @onEnum {
218-
MY_VALUE @onEnumValue
219-
}
251+
extend scalar MyScalar @onScalar
220252
221-
extend enum MyEnum @onEnum
253+
interface MyInterface @onInterface {
254+
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
255+
}
222256
223-
input MyInput @onInputObject {
224-
myField: Int @onInputFieldDefinition
225-
}
257+
extend interface MyInterface @onInterface
226258
227-
extend input MyInput @onInputObject
259+
union MyUnion @onUnion = MyObj | Other
228260
229-
schema @onSchema {
230-
query: MyQuery
231-
}
261+
extend union MyUnion @onUnion
262+
263+
enum MyEnum @onEnum {
264+
MY_VALUE @onEnumValue
265+
}
266+
267+
extend enum MyEnum @onEnum
268+
269+
input MyInput @onInputObject {
270+
myField: Int @onInputFieldDefinition
271+
}
272+
273+
extend input MyInput @onInputObject
274+
275+
schema @onSchema {
276+
query: MyQuery
277+
}
232278
233-
extend schema @onSchema
279+
extend schema @onSchema
234280
`,
235281
schemaWithSDLDirectives,
236282
).to.deep.equal([]);
@@ -239,63 +285,63 @@ describe('Validate: Known directives', () => {
239285
it('with misplaced directives', () => {
240286
expectSDLErrors(
241287
`
242-
type MyObj implements MyInterface @onInterface {
243-
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
244-
}
288+
type MyObj implements MyInterface @onInterface {
289+
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
290+
}
245291
246-
scalar MyScalar @onEnum
292+
scalar MyScalar @onEnum
247293
248-
interface MyInterface @onObject {
249-
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
250-
}
294+
interface MyInterface @onObject {
295+
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
296+
}
251297
252-
union MyUnion @onEnumValue = MyObj | Other
298+
union MyUnion @onEnumValue = MyObj | Other
253299
254-
enum MyEnum @onScalar {
255-
MY_VALUE @onUnion
256-
}
300+
enum MyEnum @onScalar {
301+
MY_VALUE @onUnion
302+
}
257303
258-
input MyInput @onEnum {
259-
myField: Int @onArgumentDefinition
260-
}
304+
input MyInput @onEnum {
305+
myField: Int @onArgumentDefinition
306+
}
261307
262-
schema @onObject {
263-
query: MyQuery
264-
}
308+
schema @onObject {
309+
query: MyQuery
310+
}
265311
266-
extend schema @onObject
312+
extend schema @onObject
267313
`,
268314
schemaWithSDLDirectives,
269315
).to.deep.equal([
270-
misplacedDirective('onInterface', 'OBJECT', 2, 43),
316+
misplacedDirective('onInterface', 'OBJECT', 2, 45),
271317
misplacedDirective(
272318
'onInputFieldDefinition',
273319
'ARGUMENT_DEFINITION',
274320
3,
275-
30,
321+
32,
276322
),
277-
misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 3, 63),
278-
misplacedDirective('onEnum', 'SCALAR', 6, 25),
279-
misplacedDirective('onObject', 'INTERFACE', 8, 31),
323+
misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 3, 65),
324+
misplacedDirective('onEnum', 'SCALAR', 6, 27),
325+
misplacedDirective('onObject', 'INTERFACE', 8, 33),
280326
misplacedDirective(
281327
'onInputFieldDefinition',
282328
'ARGUMENT_DEFINITION',
283329
9,
284-
30,
330+
32,
285331
),
286-
misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 9, 63),
287-
misplacedDirective('onEnumValue', 'UNION', 12, 23),
288-
misplacedDirective('onScalar', 'ENUM', 14, 21),
289-
misplacedDirective('onUnion', 'ENUM_VALUE', 15, 20),
290-
misplacedDirective('onEnum', 'INPUT_OBJECT', 18, 23),
332+
misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 9, 65),
333+
misplacedDirective('onEnumValue', 'UNION', 12, 25),
334+
misplacedDirective('onScalar', 'ENUM', 14, 23),
335+
misplacedDirective('onUnion', 'ENUM_VALUE', 15, 22),
336+
misplacedDirective('onEnum', 'INPUT_OBJECT', 18, 25),
291337
misplacedDirective(
292338
'onArgumentDefinition',
293339
'INPUT_FIELD_DEFINITION',
294340
19,
295-
24,
341+
26,
296342
),
297-
misplacedDirective('onObject', 'SCHEMA', 22, 16),
298-
misplacedDirective('onObject', 'SCHEMA', 26, 23),
343+
misplacedDirective('onObject', 'SCHEMA', 22, 18),
344+
misplacedDirective('onObject', 'SCHEMA', 26, 25),
299345
]);
300346
});
301347
});

0 commit comments

Comments
 (0)