-
Notifications
You must be signed in to change notification settings - Fork 468
/
buildSchemaFromSDL.test.ts
554 lines (480 loc) · 13.3 KB
/
buildSchemaFromSDL.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import gql from "graphql-tag";
import { buildSchemaFromSDL } from "../buildSchemaFromSDL";
import {
GraphQLSchema,
GraphQLDirective,
DirectiveLocation,
GraphQLObjectType,
GraphQLAbstractType,
GraphQLScalarType,
GraphQLScalarTypeConfig,
GraphQLEnumType,
Kind,
execute,
ExecutionResult
} from "graphql";
import astSerializer from "./snapshotSerializers/astSerializer";
import graphQLTypeSerializer from "./snapshotSerializers/graphQLTypeSerializer";
import selectionSetSerializer from "./snapshotSerializers/selectionSetSerializer";
expect.addSnapshotSerializer(astSerializer);
expect.addSnapshotSerializer(graphQLTypeSerializer);
expect.addSnapshotSerializer(selectionSetSerializer);
describe("buildSchemaFromSDL", () => {
describe(`type definitions`, () => {
it(`should construct types from definitions`, () => {
const schema = buildSchemaFromSDL(
gql`
type User {
name: String
}
type Post {
title: String
}
`
);
expect(schema.getType("User")).toMatchInlineSnapshot(`
type User {
name: String
}
`);
expect(schema.getType("Post")).toMatchInlineSnapshot(`
type Post {
title: String
}
`);
});
it(`should not allow multiple type definitions with the same name`, () => {
expect(() =>
buildSchemaFromSDL(
gql`
type User {
name: String
}
type User {
title: String
}
`
)
).toThrowErrorMatchingInlineSnapshot(
`"There can be only one type named \\"User\\"."`
);
});
});
describe(`type extension`, () => {
it(`should allow extending a type defined in the same document`, () => {
const schema = buildSchemaFromSDL(
gql`
type User {
name: String
}
extend type User {
email: String
}
`
);
expect(schema.getType("User")).toMatchInlineSnapshot(`
type User {
name: String
email: String
}
`);
});
it(`should allow extending a non-existent type`, () => {
const schema = buildSchemaFromSDL(
gql`
extend type User {
email: String
}
`
);
expect(schema.getType("User")).toMatchInlineSnapshot(`
type User {
email: String
}
`);
});
it.skip(`should report an error when extending a non-existent type`, () => {
expect(() =>
buildSchemaFromSDL(
gql`
extend type User {
email: String
}
`
)
).toThrowErrorMatchingInlineSnapshot(
`"Cannot extend type \\"User\\" because it is not defined."`
);
});
});
describe(`root operation types`, () => {
it(`should include a root type with a default type name`, () => {
const schema = buildSchemaFromSDL(
gql`
type Query {
rootField: String
}
`
);
expect(schema.getType("Query")).toMatchInlineSnapshot(`
type Query {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("Query"));
});
it(`should include a root type with a non-default type name`, () => {
const schema = buildSchemaFromSDL(
gql`
schema {
query: Query
}
type Query {
rootField: String
}
`
);
expect(schema.getType("Query")).toMatchInlineSnapshot(`
type Query {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("Query"));
});
it(`should include a root type with a non-default type name specified in a schema extension`, () => {
const schema = buildSchemaFromSDL(
gql`
extend schema {
query: Query
}
type Query {
rootField: String
}
`
);
expect(schema.getType("Query")).toMatchInlineSnapshot(`
type Query {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("Query"));
});
describe(`extending root operation types that aren't defined elsewhere`, () => {
it(`should be allowed`, () => {
const schema = buildSchemaFromSDL(
gql`
extend type Query {
rootField: String
}
`
);
expect(schema.getType("Query")).toMatchInlineSnapshot(`
type Query {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("Query"));
});
it(`should be allowed with a non-default type name`, () => {
const schema = buildSchemaFromSDL(
gql`
schema {
query: QueryRoot
}
extend type QueryRoot {
rootField: String
}
`
);
expect(schema.getType("QueryRoot")).toMatchInlineSnapshot(`
type QueryRoot {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("QueryRoot"));
});
it(`should be allowed with a non-default name specified in a schema extension`, () => {
const schema = buildSchemaFromSDL(
gql`
schema {
query: QueryRoot
}
type QueryRoot {
rootField: String
}
extend schema {
mutation: MutationRoot
}
extend type MutationRoot {
rootField: String
}
`
);
expect(schema.getType("MutationRoot")).toMatchInlineSnapshot(`
type MutationRoot {
rootField: String
}
`);
expect(schema.getQueryType()).toEqual(schema.getType("QueryRoot"));
expect(schema.getMutationType()).toEqual(
schema.getType("MutationRoot")
);
});
});
});
describe(`directives`, () => {
it(`should construct directives from definitions`, () => {
const schema = buildSchemaFromSDL(
gql`
directive @something on FIELD_DEFINITION
directive @another on FIELD_DEFINITION
`
);
expect(schema.getDirective("something")).toMatchInlineSnapshot(
`"@something"`
);
expect(schema.getDirective("another")).toMatchInlineSnapshot(
`"@another"`
);
});
it(`should not allow multiple directive definitions with the same name`, () => {
expect(() =>
buildSchemaFromSDL(
gql`
directive @something on FIELD_DEFINITION
directive @something on FIELD_DEFINITION
`
)
).toThrowErrorMatchingInlineSnapshot(
`"There can be only one directive named \\"@something\\"."`
);
});
it(`should not allow a directive definition with the same name as a predefined schema directive`, () => {
expect(() =>
buildSchemaFromSDL(
gql`
directive @something on FIELD_DEFINITION
`,
new GraphQLSchema({
query: undefined,
directives: [
new GraphQLDirective({
name: "something",
locations: [DirectiveLocation.FIELD_DEFINITION]
})
]
})
)
).toThrowErrorMatchingInlineSnapshot(
`"Directive \\"@something\\" already exists in the schema. It cannot be redefined."`
);
});
it(`should allow predefined schema directives to be used`, () => {
const schema = buildSchemaFromSDL(
gql`
type User {
name: String @something
}
`,
new GraphQLSchema({
query: undefined,
directives: [
new GraphQLDirective({
name: "something",
locations: [DirectiveLocation.FIELD_DEFINITION]
})
]
})
);
});
it(`should allow schema directives to be used in the same document they are defined in`, () => {
const schema = buildSchemaFromSDL(
gql`
directive @something on FIELD_DEFINITION
type User {
name: String @something
}
`
);
});
it(`should report an error for unknown schema directives`, () => {
expect(() =>
buildSchemaFromSDL(
gql`
type User {
name: String @something
}
`
)
).toThrowErrorMatchingInlineSnapshot(
`"Unknown directive \\"@something\\"."`
);
});
});
describe(`resolvers`, () => {
it(`should add a resolver for a field`, () => {
const name = () => {};
const schema = buildSchemaFromSDL([
{
typeDefs: gql`
type User {
name: String
}
`,
resolvers: {
User: {
name
}
}
}
]);
const userType = schema.getType("User");
expect(userType).toBeDefined();
const nameField = (userType! as GraphQLObjectType).getFields()["name"];
expect(nameField).toBeDefined();
expect(nameField.resolve).toEqual(name);
});
it(`should add meta fields to abstract types`, () => {
const typeDefs = gql`
union Animal = Dog
interface Creature {
name: String!
legs: Int!
}
type Dog {
id: ID!
}
type Cat implements Creature {
meow: Boolean!
}
`;
const resolveTypeUnion = (obj: any) => {
return "Dog";
};
const resolveTypeInterface = (obj: any) => {
if (obj.meow) {
return "Cat";
}
throw Error("Couldn't resolve interface");
};
const resolvers = {
Animal: {
__resolveType: resolveTypeUnion
},
Creature: {
__resolveType: resolveTypeInterface
}
};
const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const animalUnion = schema.getType("Animal") as GraphQLAbstractType;
const creatureInterface = schema.getType(
"Creature"
) as GraphQLAbstractType;
expect(animalUnion.resolveType).toBe(resolveTypeUnion);
expect(creatureInterface.resolveType).toBe(resolveTypeInterface);
});
it(`should add resolvers for scalar types`, () => {
const typeDefs = gql`
scalar Custom
`;
const customTypeConfig: GraphQLScalarTypeConfig<string, string> = {
name: "Custom",
serialize: value => value,
parseValue: value => value,
parseLiteral: input => {
if (input.kind !== Kind.STRING) {
throw new Error("Expected value to be string");
}
return input.value;
}
};
const CustomType = new GraphQLScalarType(customTypeConfig);
const resolvers = { Custom: CustomType };
const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const custom = schema.getType("Custom") as GraphQLScalarType;
expect(custom.parseLiteral).toBe(CustomType.parseLiteral);
expect(custom.parseValue).toBe(CustomType.parseValue);
expect(custom.serialize).toBe(CustomType.serialize);
});
it(`should add resolvers to enum types`, () => {
const typeDefs = gql`
enum AllowedColor {
RED
GREEN
BLUE
}
type Query {
favoriteColor: AllowedColor
avatar(borderColor: AllowedColor): String
}
`;
const mockResolver = jest.fn();
const resolvers = {
AllowedColor: {
RED: "#f00",
GREEN: "#0f0",
BLUE: "#00f"
},
Query: {
favoriteColor: () => "#f00",
avatar: (_: any, params: any) => mockResolver(_, params)
}
};
const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const colorEnum = schema.getType("AllowedColor") as GraphQLEnumType;
let result = execute(
schema,
gql`
query {
favoriteColor
avatar(borderColor: RED)
}
`
);
expect((result as ExecutionResult).data!.favoriteColor).toBe("RED");
expect(colorEnum.getValue("RED")!.value).toBe("#f00");
expect(mockResolver).toBeCalledWith(undefined, { borderColor: "#f00" });
});
it(`should add resolvers to enum types with 0 value`, () => {
const typeDefs = gql`
enum CustomerType {
EXISTING
NEW
}
type Query {
existingCustomer: CustomerType
newCustomer: CustomerType
}
`;
const resolvers = {
CustomerType: {
EXISTING: 0,
NEW: 1
},
Query: {
existingCustomer: () => 0,
newCustomer: () => 1
}
};
const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const customerTypeEnum = schema.getType(
"CustomerType"
) as GraphQLEnumType;
let result = execute(
schema,
gql`
query {
existingCustomer
newCustomer
}
`
);
expect((result as ExecutionResult).data!.existingCustomer).toBe(
"EXISTING"
);
expect(customerTypeEnum.getValue("EXISTING")!.value).toBe(0);
expect((result as ExecutionResult).data!.newCustomer).toBe("NEW");
expect(customerTypeEnum.getValue("NEW")!.value).toBe(1);
});
});
});