forked from MichalLytek/type-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.ts
More file actions
485 lines (413 loc) · 15.8 KB
/
extensions.ts
File metadata and controls
485 lines (413 loc) · 15.8 KB
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
import "reflect-metadata";
import {
type GraphQLFieldMap,
type GraphQLInputObjectType,
GraphQLInterfaceType,
type GraphQLObjectType,
type GraphQLSchema,
} from "graphql";
import {
Arg,
Extensions,
Field,
FieldResolver,
InputType,
InterfaceType,
Mutation,
ObjectType,
Query,
Resolver,
buildSchema,
} from "type-graphql";
import { getMetadataStorage } from "@/metadata/getMetadataStorage";
describe("Extensions", () => {
let schema: GraphQLSchema;
describe("Schema", () => {
beforeAll(async () => {
getMetadataStorage().clear();
@InputType()
class ExtensionsOnFieldInput {
@Field()
@Extensions({ role: "admin" })
withExtensions!: string;
}
@InputType()
@Extensions({ roles: ["admin", "user"] })
class ExtensionsOnClassInput {
@Field()
regularField!: string;
}
@ObjectType()
@Extensions({ id: 1234 })
class ExtensionsOnClassObjectType {
@Field()
regularField!: string;
}
@ObjectType()
class SampleObjectType {
@Field()
@Extensions({ role: "user" })
withExtensions: string = "withExtensions";
@Field()
@Extensions({ first: "first value", second: "second value" })
withMultipleExtensions: string = "withMultipleExtensions";
@Field()
@Extensions({ first: "first value" })
@Extensions({ second: "second value", third: "third value" })
withMultipleExtensionsDecorators: string = "hello";
@Field()
@Extensions({ duplicate: "first value" })
@Extensions({ duplicate: "second value" })
withConflictingExtensionsKeys: string = "hello";
@Field()
withInput(@Arg("input") input: ExtensionsOnFieldInput): string {
return `hello${input.withExtensions}`;
}
@Field()
@Extensions({ other: "extension" })
withInputAndField(@Arg("input") input: ExtensionsOnFieldInput): string {
return `hello${input.withExtensions}`;
}
@Field()
withInputOnClass(@Arg("input") input: ExtensionsOnClassInput): string {
return `hello${input.regularField}`;
}
@Field()
@Extensions({ other: "extension" })
withInputAndFieldOnClass(@Arg("input") input: ExtensionsOnClassInput): string {
return `hello${input.regularField}`;
}
}
@InterfaceType()
@Extensions({ meta: "interfaceExtensionData" })
class SampleInterfaceType {
@Field()
@Extensions({ meta: "interfaceFieldExtensionData" })
withInterfaceFieldExtension!: string;
}
@ObjectType({
implements: [SampleInterfaceType],
})
class SampleObjectInterfaceImplementation extends SampleInterfaceType {}
@InterfaceType({
implements: [SampleInterfaceType],
})
class SampleInterfaceInterfaceImplementation extends SampleInterfaceType {}
@ObjectType({
implements: [SampleInterfaceInterfaceImplementation],
})
class SampleObjectInterfaceInterfaceImplementation extends SampleInterfaceInterfaceImplementation {}
@Resolver()
class SampleResolver {
@Query(() => SampleObjectType)
sampleObjectType(): SampleObjectType {
return new SampleObjectType();
}
@Query(() => ExtensionsOnClassObjectType)
extensionsOnClassObjectType(): ExtensionsOnClassObjectType {
return new ExtensionsOnClassObjectType();
}
@Query(() => SampleObjectInterfaceImplementation)
sampleObjectInterfaceImplementation(): SampleObjectInterfaceImplementation {
return new SampleObjectInterfaceImplementation();
}
@Query(() => SampleInterfaceInterfaceImplementation)
sampleInterfaceInterfaceImplementation(): SampleInterfaceInterfaceImplementation {
return new SampleInterfaceInterfaceImplementation();
}
@Query(() => SampleObjectInterfaceInterfaceImplementation)
sampleObjectInterfaceInterfaceImplementation(): SampleObjectInterfaceInterfaceImplementation {
return new SampleObjectInterfaceInterfaceImplementation();
}
@Query(() => SampleInterfaceType)
sampleInterfaceType(): SampleInterfaceType {
return new SampleObjectInterfaceImplementation();
}
@Query()
@Extensions({ mandatory: true })
queryWithExtensions(): string {
return "queryWithExtensions";
}
@Query()
@Extensions({ first: "first query value", second: "second query value" })
queryWithMultipleExtensions(): string {
return "hello";
}
@Query()
@Extensions({ first: "first query value" })
@Extensions({ second: "second query value", third: "third query value" })
queryWithMultipleExtensionsDecorators(): string {
return "hello";
}
@Mutation()
@Extensions({ mandatory: false })
mutationWithExtensions(): string {
return "mutationWithExtensions";
}
@Mutation()
@Extensions({ first: "first mutation value", second: "second mutation value" })
mutationWithMultipleExtensions(): string {
return "mutationWithMultipleExtensions";
}
@Mutation()
@Extensions({ first: "first mutation value" })
@Extensions({ second: "second mutation value", third: "third mutation value" })
mutationWithMultipleExtensionsDecorators(): string {
return "mutationWithMultipleExtensionsDecorators";
}
}
@Resolver(() => SampleObjectType)
class SampleObjectTypeResolver {
@FieldResolver()
@Extensions({ some: "extension" })
fieldResolverWithExtensions(): string {
return "hello";
}
}
schema = await buildSchema({
resolvers: [SampleResolver, SampleObjectTypeResolver],
});
});
it("should generate schema without errors", async () => {
expect(schema).toBeDefined();
});
describe("Fields", () => {
let fields: GraphQLFieldMap<any, any>;
beforeAll(async () => {
fields = (schema.getType("SampleObjectType") as GraphQLObjectType).getFields();
});
it("should add simple extensions to object fields", async () => {
expect(fields.withExtensions.extensions).toEqual({ role: "user" });
});
it("should add extensions with multiple properties to object fields", async () => {
expect(fields.withMultipleExtensions.extensions).toEqual({
first: "first value",
second: "second value",
});
});
it("should allow multiple extensions decorators for object fields", async () => {
expect(fields.withMultipleExtensionsDecorators.extensions).toEqual({
first: "first value",
second: "second value",
third: "third value",
});
});
it("should override extensions values when duplicate keys are provided", async () => {
expect(fields.withConflictingExtensionsKeys.extensions).toEqual({
duplicate: "second value",
});
});
});
describe("Query", () => {
it("should add simple extensions to query types", async () => {
const { queryWithExtensions } = schema.getQueryType()!.getFields();
expect(queryWithExtensions.extensions).toEqual({ mandatory: true });
});
it("should add extensions with multiple properties to query types", async () => {
const { queryWithMultipleExtensions } = schema.getQueryType()!.getFields();
expect(queryWithMultipleExtensions.extensions).toEqual({
first: "first query value",
second: "second query value",
});
});
it("should allow multiple extensions decorators for query types", async () => {
const { queryWithMultipleExtensionsDecorators } = schema.getQueryType()!.getFields();
expect(queryWithMultipleExtensionsDecorators.extensions).toEqual({
first: "first query value",
second: "second query value",
third: "third query value",
});
});
});
describe("Mutation", () => {
it("should add simple extensions to mutation types", async () => {
const { mutationWithExtensions } = schema.getMutationType()!.getFields();
expect(mutationWithExtensions.extensions).toEqual({ mandatory: false });
});
it("should add extensions with multiple properties to mutation types", async () => {
const { mutationWithMultipleExtensions } = schema.getMutationType()!.getFields();
expect(mutationWithMultipleExtensions.extensions).toEqual({
first: "first mutation value",
second: "second mutation value",
});
});
it("should allow multiple extensions decorators for mutation types", async () => {
const { mutationWithMultipleExtensionsDecorators } = schema.getMutationType()!.getFields();
expect(mutationWithMultipleExtensionsDecorators.extensions).toEqual({
first: "first mutation value",
second: "second mutation value",
third: "third mutation value",
});
});
});
describe("ObjectType", () => {
it("should add extensions to object types", async () => {
const objectType = schema.getType("ExtensionsOnClassObjectType") as GraphQLObjectType;
expect(objectType.extensions).toEqual({ id: 1234 });
});
});
describe("InputType", () => {
it("should add extensions to input types", async () => {
const inputType = schema.getType("ExtensionsOnClassInput") as GraphQLInputObjectType;
expect(inputType.extensions).toEqual({ roles: ["admin", "user"] });
});
it("should add extensions to input type fields", async () => {
const fields = (
schema.getType("ExtensionsOnFieldInput") as GraphQLInputObjectType
).getFields();
expect(fields.withExtensions.extensions).toEqual({ role: "admin" });
});
});
describe("FieldResolver", () => {
it("should add extensions to field resolvers", async () => {
const fields = (schema.getType("SampleObjectType") as GraphQLObjectType).getFields();
expect(fields.fieldResolverWithExtensions.extensions).toEqual({ some: "extension" });
});
});
describe("Interface Fields", () => {
it("should add extensions to interface types", async () => {
const fields = (schema.getType("SampleInterfaceType") as GraphQLObjectType).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interfaceFieldExtensionData",
});
});
it("should add extensions to ObjectType which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleObjectInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interfaceFieldExtensionData",
});
});
it("should add extensions to Interface which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleInterfaceInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interfaceFieldExtensionData",
});
});
it("should add extensions to ObjectType which extends InterfaceType which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleInterfaceInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interfaceFieldExtensionData",
});
});
});
describe("Interface Class", () => {
it("should add extensions to interface types", async () => {
const sampleInterface = schema.getType("SampleInterfaceType") as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interfaceExtensionData",
});
});
it("should add extensions to ObjectType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleObjectInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interfaceExtensionData",
});
});
it("should add extensions to InterfaceType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleInterfaceInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interfaceExtensionData",
});
});
it("should add extensions to ObjectType which extends InterfaceType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleObjectInterfaceInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interfaceExtensionData",
});
});
});
describe("Inheritance", () => {
beforeAll(async () => {
getMetadataStorage().clear();
@ObjectType()
@Extensions({ parentClass: true })
class Parent {
@Field()
@Extensions({ parentField: true })
parentField!: string;
}
@Extensions({ childClass: true })
@ObjectType()
class Child extends Parent {
@Field()
@Extensions({ childField: true })
childField!: string;
}
@Resolver()
class SampleResolver {
@Query()
sampleQuery(): Child {
return {} as Child;
}
}
schema = await buildSchema({
resolvers: [SampleResolver],
orphanedTypes: [Parent],
});
});
it("should inherit object type extensions from parent object type class", () => {
const childObjectType = schema.getType("Child") as GraphQLObjectType;
expect(childObjectType.extensions).toEqual({
parentClass: true,
childClass: true,
});
});
it("should not get object type extensions from child object type class", () => {
const parentObjectType = schema.getType("Parent") as GraphQLObjectType;
expect(parentObjectType.extensions).toEqual({
parentClass: true,
});
});
it("should inherit object type field extensions from parent object type field", () => {
const childObjectType = schema.getType("Child") as GraphQLObjectType;
const childObjectTypeParentField = childObjectType.getFields().parentField;
expect(childObjectTypeParentField.extensions).toEqual({ parentField: true });
});
});
describe("Fields with field resolvers", () => {
beforeAll(async () => {
getMetadataStorage().clear();
@ObjectType()
class Child {
@Field()
@Extensions({ childField: true })
childField!: string;
}
@Resolver(() => Child)
class ChildResolver {
@Query()
sampleQuery(): Child {
return {} as Child;
}
@Extensions({ childFieldResolver: true })
@FieldResolver()
childField(): string {
return "childField";
}
}
schema = await buildSchema({
resolvers: [ChildResolver],
});
});
it("should merge field level with field resolver level extensions", () => {
const childObjectType = schema.getType("Child") as GraphQLObjectType;
expect(childObjectType.getFields().childField.extensions).toEqual({
childField: true,
childFieldResolver: true,
});
});
});
});
});