I'm working through a GraphQL Node/Prisma server tutorial and encountered an error due to something wrong in my code. I've solved the error but I want to understand the error message, in particular, what does locations
refers to? That is, I have a location
of line 2, column 3, but line 2, column 3 of what? The relevant method in my code (signup
, in this case)? Of my mutation?
// error message
{
"data": {
"signup": null
},
"errors": [
{
"message": "secretOrPrivateKey must have a value",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"signup"
]
}
]
}
Also of note here is that the errors array appears before the data. This is a recommendation from the GraphQL spec and a good one if you can implement it. When reading a response, it is very easy to miss that there are errors when the data is large.
These partial failures lead to a responsibility on the consumer to always check for errors in the response, even when data are returned. Firstly we must understand what the GraphQL spec says about errors. Let's look at some examples. The above shows the simplest allowable error response from a GraphQL API.
Let's take a typical GraphQL query: The response of the above query can have the following objects: both. When the response contains the errors object along with the data object, it could mean a partially correct response for the request. Digging into the errors object will give a better idea into what part of the query went wrong.
graphql-java provides the GraphQLError interface representing a GraphQL standard error that our exceptions will implement. Overriding getLocations () and getErrorType () is mandatory. We will just return null in these methods because they are ignored by the default error handler anyway.
Just like path
, locations
refers to where inside your GraphQL document the error occurred. You can see this for yourself using something like the SWAPI GraphQL endpoint. We can trigger a validation error by requesting a field that doesn't exist:
{
allFilmz
}
The resulting error has this locations
array:
[
{
"line": 2,
"column": 3
}
]
That's because the offending field is on line 2, starting with column 3. If we instead send:
{allFilmz}
we get:
[
{
"line": 1,
"column": 2
}
]
Generally, the path
of the error will be more informative than the locations
, but the path
won't exist when there are syntax errors in your document -- in those cases, the locations
are the only thing you can use to track down where your syntax error occurred.
Word of caution if using GraphQL Playground -- unlike GraphiQL, Playground will strip out comments and format your request before sending it, so the locations
may not match what you see in the Playground UI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With