Skip to content

Commit

Permalink
internal/exec/resolvable: include struct field name in errors (#477)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
agnivade committed Dec 9, 2021
1 parent c32bb42 commit aa16691
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 27 additions & 1 deletion graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3074,10 +3074,20 @@ type inputArgumentsObjectMismatch2 struct{}

type inputArgumentsObjectMismatch3 struct{}

type fieldNameMismatch struct{}

type helloInput struct {
Name string
}

type helloOutput struct {
Name string
}

func (*fieldNameMismatch) Hello() helloOutput {
return helloOutput{}
}

type helloInputMismatch struct {
World string
}
Expand Down Expand Up @@ -3110,6 +3120,7 @@ func TestInputArguments_failSchemaParsing(t *testing.T) {
type args struct {
Resolver interface{}
Schema string
Opts []graphql.SchemaOpt
}
type want struct {
Error string
Expand Down Expand Up @@ -3217,14 +3228,29 @@ func TestInputArguments_failSchemaParsing(t *testing.T) {
},
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"},
},
"Struct field name inclusion": {
Args: args{
Resolver: &fieldNameMismatch{},
Opts: []graphql.SchemaOpt{graphql.UseFieldResolvers()},
Schema: `
type Query {
hello(): HelloOutput!
}
type HelloOutput {
name: Int
}
`,
},
Want: want{Error: "string is not a pointer\n\tused by (graphql_test.helloOutput).Name\n\tused by (*graphql_test.fieldNameMismatch).Hello"},
},
}

for name, tt := range testTable {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

_, err := graphql.ParseSchema(tt.Args.Schema, tt.Args.Resolver)
_, err := graphql.ParseSchema(tt.Args.Schema, tt.Args.Resolver, tt.Args.Opts...)
if err == nil || err.Error() != tt.Want.Error {
t.Log("Schema parsing error mismatch")
t.Logf("got: %s", err)
Expand Down
8 changes: 7 additions & 1 deletion internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,13 @@ func (b *execBuilder) makeObjectExec(typeName string, fields types.FieldsDefinit
}
fe, err := b.makeFieldExec(typeName, f, m, sf, methodIndex, fieldIndex, methodHasReceiver)
if err != nil {
return nil, fmt.Errorf("%s\n\tused by (%s).%s", err, resolverType, m.Name)
var resolverName string
if methodIndex != -1 {
resolverName = m.Name
} else {
resolverName = sf.Name
}
return nil, fmt.Errorf("%s\n\tused by (%s).%s", err, resolverType, resolverName)
}
Fields[f.Name] = fe
}
Expand Down

0 comments on commit aa16691

Please sign in to comment.