@@ -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
}
@@ -441,20 +443,30 @@ func (gt *Object) String() string {
441
443
return gt .PrivateName
442
444
}
443
445
func (gt * Object ) Fields () FieldDefinitionMap {
446
+ if gt .initialisedFields {
447
+ return gt .fields
448
+ }
449
+
444
450
var configureFields Fields
445
451
switch gt .typeConfig .Fields .(type ) {
446
452
case Fields :
447
453
configureFields = gt .typeConfig .Fields .(Fields )
448
454
case FieldsThunk :
449
455
configureFields = gt .typeConfig .Fields .(FieldsThunk )()
450
456
}
457
+
451
458
fields , err := defineFieldMap (gt , configureFields )
452
459
gt .err = err
453
460
gt .fields = fields
461
+ gt .initialisedFields = true
454
462
return gt .fields
455
463
}
456
464
457
465
func (gt * Object ) Interfaces () []* Interface {
466
+ if gt .initialisedInterfaces {
467
+ return gt .interfaces
468
+ }
469
+
458
470
var configInterfaces []* Interface
459
471
switch gt .typeConfig .Interfaces .(type ) {
460
472
case InterfacesThunk :
@@ -463,14 +475,18 @@ func (gt *Object) Interfaces() []*Interface {
463
475
configInterfaces = gt .typeConfig .Interfaces .([]* Interface )
464
476
case nil :
465
477
default :
466
- gt .err = fmt .Errorf ("Unknown Object.Interfaces type: %v" , reflect .TypeOf (gt .typeConfig .Interfaces ))
478
+ gt .err = fmt .Errorf ("Unknown Object.Interfaces type: %T" , gt .typeConfig .Interfaces )
479
+ gt .initialisedInterfaces = true
467
480
return nil
468
481
}
482
+
469
483
interfaces , err := defineInterfaces (gt , configInterfaces )
470
484
gt .err = err
471
485
gt .interfaces = interfaces
486
+ gt .initialisedInterfaces = true
472
487
return gt .interfaces
473
488
}
489
+
474
490
func (gt * Object ) Error () error {
475
491
return gt .err
476
492
}
@@ -507,15 +523,7 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
507
523
return ifaces , nil
508
524
}
509
525
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
-
526
+ func defineFieldMap (ttype Named , fieldMap Fields ) (FieldDefinitionMap , error ) {
519
527
resultFieldMap := FieldDefinitionMap {}
520
528
521
529
err := invariant (
@@ -695,9 +703,10 @@ type Interface struct {
695
703
PrivateDescription string `json:"description"`
696
704
ResolveType ResolveTypeFn
697
705
698
- typeConfig InterfaceConfig
699
- fields FieldDefinitionMap
700
- err error
706
+ typeConfig InterfaceConfig
707
+ initialisedFields bool
708
+ fields FieldDefinitionMap
709
+ err error
701
710
}
702
711
type InterfaceConfig struct {
703
712
Name string `json:"name"`
@@ -753,28 +762,39 @@ func (it *Interface) AddFieldConfig(fieldName string, fieldConfig *Field) {
753
762
it .typeConfig .Fields .(Fields )[fieldName ] = fieldConfig
754
763
}
755
764
}
765
+
756
766
func (it * Interface ) Name () string {
757
767
return it .PrivateName
758
768
}
769
+
759
770
func (it * Interface ) Description () string {
760
771
return it .PrivateDescription
761
772
}
773
+
762
774
func (it * Interface ) Fields () (fields FieldDefinitionMap ) {
775
+ if it .initialisedFields {
776
+ return it .fields
777
+ }
778
+
763
779
var configureFields Fields
764
780
switch it .typeConfig .Fields .(type ) {
765
781
case Fields :
766
782
configureFields = it .typeConfig .Fields .(Fields )
767
783
case FieldsThunk :
768
784
configureFields = it .typeConfig .Fields .(FieldsThunk )()
769
785
}
786
+
770
787
fields , err := defineFieldMap (it , configureFields )
771
788
it .err = err
772
789
it .fields = fields
790
+ it .initialisedFields = true
773
791
return it .fields
774
792
}
793
+
775
794
func (it * Interface ) String () string {
776
795
return it .PrivateName
777
796
}
797
+
778
798
func (it * Interface ) Error () error {
779
799
return it .err
780
800
}
0 commit comments