Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/exec/resolvable: include struct field name in errors #477

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}
}
agnivade marked this conversation as resolved.
Show resolved Hide resolved

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
agnivade marked this conversation as resolved.
Show resolved Hide resolved
}
`,
},
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)
agnivade marked this conversation as resolved.
Show resolved Hide resolved
}
Fields[f.Name] = fe
}
Expand Down