-
Notifications
You must be signed in to change notification settings - Fork 123
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
Use error instead of *gqlerror.Error #99
Comments
Yeah this is an disappointing gotcha in go. I wish it would get fixed. Returning error is fine, but its really only returning one type of error and gqlgen is going to reach into it on the other side. This results in interface overhead and type assertion spaghetti. All that said, would be open to a pr if you want to do the conversion in gqlgen too. |
I've opened a PR for gqlparser but I'm having trouble adding the conversions you mentioned to gqlgen. |
you can add something like this at the end of the go.mod
and you're done! One of the best go modules features |
Okay thanks 😄 |
I wasted a lot of time trying to work out why ParseQuery was always returning an error (it wasn't).
Please just change all error return types to |
I hit the same issue when I first picked this up, too. It's definitely a surprising behavior, compared to most (if not all) Go libraries I've worked with over the years.
@vektah As an idea, I think most potential type assertions in gqlgen could be handled using the built-in err := gqlparser.LoadSchema(files...)
var gqlErr *gqlerror.Error
if !errors.As(err, &gqlErr) {
return err
}
// do gqlparser-specific error handling here
gqlErr.Message At the very least, the spaghetti could be cleaner spaghetti 😉 |
I have a counter-argument. It looks like the parsing library never return errors other than Can I propose a pure additive change?
|
In gqlparser all methods which are returning an error are using the explicit struct type (e.g.
*gqlerror.Error
orgqlerror.List
).Problem
As long as you use the explicit types everything is fine, but as soon as you convert them to the
error
interface type, nil checks will fail.See the golang FAQ for information why the checks fail.
https://golang.org/doc/faq#nil_error
Example
read.go
The ReadSchema function may return io errors and graphql errors. Because of this I can't use
*gqlerror.Error
as return type. When testing this I noticed that my nil checks failed.read_test.go
It's kind of strange that gqlparser returns errors as struct types. I've never seen anywhere else. All other go packages (including the internal ones) use the
error
interface type.I think this should be changed so all methods use the
error
interface when returning errors.The text was updated successfully, but these errors were encountered: