@@ -269,20 +269,20 @@ func NewScalar(config ScalarConfig) *Scalar {
269
269
st .PrivateName = config .Name
270
270
st .PrivateDescription = config .Description
271
271
272
- err = invariant (
272
+ err = invariantf (
273
273
config .Serialize != nil ,
274
- fmt . Sprintf ( `%v must provide "serialize" function. If this custom Scalar is ` +
274
+ `%v must provide "serialize" function. If this custom Scalar is ` +
275
275
`also used as an input type, ensure "parseValue" and "parseLiteral" ` +
276
- `functions are also provided.` , st ) ,
276
+ `functions are also provided.` , st ,
277
277
)
278
278
if err != nil {
279
279
st .err = err
280
280
return st
281
281
}
282
282
if config .ParseValue != nil || config .ParseLiteral != nil {
283
- err = invariant (
283
+ err = invariantf (
284
284
config .ParseValue != nil && config .ParseLiteral != nil ,
285
- fmt . Sprintf ( `%v must provide both "parseValue" and "parseLiteral" functions.` , st ) ,
285
+ `%v must provide both "parseValue" and "parseLiteral" functions.` , st ,
286
286
)
287
287
if err != nil {
288
288
st .err = err
@@ -365,9 +365,11 @@ type Object struct {
365
365
PrivateDescription string `json:"description"`
366
366
IsTypeOf IsTypeOfFn
367
367
368
- typeConfig ObjectConfig
369
- fields FieldDefinitionMap
370
- interfaces []* Interface
368
+ typeConfig ObjectConfig
369
+ initialisedFields bool
370
+ fields FieldDefinitionMap
371
+ initialisedInterfaces bool
372
+ interfaces []* Interface
371
373
// Interim alternative to throwing an error during schema definition at run-time
372
374
err error
373
375
}
@@ -429,6 +431,7 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
429
431
switch gt .typeConfig .Fields .(type ) {
430
432
case Fields :
431
433
gt .typeConfig .Fields .(Fields )[fieldName ] = fieldConfig
434
+ gt .initialisedFields = false
432
435
}
433
436
}
434
437
func (gt * Object ) Name () string {
@@ -441,20 +444,30 @@ func (gt *Object) String() string {
441
444
return gt .PrivateName
442
445
}
443
446
func (gt * Object ) Fields () FieldDefinitionMap {
447
+ if gt .initialisedFields {
448
+ return gt .fields
449
+ }
450
+
444
451
var configureFields Fields
445
452
switch gt .typeConfig .Fields .(type ) {
446
453
case Fields :
447
454
configureFields = gt .typeConfig .Fields .(Fields )
448
455
case FieldsThunk :
449
456
configureFields = gt .typeConfig .Fields .(FieldsThunk )()
450
457
}
458
+
451
459
fields , err := defineFieldMap (gt , configureFields )
452
460
gt .err = err
453
461
gt .fields = fields
462
+ gt .initialisedFields = true
454
463
return gt .fields
455
464
}
456
465
457
466
func (gt * Object ) Interfaces () []* Interface {
467
+ if gt .initialisedInterfaces {
468
+ return gt .interfaces
469
+ }
470
+
458
471
var configInterfaces []* Interface
459
472
switch gt .typeConfig .Interfaces .(type ) {
460
473
case InterfacesThunk :
@@ -463,14 +476,18 @@ func (gt *Object) Interfaces() []*Interface {
463
476
configInterfaces = gt .typeConfig .Interfaces .([]* Interface )
464
477
case nil :
465
478
default :
466
- gt .err = fmt .Errorf ("Unknown Object.Interfaces type: %v" , reflect .TypeOf (gt .typeConfig .Interfaces ))
479
+ gt .err = fmt .Errorf ("Unknown Object.Interfaces type: %T" , gt .typeConfig .Interfaces )
480
+ gt .initialisedInterfaces = true
467
481
return nil
468
482
}
483
+
469
484
interfaces , err := defineInterfaces (gt , configInterfaces )
470
485
gt .err = err
471
486
gt .interfaces = interfaces
487
+ gt .initialisedInterfaces = true
472
488
return gt .interfaces
473
489
}
490
+
474
491
func (gt * Object ) Error () error {
475
492
return gt .err
476
493
}
@@ -482,20 +499,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
482
499
return ifaces , nil
483
500
}
484
501
for _ , iface := range interfaces {
485
- err := invariant (
502
+ err := invariantf (
486
503
iface != nil ,
487
- fmt . Sprintf ( `%v may only implement Interface types, it cannot implement: %v.` , ttype , iface ) ,
504
+ `%v may only implement Interface types, it cannot implement: %v.` , ttype , iface ,
488
505
)
489
506
if err != nil {
490
507
return ifaces , err
491
508
}
492
509
if iface .ResolveType != nil {
493
- err = invariant (
510
+ err = invariantf (
494
511
iface .ResolveType != nil ,
495
- fmt . Sprintf ( `Interface Type %v does not provide a "resolveType" function ` +
512
+ `Interface Type %v does not provide a "resolveType" function ` +
496
513
`and implementing Type %v does not provide a "isTypeOf" ` +
497
514
`function. There is no way to resolve this implementing type ` +
498
- `during execution.` , iface , ttype ) ,
515
+ `during execution.` , iface , ttype ,
499
516
)
500
517
if err != nil {
501
518
return ifaces , err
@@ -507,20 +524,12 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
507
524
return ifaces , nil
508
525
}
509
526
510
- func defineFieldMap (ttype Named , fields interface {}) (FieldDefinitionMap , error ) {
511
- var fieldMap Fields
512
- switch fields .(type ) {
513
- case Fields :
514
- fieldMap = fields .(Fields )
515
- case FieldsThunk :
516
- fieldMap = fields .(FieldsThunk )()
517
- }
518
-
527
+ func defineFieldMap (ttype Named , fieldMap Fields ) (FieldDefinitionMap , error ) {
519
528
resultFieldMap := FieldDefinitionMap {}
520
529
521
- err := invariant (
530
+ err := invariantf (
522
531
len (fieldMap ) > 0 ,
523
- fmt . Sprintf ( `%v fields must be an object with field names as keys or a function which return such an object.` , ttype ) ,
532
+ `%v fields must be an object with field names as keys or a function which return such an object.` , ttype ,
524
533
)
525
534
if err != nil {
526
535
return resultFieldMap , err
@@ -530,9 +539,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
530
539
if field == nil {
531
540
continue
532
541
}
533
- err = invariant (
542
+ err = invariantf (
534
543
field .Type != nil ,
535
- fmt . Sprintf ( `%v.%v field type must be Output Type but got: %v.` , ttype , fieldName , field .Type ) ,
544
+ `%v.%v field type must be Output Type but got: %v.` , ttype , fieldName , field .Type ,
536
545
)
537
546
if err != nil {
538
547
return resultFieldMap , err
@@ -558,16 +567,16 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
558
567
if err != nil {
559
568
return resultFieldMap , err
560
569
}
561
- err = invariant (
570
+ err = invariantf (
562
571
arg != nil ,
563
- fmt . Sprintf ( `%v.%v args must be an object with argument names as keys.` , ttype , fieldName ) ,
572
+ `%v.%v args must be an object with argument names as keys.` , ttype , fieldName ,
564
573
)
565
574
if err != nil {
566
575
return resultFieldMap , err
567
576
}
568
- err = invariant (
577
+ err = invariantf (
569
578
arg .Type != nil ,
570
- fmt . Sprintf ( `%v.%v(%v:) argument type must be Input Type but got: %v.` , ttype , fieldName , argName , arg .Type ) ,
579
+ `%v.%v(%v:) argument type must be Input Type but got: %v.` , ttype , fieldName , argName , arg .Type ,
571
580
)
572
581
if err != nil {
573
582
return resultFieldMap , err
@@ -695,9 +704,10 @@ type Interface struct {
695
704
PrivateDescription string `json:"description"`
696
705
ResolveType ResolveTypeFn
697
706
698
- typeConfig InterfaceConfig
699
- fields FieldDefinitionMap
700
- err error
707
+ typeConfig InterfaceConfig
708
+ initialisedFields bool
709
+ fields FieldDefinitionMap
710
+ err error
701
711
}
702
712
type InterfaceConfig struct {
703
713
Name string `json:"name"`
@@ -751,30 +761,42 @@ func (it *Interface) AddFieldConfig(fieldName string, fieldConfig *Field) {
751
761
switch it .typeConfig .Fields .(type ) {
752
762
case Fields :
753
763
it .typeConfig .Fields .(Fields )[fieldName ] = fieldConfig
764
+ it .initialisedFields = false
754
765
}
755
766
}
767
+
756
768
func (it * Interface ) Name () string {
757
769
return it .PrivateName
758
770
}
771
+
759
772
func (it * Interface ) Description () string {
760
773
return it .PrivateDescription
761
774
}
775
+
762
776
func (it * Interface ) Fields () (fields FieldDefinitionMap ) {
777
+ if it .initialisedFields {
778
+ return it .fields
779
+ }
780
+
763
781
var configureFields Fields
764
782
switch it .typeConfig .Fields .(type ) {
765
783
case Fields :
766
784
configureFields = it .typeConfig .Fields .(Fields )
767
785
case FieldsThunk :
768
786
configureFields = it .typeConfig .Fields .(FieldsThunk )()
769
787
}
788
+
770
789
fields , err := defineFieldMap (it , configureFields )
771
790
it .err = err
772
791
it .fields = fields
792
+ it .initialisedFields = true
773
793
return it .fields
774
794
}
795
+
775
796
func (it * Interface ) String () string {
776
797
return it .PrivateName
777
798
}
799
+
778
800
func (it * Interface ) Error () error {
779
801
return it .err
780
802
}
@@ -834,30 +856,30 @@ func NewUnion(config UnionConfig) *Union {
834
856
objectType .PrivateDescription = config .Description
835
857
objectType .ResolveType = config .ResolveType
836
858
837
- err = invariant (
859
+ err = invariantf (
838
860
len (config .Types ) > 0 ,
839
- fmt . Sprintf ( `Must provide Array of types for Union %v.` , config .Name ) ,
861
+ `Must provide Array of types for Union %v.` , config .Name ,
840
862
)
841
863
if err != nil {
842
864
objectType .err = err
843
865
return objectType
844
866
}
845
867
for _ , ttype := range config .Types {
846
- err := invariant (
868
+ err := invariantf (
847
869
ttype != nil ,
848
- fmt . Sprintf ( `%v may only contain Object types, it cannot contain: %v.` , objectType , ttype ) ,
870
+ `%v may only contain Object types, it cannot contain: %v.` , objectType , ttype ,
849
871
)
850
872
if err != nil {
851
873
objectType .err = err
852
874
return objectType
853
875
}
854
876
if objectType .ResolveType == nil {
855
- err = invariant (
877
+ err = invariantf (
856
878
ttype .IsTypeOf != nil ,
857
- fmt . Sprintf ( `Union Type %v does not provide a "resolveType" function ` +
879
+ `Union Type %v does not provide a "resolveType" function ` +
858
880
`and possible Type %v does not provide a "isTypeOf" ` +
859
881
`function. There is no way to resolve this possible type ` +
860
- `during execution.` , objectType , ttype ) ,
882
+ `during execution.` , objectType , ttype ,
861
883
)
862
884
if err != nil {
863
885
objectType .err = err
@@ -958,19 +980,19 @@ func NewEnum(config EnumConfig) *Enum {
958
980
func (gt * Enum ) defineEnumValues (valueMap EnumValueConfigMap ) ([]* EnumValueDefinition , error ) {
959
981
values := []* EnumValueDefinition {}
960
982
961
- err := invariant (
983
+ err := invariantf (
962
984
len (valueMap ) > 0 ,
963
- fmt . Sprintf ( `%v values must be an object with value names as keys.` , gt ) ,
985
+ `%v values must be an object with value names as keys.` , gt ,
964
986
)
965
987
if err != nil {
966
988
return values , err
967
989
}
968
990
969
991
for valueName , valueConfig := range valueMap {
970
- err := invariant (
992
+ err := invariantf (
971
993
valueConfig != nil ,
972
- fmt . Sprintf ( `%v.%v must refer to an object with a "value" key ` +
973
- `representing an internal value but got: %v.` , gt , valueName , valueConfig ) ,
994
+ `%v.%v must refer to an object with a "value" key ` +
995
+ `representing an internal value but got: %v.` , gt , valueName , valueConfig ,
974
996
)
975
997
if err != nil {
976
998
return values , err
@@ -1151,9 +1173,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
1151
1173
}
1152
1174
resultFieldMap := InputObjectFieldMap {}
1153
1175
1154
- err := invariant (
1176
+ err := invariantf (
1155
1177
len (fieldMap ) > 0 ,
1156
- fmt . Sprintf ( `%v fields must be an object with field names as keys or a function which return such an object.` , gt ) ,
1178
+ `%v fields must be an object with field names as keys or a function which return such an object.` , gt ,
1157
1179
)
1158
1180
if err != nil {
1159
1181
gt .err = err
@@ -1168,9 +1190,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
1168
1190
if err != nil {
1169
1191
continue
1170
1192
}
1171
- err = invariant (
1193
+ err = invariantf (
1172
1194
fieldConfig .Type != nil ,
1173
- fmt . Sprintf ( `%v.%v field type must be Input Type but got: %v.` , gt , fieldName , fieldConfig .Type ) ,
1195
+ `%v.%v field type must be Input Type but got: %v.` , gt , fieldName , fieldConfig .Type ,
1174
1196
)
1175
1197
if err != nil {
1176
1198
gt .err = err
@@ -1231,7 +1253,7 @@ type List struct {
1231
1253
func NewList (ofType Type ) * List {
1232
1254
gl := & List {}
1233
1255
1234
- err := invariant (ofType != nil , fmt . Sprintf ( `Can only create List of a Type but got: %v.` , ofType ) )
1256
+ err := invariantf (ofType != nil , `Can only create List of a Type but got: %v.` , ofType )
1235
1257
if err != nil {
1236
1258
gl .err = err
1237
1259
return gl
@@ -1284,7 +1306,7 @@ func NewNonNull(ofType Type) *NonNull {
1284
1306
gl := & NonNull {}
1285
1307
1286
1308
_ , isOfTypeNonNull := ofType .(* NonNull )
1287
- err := invariant (ofType != nil && ! isOfTypeNonNull , fmt . Sprintf ( `Can only create NonNull of a Nullable Type but got: %v.` , ofType ) )
1309
+ err := invariantf (ofType != nil && ! isOfTypeNonNull , `Can only create NonNull of a Nullable Type but got: %v.` , ofType )
1288
1310
if err != nil {
1289
1311
gl .err = err
1290
1312
return gl
@@ -1311,8 +1333,8 @@ func (gl *NonNull) Error() error {
1311
1333
var NameRegExp , _ = regexp .Compile ("^[_a-zA-Z][_a-zA-Z0-9]*$" )
1312
1334
1313
1335
func assertValidName (name string ) error {
1314
- return invariant (
1336
+ return invariantf (
1315
1337
NameRegExp .MatchString (name ),
1316
- fmt . Sprintf ( `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.` , name ),
1317
- )
1338
+ `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.` , name )
1339
+
1318
1340
}
0 commit comments