@@ -375,7 +375,7 @@ type ObjectConfig struct {
375375 Description string `json:"description"`
376376}
377377
378- type FieldsThunk func () Fields
378+ type FieldsThunk func () ( Fields , error )
379379
380380func NewObject (config ObjectConfig ) * Object {
381381 objectType := & Object {}
@@ -416,10 +416,11 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
416416 case FieldsThunk :
417417 // if the fields are defined as a thunk when the object is created
418418 // then wrap the thunk in another thunk.
419- gt .typeConfig .Fields = (FieldsThunk )(func () Fields {
420- newFields := fields ()
419+ gt .typeConfig .Fields = (FieldsThunk )(func () (Fields , error ) {
420+ newFields , err := fields ()
421+ gt .err = err
421422 newFields [fieldName ] = fieldConfig
422- return newFields
423+ return newFields , err
423424 })
424425 gt .initialisedFields = false
425426 }
@@ -433,22 +434,36 @@ func (gt *Object) Description() string {
433434func (gt * Object ) String () string {
434435 return gt .PrivateName
435436}
437+
436438func (gt * Object ) Fields () FieldDefinitionMap {
439+ // We discard the returned error to preserve the orignal behaviour
440+ // The error will still be added to the `gt.err` property
441+ m , _ := gt .InitFields ()
442+ return m
443+ }
444+
445+ func (gt * Object ) InitFields () (FieldDefinitionMap , error ) {
437446 if gt .initialisedFields {
438- return gt .fields
447+ return gt .fields , nil
439448 }
440449
441450 var configureFields Fields
451+ var err error
442452 switch fields := gt .typeConfig .Fields .(type ) {
443453 case Fields :
444454 configureFields = fields
445455 case FieldsThunk :
446- configureFields = fields ()
456+ configureFields , err = fields ()
457+ }
458+
459+ if err != nil {
460+ gt .err = err
461+ return nil , err
447462 }
448463
449464 gt .fields , gt .err = defineFieldMap (gt , configureFields )
450465 gt .initialisedFields = true
451- return gt .fields
466+ return gt .fields , nil
452467}
453468
454469func (gt * Object ) Interfaces () []* Interface {
@@ -753,21 +768,34 @@ func (it *Interface) Description() string {
753768}
754769
755770func (it * Interface ) Fields () (fields FieldDefinitionMap ) {
771+ // We discard the returned error to preserve the orignal behaviour
772+ // The error will still be added to the `gt.err` property
773+ m , _ := it .InitFields ()
774+ return m
775+ }
776+
777+ func (it * Interface ) InitFields () (FieldDefinitionMap , error ) {
756778 if it .initialisedFields {
757- return it .fields
779+ return it .fields , nil
758780 }
759781
760782 var configureFields Fields
783+ var err error
761784 switch fields := it .typeConfig .Fields .(type ) {
762785 case Fields :
763786 configureFields = fields
764787 case FieldsThunk :
765- configureFields = fields ()
788+ configureFields , err = fields ()
789+ }
790+
791+ if err != nil {
792+ it .err = err
793+ return nil , err
766794 }
767795
768796 it .fields , it .err = defineFieldMap (it , configureFields )
769797 it .initialisedFields = true
770- return it .fields
798+ return it .fields , nil
771799}
772800
773801func (it * Interface ) String () string {
0 commit comments