Skip to content

Commit aa16691

Browse files
authored
internal/exec/resolvable: include struct field name in errors (#477)
* internal/exec/resolvable: include struct field name in errors We were only adding method name, which meant that it was taking an empty string if the resolver was a struct field. This was making the error messages hard to parse as the user can't know which field has the error. Added a check to use the correct variable. * improve test
1 parent c32bb42 commit aa16691

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

graphql_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3074,10 +3074,20 @@ type inputArgumentsObjectMismatch2 struct{}
30743074

30753075
type inputArgumentsObjectMismatch3 struct{}
30763076

3077+
type fieldNameMismatch struct{}
3078+
30773079
type helloInput struct {
30783080
Name string
30793081
}
30803082

3083+
type helloOutput struct {
3084+
Name string
3085+
}
3086+
3087+
func (*fieldNameMismatch) Hello() helloOutput {
3088+
return helloOutput{}
3089+
}
3090+
30813091
type helloInputMismatch struct {
30823092
World string
30833093
}
@@ -3110,6 +3120,7 @@ func TestInputArguments_failSchemaParsing(t *testing.T) {
31103120
type args struct {
31113121
Resolver interface{}
31123122
Schema string
3123+
Opts []graphql.SchemaOpt
31133124
}
31143125
type want struct {
31153126
Error string
@@ -3217,14 +3228,29 @@ func TestInputArguments_failSchemaParsing(t *testing.T) {
32173228
},
32183229
Want: want{Error: "field \"Input\": *struct { Thing string } does not define field \"name\" (hint: missing `args struct { ... }` wrapper for field arguments, or missing field on input struct)\n\tused by (*graphql_test.inputArgumentsObjectMismatch3).Hello"},
32193230
},
3231+
"Struct field name inclusion": {
3232+
Args: args{
3233+
Resolver: &fieldNameMismatch{},
3234+
Opts: []graphql.SchemaOpt{graphql.UseFieldResolvers()},
3235+
Schema: `
3236+
type Query {
3237+
hello(): HelloOutput!
3238+
}
3239+
type HelloOutput {
3240+
name: Int
3241+
}
3242+
`,
3243+
},
3244+
Want: want{Error: "string is not a pointer\n\tused by (graphql_test.helloOutput).Name\n\tused by (*graphql_test.fieldNameMismatch).Hello"},
3245+
},
32203246
}
32213247

32223248
for name, tt := range testTable {
32233249
tt := tt
32243250
t.Run(name, func(t *testing.T) {
32253251
t.Parallel()
32263252

3227-
_, err := graphql.ParseSchema(tt.Args.Schema, tt.Args.Resolver)
3253+
_, err := graphql.ParseSchema(tt.Args.Schema, tt.Args.Resolver, tt.Args.Opts...)
32283254
if err == nil || err.Error() != tt.Want.Error {
32293255
t.Log("Schema parsing error mismatch")
32303256
t.Logf("got: %s", err)

internal/exec/resolvable/resolvable.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,13 @@ func (b *execBuilder) makeObjectExec(typeName string, fields types.FieldsDefinit
256256
}
257257
fe, err := b.makeFieldExec(typeName, f, m, sf, methodIndex, fieldIndex, methodHasReceiver)
258258
if err != nil {
259-
return nil, fmt.Errorf("%s\n\tused by (%s).%s", err, resolverType, m.Name)
259+
var resolverName string
260+
if methodIndex != -1 {
261+
resolverName = m.Name
262+
} else {
263+
resolverName = sf.Name
264+
}
265+
return nil, fmt.Errorf("%s\n\tused by (%s).%s", err, resolverType, resolverName)
260266
}
261267
Fields[f.Name] = fe
262268
}

0 commit comments

Comments
 (0)