9
9
*/
10
10
11
11
import invariant from '../jsutils/invariant' ;
12
+ import isNullish from '../jsutils/isNullish' ;
12
13
import { ENUM } from '../language/kinds' ;
13
14
import type {
14
15
OperationDefinition ,
@@ -296,6 +297,12 @@ export class GraphQLObjectType {
296
297
invariant ( config . name , 'Type must be named.' ) ;
297
298
this . name = config . name ;
298
299
this . description = config . description ;
300
+ if ( config . isTypeOf ) {
301
+ invariant (
302
+ typeof config . isTypeOf === 'function' ,
303
+ `${ this } must provide "isTypeOf" as a function.`
304
+ ) ;
305
+ }
299
306
this . isTypeOf = config . isTypeOf ;
300
307
this . _typeConfig = config ;
301
308
addImplementationToInterfaces ( this ) ;
@@ -359,7 +366,7 @@ function defineFieldMap(
359
366
) : GraphQLFieldDefinitionMap {
360
367
var fieldMap : any = resolveMaybeThunk ( fields ) ;
361
368
invariant (
362
- typeof fieldMap === 'object' && ! Array . isArray ( fieldMap ) ,
369
+ isPlainObj ( fieldMap ) ,
363
370
`${ type } fields must be an object with field names as keys or a ` +
364
371
`function which returns such an object.`
365
372
) ;
@@ -381,7 +388,7 @@ function defineFieldMap(
381
388
field . args = [ ] ;
382
389
} else {
383
390
invariant (
384
- typeof field . args === 'object' && ! Array . isArray ( field . args ) ,
391
+ isPlainObj ( field . args ) ,
385
392
`${ type } .${ fieldName } args must be an object with argument names ` +
386
393
`as keys.`
387
394
) ;
@@ -404,6 +411,10 @@ function defineFieldMap(
404
411
return fieldMap ;
405
412
}
406
413
414
+ function isPlainObj ( obj ) {
415
+ return obj && typeof obj === 'object' && ! Array . isArray ( obj ) ;
416
+ }
417
+
407
418
/**
408
419
* Update the interfaces to know about this implementation.
409
420
* This is an rare and unfortunate use of mutation in the type definition
@@ -521,6 +532,12 @@ export class GraphQLInterfaceType {
521
532
invariant ( config . name , 'Type must be named.' ) ;
522
533
this . name = config . name ;
523
534
this . description = config . description ;
535
+ if ( config . resolveType ) {
536
+ invariant (
537
+ typeof config . resolveType === 'function' ,
538
+ `${ this } must provide "resolveType" as a function.`
539
+ ) ;
540
+ }
524
541
this . resolveType = config . resolveType ;
525
542
this . _typeConfig = config ;
526
543
this . _implementations = [ ] ;
@@ -621,6 +638,12 @@ export class GraphQLUnionType {
621
638
invariant ( config . name , 'Type must be named.' ) ;
622
639
this . name = config . name ;
623
640
this . description = config . description ;
641
+ if ( config . resolveType ) {
642
+ invariant (
643
+ typeof config . resolveType === 'function' ,
644
+ `${ this } must provide "resolveType" as a function.`
645
+ ) ;
646
+ }
624
647
this . resolveType = config . resolveType ;
625
648
invariant (
626
649
Array . isArray ( config . types ) && config . types . length > 0 ,
@@ -711,18 +734,19 @@ export class GraphQLEnumType/* <T> */ {
711
734
description : ?string ;
712
735
713
736
_enumConfig : GraphQLEnumTypeConfig /* <T> */ ;
714
- _values : GraphQLEnumValueDefinitionMap /* <T> */ ;
737
+ _values : Array < GraphQLEnumValueDefinition /* <T> */ > ;
715
738
_valueLookup : Map < any /* T */ , GraphQLEnumValueDefinition > ;
716
- _nameLookup : Map < string , GraphQLEnumValueDefinition > ;
739
+ _nameLookup : { [ valueName : string ] : GraphQLEnumValueDefinition } ;
717
740
718
741
constructor ( config : GraphQLEnumTypeConfig /* <T> */ ) {
719
742
this . name = config . name ;
720
743
this . description = config . description ;
744
+ this . _values = defineEnumValues ( this , config . values ) ;
721
745
this . _enumConfig = config ;
722
746
}
723
747
724
- getValues ( ) : GraphQLEnumValueDefinitionMap /* <T> */ {
725
- return this . _values || ( this . _values = this . _defineValueMap ( ) ) ;
748
+ getValues ( ) : Array < GraphQLEnumValueDefinition /* <T> */ > {
749
+ return this . _values ;
726
750
}
727
751
728
752
serialize ( value : any /* T */ ) : ?string {
@@ -731,53 +755,37 @@ export class GraphQLEnumType/* <T> */ {
731
755
}
732
756
733
757
parseValue ( value : any ) : ?any / * T * / {
734
- var enumValue = this . _getNameLookup ( ) . get ( value ) ;
758
+ var enumValue = this . _getNameLookup ( ) [ value ] ;
735
759
if ( enumValue ) {
736
760
return enumValue . value ;
737
761
}
738
762
}
739
763
740
764
parseLiteral ( valueAST : Value ) : ?any / * T * / {
741
765
if ( valueAST . kind === ENUM ) {
742
- var enumValue = this . _getNameLookup ( ) . get ( valueAST . value ) ;
766
+ var enumValue = this . _getNameLookup ( ) [ valueAST . value ] ;
743
767
if ( enumValue ) {
744
768
return enumValue . value ;
745
769
}
746
770
}
747
771
}
748
772
749
- _defineValueMap ( ) : GraphQLEnumValueDefinitionMap / * < T > */ {
750
- var valueMap = ( this . _enumConfig . values : any ) ;
751
- Object . keys ( valueMap ) . forEach ( valueName => {
752
- var value = valueMap [ valueName ] ;
753
- value . name = valueName ;
754
- if ( ! value . hasOwnProperty ( 'value' ) ) {
755
- value . value = valueName ;
756
- }
757
- } ) ;
758
- return valueMap ;
759
- }
760
-
761
773
_getValueLookup ( ) : Map < any /* T */ , GraphQLEnumValueDefinition > {
762
774
if ( ! this . _valueLookup ) {
763
775
var lookup = new Map ( ) ;
764
- var values = this . getValues ( ) ;
765
- Object . keys ( values ) . forEach ( valueName => {
766
- var value = values [ valueName ] ;
776
+ this . getValues ( ) . forEach ( value => {
767
777
lookup . set ( value . value , value ) ;
768
778
} ) ;
769
779
this . _valueLookup = lookup ;
770
780
}
771
781
return this . _valueLookup ;
772
782
}
773
783
774
- _getNameLookup ( ) : Map < string , GraphQLEnumValueDefinition > {
784
+ _getNameLookup ( ) : { [ valueName : string ] : GraphQLEnumValueDefinition } {
775
785
if ( ! this . _nameLookup ) {
776
- var lookup = new Map ( ) ;
777
- var values = this . getValues ( ) ;
778
- Object . keys ( values ) . forEach ( valueName => {
779
- var value = values [ valueName ] ;
780
- lookup . set ( value . name , value ) ;
786
+ var lookup = Object . create ( null ) ;
787
+ this . getValues ( ) . forEach ( value => {
788
+ lookup [ value . name ] = value ;
781
789
} ) ;
782
790
this . _nameLookup = lookup ;
783
791
}
@@ -789,6 +797,34 @@ export class GraphQLEnumType/* <T> */ {
789
797
}
790
798
}
791
799
800
+ function defineEnumValues (
801
+ type : GraphQLEnumType ,
802
+ valueMap : GraphQLEnumValueConfigMap /* <T> */
803
+ ) : Array < GraphQLEnumValueDefinition /* <T> */ > {
804
+ invariant (
805
+ isPlainObj ( valueMap ) ,
806
+ `${ type } values must be an object with value names as keys.`
807
+ ) ;
808
+ var valueNames = Object . keys ( valueMap ) ;
809
+ invariant (
810
+ valueNames . length > 0 ,
811
+ `${ type } values must be an object with value names as keys.`
812
+ ) ;
813
+ return valueNames . map ( valueName => {
814
+ var value = valueMap [ valueName ] ;
815
+ invariant (
816
+ isPlainObj ( value ) ,
817
+ `${ type } .${ valueName } must refer to an object with a "value" key ` +
818
+ `representing an internal value but got: ${ value } .`
819
+ ) ;
820
+ value . name = valueName ;
821
+ if ( isNullish ( value . value ) ) {
822
+ value . value = valueName ;
823
+ }
824
+ return value ;
825
+ } ) ;
826
+ }
827
+
792
828
export type GraphQLEnumTypeConfig /* <T> */ = {
793
829
name : string ;
794
830
values : GraphQLEnumValueConfigMap /* <T> */ ;
@@ -805,10 +841,6 @@ export type GraphQLEnumValueConfig/* <T> */ = {
805
841
description ?: ?string ;
806
842
}
807
843
808
- export type GraphQLEnumValueDefinitionMap /* <T> */ = {
809
- [ valueName : string ] : GraphQLEnumValueDefinition /* <T> */ ;
810
- } ;
811
-
812
844
export type GraphQLEnumValueDefinition /* <T> */ = {
813
845
name : string ;
814
846
value ?: any /* T */ ;
@@ -859,7 +891,7 @@ export class GraphQLInputObjectType {
859
891
_defineFieldMap ( ) : InputObjectFieldMap {
860
892
var fieldMap : any = resolveMaybeThunk ( this . _typeConfig . fields ) ;
861
893
invariant (
862
- typeof fieldMap === 'object' && ! Array . isArray ( fieldMap ) ,
894
+ isPlainObj ( fieldMap ) ,
863
895
`${ this } fields must be an object with field names as keys or a ` +
864
896
`function which returns such an object.`
865
897
) ;
0 commit comments