@@ -13,7 +13,7 @@ import { BaseTypeOptions, GraphqlType, ResolvableFieldOptions } from './schema-f
13
13
*
14
14
* @experimental
15
15
*/
16
- export interface IntermediateTypeProps {
16
+ export interface IntermediateTypeOptions {
17
17
/**
18
18
* the attributes of this type
19
19
*/
@@ -52,22 +52,12 @@ export class InterfaceType implements IIntermediateType {
52
52
*/
53
53
protected modes ?: AuthorizationType [ ] ;
54
54
55
- public constructor ( name : string , props : IntermediateTypeProps ) {
55
+ public constructor ( name : string , props : IntermediateTypeOptions ) {
56
56
this . name = name ;
57
57
this . definition = props . definition ;
58
58
this . directives = props . directives ;
59
59
}
60
60
61
- /**
62
- * Method called when the stringifying Intermediate Types for schema generation
63
- *
64
- * @internal
65
- */
66
- public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
67
- this . modes = api . modes ;
68
- return this ;
69
- }
70
-
71
61
/**
72
62
* Create a GraphQL Type representing this Intermediate Type
73
63
*
@@ -111,6 +101,16 @@ export class InterfaceType implements IIntermediateType {
111
101
}
112
102
this . definition [ options . fieldName ] = options . field ;
113
103
}
104
+
105
+ /**
106
+ * Method called when the stringifying Intermediate Types for schema generation
107
+ *
108
+ * @internal
109
+ */
110
+ public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
111
+ this . modes = api . modes ;
112
+ return this ;
113
+ }
114
114
}
115
115
116
116
/**
@@ -123,7 +123,7 @@ export class InterfaceType implements IIntermediateType {
123
123
*
124
124
* @experimental
125
125
*/
126
- export interface ObjectTypeProps extends IntermediateTypeProps {
126
+ export interface ObjectTypeOptions extends IntermediateTypeOptions {
127
127
/**
128
128
* The Interface Types this Object Type implements
129
129
*
@@ -149,7 +149,7 @@ export class ObjectType extends InterfaceType implements IIntermediateType {
149
149
*/
150
150
public resolvers ?: Resolver [ ] ;
151
151
152
- public constructor ( name : string , props : ObjectTypeProps ) {
152
+ public constructor ( name : string , props : ObjectTypeOptions ) {
153
153
const options = {
154
154
definition : props . interfaceTypes ?. reduce ( ( def , interfaceType ) => {
155
155
return Object . assign ( { } , def , interfaceType . definition ) ;
@@ -234,7 +234,7 @@ export class InputType implements IIntermediateType {
234
234
*/
235
235
protected modes ?: AuthorizationType [ ] ;
236
236
237
- public constructor ( name : string , props : IntermediateTypeProps ) {
237
+ public constructor ( name : string , props : IntermediateTypeOptions ) {
238
238
this . name = name ;
239
239
this . definition = props . definition ;
240
240
}
@@ -253,16 +253,6 @@ export class InputType implements IIntermediateType {
253
253
} ) ;
254
254
}
255
255
256
- /**
257
- * Method called when the stringifying Intermediate Types for schema generation
258
- *
259
- * @internal
260
- */
261
- public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
262
- this . modes = api . modes ;
263
- return this ;
264
- }
265
-
266
256
/**
267
257
* Generate the string of this input type
268
258
*/
@@ -289,13 +279,21 @@ export class InputType implements IIntermediateType {
289
279
}
290
280
this . definition [ options . fieldName ] = options . field ;
291
281
}
282
+
283
+ /**
284
+ * Method called when the stringifying Intermediate Types for schema generation
285
+ *
286
+ * @internal
287
+ */
288
+ public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
289
+ this . modes = api . modes ;
290
+ return this ;
291
+ }
292
292
}
293
293
294
294
/**
295
295
* Properties for configuring an Union Type
296
296
*
297
- * @param definition - the object types for this union type
298
- *
299
297
* @experimental
300
298
*/
301
299
export interface UnionTypeOptions {
@@ -348,16 +346,6 @@ export class UnionType implements IIntermediateType {
348
346
} ) ;
349
347
}
350
348
351
- /**
352
- * Method called when the stringifying Intermediate Types for schema generation
353
- *
354
- * @internal
355
- */
356
- public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
357
- this . modes = api . modes ;
358
- return this ;
359
- }
360
-
361
349
/**
362
350
* Generate the string of this Union type
363
351
*/
@@ -387,4 +375,108 @@ export class UnionType implements IIntermediateType {
387
375
}
388
376
this . definition [ options . field ?. toString ( ) + 'id' ] = options . field ;
389
377
}
378
+
379
+ /**
380
+ * Method called when the stringifying Intermediate Types for schema generation
381
+ *
382
+ * @internal
383
+ */
384
+ public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
385
+ this . modes = api . modes ;
386
+ return this ;
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Properties for configuring an Enum Type
392
+ *
393
+ * @experimental
394
+ */
395
+ export interface EnumTypeOptions {
396
+ /**
397
+ * the attributes of this type
398
+ */
399
+ readonly definition : string [ ] ;
400
+ }
401
+
402
+ /**
403
+ * Enum Types are abstract types that includes a set of fields
404
+ * that represent the strings this type can create.
405
+ *
406
+ * @experimental
407
+ */
408
+ export class EnumType implements IIntermediateType {
409
+ /**
410
+ * the name of this type
411
+ */
412
+ public readonly name : string ;
413
+ /**
414
+ * the attributes of this type
415
+ */
416
+ public readonly definition : { [ key : string ] : IField } ;
417
+ /**
418
+ * the authorization modes for this intermediate type
419
+ */
420
+ protected modes ?: AuthorizationType [ ] ;
421
+
422
+ public constructor ( name : string , options : EnumTypeOptions ) {
423
+ this . name = name ;
424
+ this . definition = { } ;
425
+ options . definition . map ( ( fieldName : string ) => this . addField ( { fieldName } ) ) ;
426
+ }
427
+
428
+ /**
429
+ * Create an GraphQL Type representing this Enum Type
430
+ */
431
+ public attribute ( options ?: BaseTypeOptions ) : GraphqlType {
432
+ return GraphqlType . intermediate ( {
433
+ isList : options ?. isList ,
434
+ isRequired : options ?. isRequired ,
435
+ isRequiredList : options ?. isRequiredList ,
436
+ intermediateType : this ,
437
+ } ) ;
438
+ }
439
+
440
+ /**
441
+ * Generate the string of this enum type
442
+ */
443
+ public toString ( ) : string {
444
+ return shapeAddition ( {
445
+ prefix : 'enum' ,
446
+ name : this . name ,
447
+ fields : Object . keys ( this . definition ) ,
448
+ modes : this . modes ,
449
+ } ) ;
450
+ }
451
+
452
+ /**
453
+ * Add a field to this Enum Type
454
+ *
455
+ * To add a field to this Enum Type, you must only configure
456
+ * addField with the fieldName options.
457
+ *
458
+ * @param options the options to add a field
459
+ */
460
+ public addField ( options : AddFieldOptions ) : void {
461
+ if ( options . field ) {
462
+ throw new Error ( 'Enum Type fields consist of strings. Use the fieldName option instead of the field option.' ) ;
463
+ }
464
+ if ( ! options . fieldName ) {
465
+ throw new Error ( 'When adding a field to an Enum Type, you must configure the fieldName option.' ) ;
466
+ }
467
+ if ( options . fieldName . indexOf ( ' ' ) > - 1 ) {
468
+ throw new Error ( `Enum Type values cannot have whitespace. Received: ${ options . fieldName } ` ) ;
469
+ }
470
+ this . definition [ options . fieldName ] = GraphqlType . string ( ) ;
471
+ }
472
+
473
+ /**
474
+ * Method called when the stringifying Intermediate Types for schema generation
475
+ *
476
+ * @internal
477
+ */
478
+ public _bindToGraphqlApi ( api : GraphqlApi ) : IIntermediateType {
479
+ this . modes = api . modes ;
480
+ return this ;
481
+ }
390
482
}
0 commit comments