Skip to content

Commit 67d621a

Browse files
committed
updates to match new FieldResolveFn returning params - #44
1 parent 149040d commit 67d621a

14 files changed

+207
-207
lines changed

abstract_test.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
4848
Fields: graphql.Fields{
4949
"name": &graphql.Field{
5050
Type: graphql.String,
51-
Resolve: func(p graphql.ResolveParams) interface{} {
51+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
5252
if dog, ok := p.Source.(*testDog); ok {
53-
return dog.Name
53+
return dog.Name, nil
5454
}
55-
return nil
55+
return nil, nil
5656
},
5757
},
5858
"woofs": &graphql.Field{
5959
Type: graphql.Boolean,
60-
Resolve: func(p graphql.ResolveParams) interface{} {
60+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
6161
if dog, ok := p.Source.(*testDog); ok {
62-
return dog.Woofs
62+
return dog.Woofs, nil
6363
}
64-
return nil
64+
return nil, nil
6565
},
6666
},
6767
},
@@ -79,20 +79,20 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
7979
Fields: graphql.Fields{
8080
"name": &graphql.Field{
8181
Type: graphql.String,
82-
Resolve: func(p graphql.ResolveParams) interface{} {
82+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
8383
if cat, ok := p.Source.(*testCat); ok {
84-
return cat.Name
84+
return cat.Name, nil
8585
}
86-
return nil
86+
return nil, nil
8787
},
8888
},
8989
"meows": &graphql.Field{
9090
Type: graphql.Boolean,
91-
Resolve: func(p graphql.ResolveParams) interface{} {
91+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
9292
if cat, ok := p.Source.(*testCat); ok {
93-
return cat.Meows
93+
return cat.Meows, nil
9494
}
95-
return nil
95+
return nil, nil
9696
},
9797
},
9898
},
@@ -103,11 +103,11 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
103103
Fields: graphql.Fields{
104104
"pets": &graphql.Field{
105105
Type: graphql.NewList(petType),
106-
Resolve: func(p graphql.ResolveParams) interface{} {
106+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
107107
return []interface{}{
108108
&testDog{"Odie", true},
109109
&testCat{"Garfield", false},
110-
}
110+
}, nil
111111
},
112112
},
113113
},
@@ -202,11 +202,11 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
202202
Fields: graphql.Fields{
203203
"pets": &graphql.Field{
204204
Type: graphql.NewList(petType),
205-
Resolve: func(p graphql.ResolveParams) interface{} {
205+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
206206
return []interface{}{
207207
&testDog{"Odie", true},
208208
&testCat{"Garfield", false},
209-
}
209+
}, nil
210210
},
211211
},
212212
},
@@ -288,11 +288,11 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
288288
Fields: graphql.Fields{
289289
"name": &graphql.Field{
290290
Type: graphql.String,
291-
Resolve: func(p graphql.ResolveParams) interface{} {
291+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
292292
if human, ok := p.Source.(*testHuman); ok {
293-
return human.Name
293+
return human.Name, nil
294294
}
295-
return nil
295+
return nil, nil
296296
},
297297
},
298298
},
@@ -309,20 +309,20 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
309309
Fields: graphql.Fields{
310310
"name": &graphql.Field{
311311
Type: graphql.String,
312-
Resolve: func(p graphql.ResolveParams) interface{} {
312+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
313313
if dog, ok := p.Source.(*testDog); ok {
314-
return dog.Name
314+
return dog.Name, nil
315315
}
316-
return nil
316+
return nil, nil
317317
},
318318
},
319319
"woofs": &graphql.Field{
320320
Type: graphql.Boolean,
321-
Resolve: func(p graphql.ResolveParams) interface{} {
321+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
322322
if dog, ok := p.Source.(*testDog); ok {
323-
return dog.Woofs
323+
return dog.Woofs, nil
324324
}
325-
return nil
325+
return nil, nil
326326
},
327327
},
328328
},
@@ -339,20 +339,20 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
339339
Fields: graphql.Fields{
340340
"name": &graphql.Field{
341341
Type: graphql.String,
342-
Resolve: func(p graphql.ResolveParams) interface{} {
342+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
343343
if cat, ok := p.Source.(*testCat); ok {
344-
return cat.Name
344+
return cat.Name, nil
345345
}
346-
return nil
346+
return nil, nil
347347
},
348348
},
349349
"meows": &graphql.Field{
350350
Type: graphql.Boolean,
351-
Resolve: func(p graphql.ResolveParams) interface{} {
351+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
352352
if cat, ok := p.Source.(*testCat); ok {
353-
return cat.Meows
353+
return cat.Meows, nil
354354
}
355-
return nil
355+
return nil, nil
356356
},
357357
},
358358
},
@@ -363,12 +363,12 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
363363
Fields: graphql.Fields{
364364
"pets": &graphql.Field{
365365
Type: graphql.NewList(petType),
366-
Resolve: func(p graphql.ResolveParams) interface{} {
366+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
367367
return []interface{}{
368368
&testDog{"Odie", true},
369369
&testCat{"Garfield", false},
370370
&testHuman{"Jon"},
371-
}
371+
}, nil
372372
},
373373
},
374374
},
@@ -480,12 +480,12 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
480480
Fields: graphql.Fields{
481481
"pets": &graphql.Field{
482482
Type: graphql.NewList(petType),
483-
Resolve: func(p graphql.ResolveParams) interface{} {
483+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
484484
return []interface{}{
485485
&testDog{"Odie", true},
486486
&testCat{"Garfield", false},
487487
&testHuman{"Jon"},
488-
}
488+
}, nil
489489
},
490490
},
491491
},

enum_type_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ var enumTypeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
3939
Type: graphql.String,
4040
},
4141
},
42-
Resolve: func(p graphql.ResolveParams) interface{} {
42+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
4343
if fromInt, ok := p.Args["fromInt"]; ok {
44-
return fromInt
44+
return fromInt, nil
4545
}
4646
if fromString, ok := p.Args["fromString"]; ok {
47-
return fromString
47+
return fromString, nil
4848
}
4949
if fromEnum, ok := p.Args["fromEnum"]; ok {
50-
return fromEnum
50+
return fromEnum, nil
5151
}
52-
return nil
52+
return nil, nil
5353
},
5454
},
5555
"colorInt": &graphql.Field{
@@ -62,14 +62,14 @@ var enumTypeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
6262
Type: graphql.Int,
6363
},
6464
},
65-
Resolve: func(p graphql.ResolveParams) interface{} {
65+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
6666
if fromInt, ok := p.Args["fromInt"]; ok {
67-
return fromInt
67+
return fromInt, nil
6868
}
6969
if fromEnum, ok := p.Args["fromEnum"]; ok {
70-
return fromEnum
70+
return fromEnum, nil
7171
}
72-
return nil
72+
return nil, nil
7373
},
7474
},
7575
},
@@ -84,11 +84,11 @@ var enumTypeTestMutationType = graphql.NewObject(graphql.ObjectConfig{
8484
Type: enumTypeTestColorType,
8585
},
8686
},
87-
Resolve: func(p graphql.ResolveParams) interface{} {
87+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
8888
if color, ok := p.Args["color"]; ok {
89-
return color
89+
return color, nil
9090
}
91-
return nil
91+
return nil, nil
9292
},
9393
},
9494
},

examples/hello-world/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func main() {
1313
fields := graphql.Fields{
1414
"hello": &graphql.Field{
1515
Type: graphql.String,
16-
Resolve: func(p graphql.ResolveParams) interface{} {
17-
return "world"
16+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
17+
return "world", nil
1818
},
1919
},
2020
}

examples/http/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ var queryType = graphql.NewObject(
5656
Type: graphql.String,
5757
},
5858
},
59-
Resolve: func(p graphql.ResolveParams) interface{} {
59+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
6060
idQuery, isOK := p.Args["id"].(string)
6161
if isOK {
62-
return data[idQuery]
62+
return data[idQuery], nil
6363
}
64-
return nil
64+
return nil, nil
6565
},
6666
},
6767
},

executor.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func resolveField(eCtx *ExecutionContext, parentType *Object, source interface{}
498498
// it is wrapped as a Error with locations. Log this error and return
499499
// null if allowed, otherwise throw the error so the parent field can handle
500500
// it.
501-
result = resolveFn(ResolveParams{
501+
result, _ = resolveFn(ResolveParams{
502502
Source: source,
503503
Args: args,
504504
Info: info,
@@ -666,14 +666,14 @@ func completeValue(eCtx *ExecutionContext, returnType Type, fieldASTs []*ast.Fie
666666

667667
}
668668

669-
func defaultResolveFn(p ResolveParams) interface{} {
669+
func defaultResolveFn(p ResolveParams) (interface{}, error) {
670670
// try to resolve p.Source as a struct first
671671
sourceVal := reflect.ValueOf(p.Source)
672672
if sourceVal.IsValid() && sourceVal.Type().Kind() == reflect.Ptr {
673673
sourceVal = sourceVal.Elem()
674674
}
675675
if !sourceVal.IsValid() {
676-
return nil
676+
return nil, nil
677677
}
678678
if sourceVal.Type().Kind() == reflect.Struct {
679679
// find field based on struct's json tag
@@ -685,7 +685,7 @@ func defaultResolveFn(p ResolveParams) interface{} {
685685
typeField := sourceVal.Type().Field(i)
686686
// try matching the field name first
687687
if typeField.Name == p.Info.FieldName {
688-
return valueField.Interface()
688+
return valueField.Interface(), nil
689689
}
690690
tag := typeField.Tag
691691
jsonTag := tag.Get("json")
@@ -696,9 +696,9 @@ func defaultResolveFn(p ResolveParams) interface{} {
696696
if jsonOptions[0] != p.Info.FieldName {
697697
continue
698698
}
699-
return valueField.Interface()
699+
return valueField.Interface(), nil
700700
}
701-
return nil
701+
return nil, nil
702702
}
703703

704704
// try p.Source as a map[string]interface
@@ -709,14 +709,14 @@ func defaultResolveFn(p ResolveParams) interface{} {
709709
// try type casting the func to the most basic func signature
710710
// for more complex signatures, user have to define ResolveFn
711711
if propertyFn, ok := property.(func() interface{}); ok {
712-
return propertyFn()
712+
return propertyFn(), nil
713713
}
714714
}
715-
return property
715+
return property, nil
716716
}
717717

718718
// last resort, return nil
719-
return nil
719+
return nil, nil
720720
}
721721

722722
/**

executor_schema_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
106106
Type: graphql.Int,
107107
},
108108
},
109-
Resolve: func(p graphql.ResolveParams) interface{} {
109+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
110110
if author, ok := p.Source.(*testAuthor); ok {
111111
width := fmt.Sprintf("%v", p.Args["width"])
112112
height := fmt.Sprintf("%v", p.Args["height"])
113-
return author.Pic(width, height)
113+
return author.Pic(width, height), nil
114114
}
115-
return nil
115+
return nil, nil
116116
},
117117
},
118118
"recentArticle": &graphql.Field{},
@@ -156,14 +156,14 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
156156
Type: graphql.ID,
157157
},
158158
},
159-
Resolve: func(p graphql.ResolveParams) interface{} {
159+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
160160
id := p.Args["id"]
161-
return article(id)
161+
return article(id), nil
162162
},
163163
},
164164
"feed": &graphql.Field{
165165
Type: graphql.NewList(blogArticle),
166-
Resolve: func(p graphql.ResolveParams) interface{} {
166+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
167167
return []*testArticle{
168168
article(1),
169169
article(2),
@@ -175,7 +175,7 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
175175
article(8),
176176
article(9),
177177
article(10),
178-
}
178+
}, nil
179179
},
180180
},
181181
},

0 commit comments

Comments
 (0)