Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'locations' refer to in GraphQL errors?

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"
      ]
    }
  ]
}
like image 517
Cerulean Avatar asked Mar 06 '19 19:03

Cerulean


People also ask

Where does the errors array appear in GraphQL?

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.

What happens when a GraphQL API fails?

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.

How do you know if a GraphQL query is correct?

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.

What is graphqlerror in GraphQL?

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.


1 Answers

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.

like image 86
Daniel Rearden Avatar answered Oct 13 '22 18:10

Daniel Rearden