@@ -200,6 +200,7 @@ export class GraphQLScalarType/* <T> */ {
200
200
201
201
constructor ( config : GraphQLScalarTypeConfig /* <T> */ ) {
202
202
invariant ( config . name , 'Type must be named.' ) ;
203
+ assertValidName ( config . name ) ;
203
204
this . name = config . name ;
204
205
this . description = config . description ;
205
206
invariant (
@@ -296,6 +297,7 @@ export class GraphQLObjectType {
296
297
297
298
constructor ( config : GraphQLObjectTypeConfig ) {
298
299
invariant ( config . name , 'Type must be named.' ) ;
300
+ assertValidName ( config . name ) ;
299
301
this . name = config . name ;
300
302
this . description = config . description ;
301
303
if ( config . isTypeOf ) {
@@ -381,6 +383,7 @@ function defineFieldMap(
381
383
382
384
var resultFieldMap = { } ;
383
385
fieldNames . forEach ( fieldName => {
386
+ assertValidName ( fieldName ) ;
384
387
var field = {
385
388
...fieldMap [ fieldName ] ,
386
389
name : fieldName
@@ -399,6 +402,7 @@ function defineFieldMap(
399
402
`as keys.`
400
403
) ;
401
404
field . args = Object . keys ( field . args ) . map ( argName => {
405
+ assertValidName ( argName ) ;
402
406
var arg = field . args [ argName ] ;
403
407
invariant (
404
408
isInputType ( arg . type ) ,
@@ -537,6 +541,7 @@ export class GraphQLInterfaceType {
537
541
538
542
constructor ( config : GraphQLInterfaceTypeConfig ) {
539
543
invariant ( config . name , 'Type must be named.' ) ;
544
+ assertValidName ( config . name ) ;
540
545
this . name = config . name ;
541
546
this . description = config . description ;
542
547
if ( config . resolveType ) {
@@ -638,6 +643,7 @@ export class GraphQLUnionType {
638
643
639
644
constructor ( config : GraphQLUnionTypeConfig ) {
640
645
invariant ( config . name , 'Type must be named.' ) ;
646
+ assertValidName ( config . name ) ;
641
647
this . name = config . name ;
642
648
this . description = config . description ;
643
649
if ( config . resolveType ) {
@@ -742,6 +748,7 @@ export class GraphQLEnumType/* <T> */ {
742
748
743
749
constructor ( config : GraphQLEnumTypeConfig /* <T> */ ) {
744
750
this . name = config . name ;
751
+ assertValidName ( config . name ) ;
745
752
this . description = config . description ;
746
753
this . _values = defineEnumValues ( this , config . values ) ;
747
754
this . _enumConfig = config ;
@@ -813,6 +820,7 @@ function defineEnumValues(
813
820
`${ type } values must be an object with value names as keys.`
814
821
) ;
815
822
return valueNames . map ( valueName => {
823
+ assertValidName ( valueName ) ;
816
824
var value = valueMap [ valueName ] ;
817
825
invariant (
818
826
isPlainObj ( value ) ,
@@ -881,6 +889,7 @@ export class GraphQLInputObjectType {
881
889
882
890
constructor ( config : InputObjectConfig ) {
883
891
invariant ( config . name , 'Type must be named.' ) ;
892
+ assertValidName ( config . name ) ;
884
893
this . name = config . name ;
885
894
this . description = config . description ;
886
895
this . _typeConfig = config ;
@@ -905,6 +914,7 @@ export class GraphQLInputObjectType {
905
914
) ;
906
915
var resultFieldMap = { } ;
907
916
fieldNames . forEach ( fieldName => {
917
+ assertValidName ( fieldName ) ;
908
918
var field = {
909
919
...fieldMap [ fieldName ] ,
910
920
name : fieldName
@@ -1025,3 +1035,13 @@ export class GraphQLNonNull {
1025
1035
return this . ofType . toString ( ) + '!' ;
1026
1036
}
1027
1037
}
1038
+
1039
+ var NAME_RX = / ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 ] * $ / ;
1040
+
1041
+ // Helper to assert that provided names are valid.
1042
+ function assertValidName ( name : string ) : void {
1043
+ invariant (
1044
+ NAME_RX . test ( name ) ,
1045
+ `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${ name } " does not.`
1046
+ ) ;
1047
+ }
0 commit comments