graphql: Adding extra fields to errors
Let’s say I have a graphql schema with a field that looks like this:
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return nil, errors.New("Whoops! An error occurred")
},
},
}
The result would be:
{
"data": {
"test": {
"id": null,
}
},
"errors": [
{
"message": "Whoops! An error occurred",
"locations": [
]
}
]
}
What I would like to do is to be able to add extra fields to the errors object. For example:
{
"data": {
"test": {
"id": null,
}
},
"errors": [
{
"message": "Whoops! An error occurred",
"locations": [
],
"field": "hello",
"code": "WHOOPS_ERROR"
}
]
}
It would be really cool if there’s way to be able to add these extra fields into the error message when an error happens.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 18
- Comments: 25 (11 by maintainers)
I think that the proposition of @bbuck is crystal clear 😃
But, to sum up I do not want to use the term “field”, but rather “entries”. From spec:
Because if you are an error between two fields, it’s an entry error no a field error.
Interface
Resolver
@sogko or @chris-ramon can you give your vision/opinion?
(as usual, a big thank you for the work)
Great idea!
I think
logrusis a good place to start with this one.Given the nature of
error(it being an interface and al) we could then do something like this:Usage from a Resolve:
Probably needs some significant cleaning up, or works. I think it’s a simple and clean API.
This is covered by the latest GraphQL spec. Your resolvers can now return errors that implement
gqlerrors.ExtendedErrorto add an “extensions” field with arbitrary contents. For example:The issue has been resolved, so I’m closing it.
The
errorsfield in the GraphQL response is defined in the official spec as follows:Having extra fields is actually addressed in the spec as well:
Would love to start a discussion if / how we want to achieve this.
Cheers!