-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
schema-intermediate.ts
488 lines (454 loc) · 13.4 KB
/
schema-intermediate.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
import { AuthorizationType, GraphqlApi } from './graphqlapi';
import { IGraphqlApi } from './graphqlapi-base';
import { shapeAddition } from './private';
import { Resolver } from './resolver';
import { Directive, IField, IIntermediateType, AddFieldOptions } from './schema-base';
import { BaseTypeOptions, GraphqlType, ResolvableFieldOptions, ResolvableField } from './schema-field';
/**
* Properties for configuring an Intermediate Type
*
* @param definition - the variables and types that define this type
* i.e. { string: GraphqlType, string: GraphqlType }
* @param directives - the directives for this object type
*
*/
export interface IntermediateTypeOptions {
/**
* the attributes of this type
*/
readonly definition: { [key: string]: IField };
/**
* the directives for this object type
*
* @default - no directives
*/
readonly directives?: Directive[];
}
/**
* Interface Types are abstract types that includes a certain set of fields
* that other types must include if they implement the interface.
*
*/
export class InterfaceType implements IIntermediateType {
/**
* the name of this type
*/
public readonly name: string;
/**
* the attributes of this type
*/
public readonly definition: { [key: string]: IField };
/**
* the directives for this object type
*
* @default - no directives
*/
public readonly directives?: Directive[];
/**
* the authorization modes for this intermediate type
*/
protected modes?: AuthorizationType[];
public constructor(name: string, props: IntermediateTypeOptions) {
this.name = name;
this.definition = props.definition;
this.directives = props.directives;
}
/**
* Create a GraphQL Type representing this Intermediate Type
*
* @param options the options to configure this attribute
*/
public attribute(options?: BaseTypeOptions): GraphqlType {
return GraphqlType.intermediate({
isList: options?.isList,
isRequired: options?.isRequired,
isRequiredList: options?.isRequiredList,
intermediateType: this,
});
}
/**
* Generate the string of this object type
*/
public toString(): string {
return shapeAddition({
prefix: 'interface',
name: this.name,
directives: this.directives,
fields: Object.keys(this.definition).map((key) => {
const field = this.definition[key];
return `${key}${field.argsToString()}: ${field.toString()}${field.directivesToString(this.modes)}`;
}),
modes: this.modes,
});
}
/**
* Add a field to this Interface Type.
*
* Interface Types must have both fieldName and field options.
*
* @param options the options to add a field
*/
public addField(options: AddFieldOptions): void {
if (!options.fieldName || !options.field) {
throw new Error('Interface Types must have both fieldName and field options.');
}
this.definition[options.fieldName] = options.field;
}
/**
* Method called when the stringifying Intermediate Types for schema generation
*
* @internal
*/
public _bindToGraphqlApi(api: GraphqlApi): IIntermediateType {
this.modes = api.modes;
return this;
}
}
/**
* Properties for configuring an Object Type
*
* @param definition - the variables and types that define this type
* i.e. { string: GraphqlType, string: GraphqlType }
* @param interfaceTypes - the interfaces that this object type implements
* @param directives - the directives for this object type
*
*/
export interface ObjectTypeOptions extends IntermediateTypeOptions {
/**
* The Interface Types this Object Type implements
*
* @default - no interface types
*/
readonly interfaceTypes?: InterfaceType[];
}
/**
* Object Types are types declared by you.
*
*/
export class ObjectType extends InterfaceType implements IIntermediateType {
/**
* The Interface Types this Object Type implements
*
* @default - no interface types
*/
public readonly interfaceTypes?: InterfaceType[];
/**
* The resolvers linked to this data source
*/
public resolvers?: Resolver[];
public constructor(name: string, props: ObjectTypeOptions) {
const options = {
definition: props.interfaceTypes?.reduce((def, interfaceType) => {
return Object.assign({}, def, interfaceType.definition);
}, props.definition) ?? props.definition,
directives: props.directives,
};
super(name, options);
this.interfaceTypes = props.interfaceTypes;
this.resolvers = [];
}
/**
* Method called when the stringifying Intermediate Types for schema generation
*
* @internal
*/
public _bindToGraphqlApi(api: GraphqlApi): IIntermediateType {
this.modes = api.modes;
// If the resolvers have been generated, skip the bind
if (this.resolvers && this.resolvers.length > 0) {
return this;
}
Object.keys(this.definition).forEach((fieldName) => {
const field = this.definition[fieldName];
if (field instanceof ResolvableField) {
if (!this.resolvers) this.resolvers = [];
this.resolvers.push(this.generateResolver(api, fieldName, field.fieldOptions));
}
});
return this;
}
/**
* Add a field to this Object Type.
*
* Object Types must have both fieldName and field options.
*
* @param options the options to add a field
*/
public addField(options: AddFieldOptions): void {
if (!options.fieldName || !options.field) {
throw new Error('Object Types must have both fieldName and field options.');
}
this.definition[options.fieldName] = options.field;
}
/**
* Generate the string of this object type
*/
public toString(): string {
return shapeAddition({
prefix: 'type',
name: this.name,
interfaceTypes: this.interfaceTypes,
directives: this.directives,
fields: Object.keys(this.definition).map((key) => {
const field = this.definition[key];
return `${key}${field.argsToString()}: ${field.toString()}${field.directivesToString(this.modes)}`;
}),
modes: this.modes,
});
}
/**
* Generate the resolvers linked to this Object Type
*/
protected generateResolver(api: IGraphqlApi, fieldName: string, options?: ResolvableFieldOptions): Resolver {
return api.createResolver({
typeName: this.name,
fieldName: fieldName,
dataSource: options?.dataSource,
pipelineConfig: options?.pipelineConfig,
requestMappingTemplate: options?.requestMappingTemplate,
responseMappingTemplate: options?.responseMappingTemplate,
});
}
}
/**
* Input Types are abstract types that define complex objects.
* They are used in arguments to represent
*
*/
export class InputType implements IIntermediateType {
/**
* the name of this type
*/
public readonly name: string;
/**
* the attributes of this type
*/
public readonly definition: { [key: string]: IField };
/**
* the authorization modes for this intermediate type
*/
protected modes?: AuthorizationType[];
public constructor(name: string, props: IntermediateTypeOptions) {
this.name = name;
this.definition = props.definition;
}
/**
* Create a GraphQL Type representing this Input Type
*
* @param options the options to configure this attribute
*/
public attribute(options?: BaseTypeOptions): GraphqlType {
return GraphqlType.intermediate({
isList: options?.isList,
isRequired: options?.isRequired,
isRequiredList: options?.isRequiredList,
intermediateType: this,
});
}
/**
* Generate the string of this input type
*/
public toString(): string {
return shapeAddition({
prefix: 'input',
name: this.name,
fields: Object.keys(this.definition).map((key) =>
`${key}${this.definition[key].argsToString()}: ${this.definition[key].toString()}`),
modes: this.modes,
});
}
/**
* Add a field to this Input Type.
*
* Input Types must have both fieldName and field options.
*
* @param options the options to add a field
*/
public addField(options: AddFieldOptions): void {
if (!options.fieldName || !options.field) {
throw new Error('Input Types must have both fieldName and field options.');
}
this.definition[options.fieldName] = options.field;
}
/**
* Method called when the stringifying Intermediate Types for schema generation
*
* @internal
*/
public _bindToGraphqlApi(api: GraphqlApi): IIntermediateType {
this.modes = api.modes;
return this;
}
}
/**
* Properties for configuring an Union Type
*
*/
export interface UnionTypeOptions {
/**
* the object types for this union type
*/
readonly definition: IIntermediateType[];
}
/**
* Union Types are abstract types that are similar to Interface Types,
* but they cannot to specify any common fields between types.
*
* Note that fields of a union type need to be object types. In other words,
* you can't create a union type out of interfaces, other unions, or inputs.
*
*/
export class UnionType implements IIntermediateType {
/**
* the name of this type
*/
public readonly name: string;
/**
* the attributes of this type
*/
public readonly definition: { [key: string]: IField };
/**
* the authorization modes supported by this intermediate type
*/
protected modes?: AuthorizationType[];
public constructor(name: string, options: UnionTypeOptions) {
this.name = name;
this.definition = {};
options.definition.map((def) => this.addField({ field: def.attribute() }));
}
/**
* Create a GraphQL Type representing this Union Type
*
* @param options the options to configure this attribute
*/
public attribute(options?: BaseTypeOptions): GraphqlType {
return GraphqlType.intermediate({
isList: options?.isList,
isRequired: options?.isRequired,
isRequiredList: options?.isRequiredList,
intermediateType: this,
});
}
/**
* Generate the string of this Union type
*/
public toString(): string {
// Return a string that appends all Object Types for this Union Type
// i.e. 'union Example = example1 | example2'
return Object.values(this.definition).reduce((acc, field) =>
`${acc} ${field.toString()} |`, `union ${this.name} =`).slice(0, -2);
}
/**
* Add a field to this Union Type
*
* Input Types must have field options and the IField must be an Object Type.
*
* @param options the options to add a field
*/
public addField(options: AddFieldOptions): void {
if (options.fieldName) {
throw new Error('Union Types cannot be configured with the fieldName option. Use the field option instead.');
}
if (!options.field) {
throw new Error('Union Types must be configured with the field option.');
}
if (options.field && !(options.field.intermediateType instanceof ObjectType)) {
throw new Error('Fields for Union Types must be Object Types.');
}
this.definition[options.field?.toString() + 'id'] = options.field;
}
/**
* Method called when the stringifying Intermediate Types for schema generation
*
* @internal
*/
public _bindToGraphqlApi(api: GraphqlApi): IIntermediateType {
this.modes = api.modes;
return this;
}
}
/**
* Properties for configuring an Enum Type
*
*/
export interface EnumTypeOptions {
/**
* the attributes of this type
*/
readonly definition: string[];
}
/**
* Enum Types are abstract types that includes a set of fields
* that represent the strings this type can create.
*
*/
export class EnumType implements IIntermediateType {
/**
* the name of this type
*/
public readonly name: string;
/**
* the attributes of this type
*/
public readonly definition: { [key: string]: IField };
/**
* the authorization modes for this intermediate type
*/
protected modes?: AuthorizationType[];
public constructor(name: string, options: EnumTypeOptions) {
this.name = name;
this.definition = {};
options.definition.map((fieldName: string) => this.addField({ fieldName }));
}
/**
* Create an GraphQL Type representing this Enum Type
*/
public attribute(options?: BaseTypeOptions): GraphqlType {
return GraphqlType.intermediate({
isList: options?.isList,
isRequired: options?.isRequired,
isRequiredList: options?.isRequiredList,
intermediateType: this,
});
}
/**
* Generate the string of this enum type
*/
public toString(): string {
return shapeAddition({
prefix: 'enum',
name: this.name,
fields: Object.keys(this.definition),
modes: this.modes,
});
}
/**
* Add a field to this Enum Type
*
* To add a field to this Enum Type, you must only configure
* addField with the fieldName options.
*
* @param options the options to add a field
*/
public addField(options: AddFieldOptions): void {
if (options.field) {
throw new Error('Enum Type fields consist of strings. Use the fieldName option instead of the field option.');
}
if (!options.fieldName) {
throw new Error('When adding a field to an Enum Type, you must configure the fieldName option.');
}
if (options.fieldName.indexOf(' ') > -1) {
throw new Error(`Enum Type values cannot have whitespace. Received: ${options.fieldName}`);
}
this.definition[options.fieldName] = GraphqlType.string();
}
/**
* Method called when the stringifying Intermediate Types for schema generation
*
* @internal
*/
public _bindToGraphqlApi(api: GraphqlApi): IIntermediateType {
this.modes = api.modes;
return this;
}
}