Skip to content

Commit 7e4c02b

Browse files
committed
feat: Allow returning of errors from fields thunk (#2)
- Allow returning of errors from fields thunk
1 parent d2166c9 commit 7e4c02b

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

definition.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ type ObjectConfig struct {
376376
Description string `json:"description"`
377377
}
378378

379-
type FieldsThunk func() Fields
379+
type FieldsThunk func() (Fields, error)
380380

381381
func NewObject(config ObjectConfig) *Object {
382382
objectType := &Object{}
@@ -417,10 +417,11 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
417417
case FieldsThunk:
418418
// if the fields are defined as a thunk when the object is created
419419
// then wrap the thunk in another thunk.
420-
gt.typeConfig.Fields = (FieldsThunk)(func() Fields {
421-
newFields := fields()
420+
gt.typeConfig.Fields = (FieldsThunk)(func() (Fields, error) {
421+
newFields, err := fields()
422+
gt.err = err
422423
newFields[fieldName] = fieldConfig
423-
return newFields
424+
return newFields, err
424425
})
425426
gt.initialisedFields = false
426427
}
@@ -434,22 +435,36 @@ func (gt *Object) Description() string {
434435
func (gt *Object) String() string {
435436
return gt.PrivateName
436437
}
438+
437439
func (gt *Object) Fields() FieldDefinitionMap {
440+
// We discard the returned error to preserve the orignal behaviour
441+
// The error will still be added to the `gt.err` property
442+
m, _ := gt.InitFields()
443+
return m
444+
}
445+
446+
func (gt *Object) InitFields() (FieldDefinitionMap, error) {
438447
if gt.initialisedFields {
439-
return gt.fields
448+
return gt.fields, nil
440449
}
441450

442451
var configureFields Fields
452+
var err error
443453
switch fields := gt.typeConfig.Fields.(type) {
444454
case Fields:
445455
configureFields = fields
446456
case FieldsThunk:
447-
configureFields = fields()
457+
configureFields, err = fields()
458+
}
459+
460+
if err != nil {
461+
gt.err = err
462+
return nil, err
448463
}
449464

450465
gt.fields, gt.err = defineFieldMap(gt, configureFields)
451466
gt.initialisedFields = true
452-
return gt.fields
467+
return gt.fields, nil
453468
}
454469

455470
func (gt *Object) Interfaces() []*Interface {
@@ -756,21 +771,34 @@ func (it *Interface) Description() string {
756771
}
757772

758773
func (it *Interface) Fields() (fields FieldDefinitionMap) {
774+
// We discard the returned error to preserve the orignal behaviour
775+
// The error will still be added to the `gt.err` property
776+
m, _ := it.InitFields()
777+
return m
778+
}
779+
780+
func (it *Interface) InitFields() (FieldDefinitionMap, error) {
759781
if it.initialisedFields {
760-
return it.fields
782+
return it.fields, nil
761783
}
762784

763785
var configureFields Fields
786+
var err error
764787
switch fields := it.typeConfig.Fields.(type) {
765788
case Fields:
766789
configureFields = fields
767790
case FieldsThunk:
768-
configureFields = fields()
791+
configureFields, err = fields()
792+
}
793+
794+
if err != nil {
795+
it.err = err
796+
return nil, err
769797
}
770798

771799
it.fields, it.err = defineFieldMap(it, configureFields)
772800
it.initialisedFields = true
773-
return it.fields
801+
return it.fields, nil
774802
}
775803

776804
func (it *Interface) String() string {

definition_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,15 @@ func TestTypeSystem_DefinitionExample_IncludesFieldsThunk(t *testing.T) {
603603
var someObject *graphql.Object
604604
someObject = graphql.NewObject(graphql.ObjectConfig{
605605
Name: "SomeObject",
606-
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
606+
Fields: (graphql.FieldsThunk)(func() (graphql.Fields, error) {
607607
return graphql.Fields{
608608
"f": &graphql.Field{
609609
Type: graphql.Int,
610610
},
611611
"s": &graphql.Field{
612612
Type: someObject,
613613
},
614-
}
614+
}, nil
615615
}),
616616
})
617617
fieldMap := someObject.Fields()
@@ -623,15 +623,15 @@ func TestTypeSystem_DefinitionExample_IncludesFieldsThunk(t *testing.T) {
623623
func TestTypeSystem_DefinitionExampe_AllowsCyclicFieldTypes(t *testing.T) {
624624
personType := graphql.NewObject(graphql.ObjectConfig{
625625
Name: "Person",
626-
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
626+
Fields: (graphql.FieldsThunk)(func() (graphql.Fields, error) {
627627
return graphql.Fields{
628628
"name": &graphql.Field{
629629
Type: graphql.String,
630630
},
631631
"bestFriend": &graphql.Field{
632632
Type: personType,
633633
},
634-
}
634+
}, nil
635635
}),
636636
})
637637

executor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,8 +2146,8 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
21462146

21472147
heroType := graphql.NewObject(graphql.ObjectConfig{
21482148
Name: "Hero",
2149-
Fields: graphql.FieldsThunk(func() graphql.Fields {
2150-
return heroFields
2149+
Fields: graphql.FieldsThunk(func() (graphql.Fields, error) {
2150+
return heroFields, nil
21512151
}),
21522152
})
21532153

rules_overlapping_fields_can_be_merged_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,23 +408,23 @@ func init() {
408408
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
409409
return stringBoxObject
410410
},
411-
Fields: graphql.FieldsThunk(func() graphql.Fields {
411+
Fields: graphql.FieldsThunk(func() (graphql.Fields, error) {
412412
return graphql.Fields{
413413
"deepBox": &graphql.Field{
414414
Type: someBoxInterface,
415415
},
416416
"unrelatedField": &graphql.Field{
417417
Type: graphql.String,
418418
},
419-
}
419+
}, nil
420420
}),
421421
})
422422
stringBoxObject = graphql.NewObject(graphql.ObjectConfig{
423423
Name: "StringBox",
424424
Interfaces: (graphql.InterfacesThunk)(func() []*graphql.Interface {
425425
return []*graphql.Interface{someBoxInterface}
426426
}),
427-
Fields: graphql.FieldsThunk(func() graphql.Fields {
427+
Fields: graphql.FieldsThunk(func() (graphql.Fields, error) {
428428
return graphql.Fields{
429429
"scalar": &graphql.Field{
430430
Type: graphql.String,
@@ -444,15 +444,15 @@ func init() {
444444
"intBox": &graphql.Field{
445445
Type: intBoxObject,
446446
},
447-
}
447+
}, nil
448448
}),
449449
})
450450
intBoxObject = graphql.NewObject(graphql.ObjectConfig{
451451
Name: "IntBox",
452452
Interfaces: (graphql.InterfacesThunk)(func() []*graphql.Interface {
453453
return []*graphql.Interface{someBoxInterface}
454454
}),
455-
Fields: graphql.FieldsThunk(func() graphql.Fields {
455+
Fields: graphql.FieldsThunk(func() (graphql.Fields, error) {
456456
return graphql.Fields{
457457
"scalar": &graphql.Field{
458458
Type: graphql.Int,
@@ -472,7 +472,7 @@ func init() {
472472
"intBox": &graphql.Field{
473473
Type: intBoxObject,
474474
},
475-
}
475+
}, nil
476476
}),
477477
})
478478
var nonNullStringBox1Interface = graphql.NewInterface(graphql.InterfaceConfig{

validation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,24 +1388,24 @@ func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_AcceptsAnObjectWit
13881388
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
13891389
return nil
13901390
},
1391-
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
1391+
Fields: (graphql.FieldsThunk)(func() (graphql.Fields, error) {
13921392
return graphql.Fields{
13931393
"field": &graphql.Field{
13941394
Type: anotherInterface,
13951395
},
1396-
}
1396+
}, nil
13971397
}),
13981398
})
13991399
var anotherObject *graphql.Object
14001400
anotherObject = graphql.NewObject(graphql.ObjectConfig{
14011401
Name: "AnotherObject",
14021402
Interfaces: []*graphql.Interface{anotherInterface},
1403-
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
1403+
Fields: (graphql.FieldsThunk)(func() (graphql.Fields, error) {
14041404
return graphql.Fields{
14051405
"field": &graphql.Field{
14061406
Type: anotherObject,
14071407
},
1408-
}
1408+
}, nil
14091409
}),
14101410
})
14111411
_, err := schemaWithFieldType(anotherObject)

0 commit comments

Comments
 (0)