12

I have a GraphQL query that calls a REST service to get the return object. The query contains an Id parameter that is then passed to the service. However, the REST service can respond with http status 404 Not Found if an object with that Id does not exist. That seems like the right response.

How do you model a Not Found response in GraphQL? Is there a way to inform the GQL caller that something does not exist?

Update
Some options I am considering:

  • Return null
  • Change the GrqlhQL Query to return a list of objects and return empty list of nothing is found
  • Return some kind of error object with an error code

but it is unclear if there is a recommended practice in GQL API design.

1 Answer 1

3

You might treat it as an error and handle it accordingly.

I recommend you to check the GraphQL spec, the paragraph about error handling.
I hope it contains exactly what you are looking for.
Basically, you should return whatever you could, and inform a client about potential problems in the "errors" field.

The example from the documentation:
Request:

{
  hero(episode: $episode) {
    name
    heroFriends: friends {
      id
      name
    }
  }
}

Response:

{
  "errors": [
    {
      "message": "Name for character with ID 1002 could not be fetched.",
      "locations": [ { "line": 6, "column": 7 } ],
      "path": [ "hero", "heroFriends", 1, "name" ]
    }
  ],
  "data": {
    "hero": {
      "name": "R2-D2",
      "heroFriends": [
        {
          "id": "1000",
          "name": "Luke Skywalker"
        },
        {
          "id": "1002",
          "name": null
        },
        {
          "id": "1003",
          "name": "Leia Organa"
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, it makes sense! I am wondering though how a client would distinguish between different type of errors and act accordingly. - There might be a network or database error on the REST service. - Or the client might be requesting data that does not exist. In each scenario a client (say mobile app) needs to react differently.
@Andrejs the responsibility of the backend is to provide enough information for the client to distinguish these cases. If "message" and "path" it is not enough, you could use "extensions" entry and provide a code, a timestamp, a list of developers liable for this issue, the phone of your manager's grandma and whatever else you want (spec.graphql.org/draft/#sel-IAPHRLZBABABKonO)
Aha, extensions seems like the way to be explicit about the error type. Thanks for that!
but "not found" is not an error rather than a success response with no data in it, therefore i dont think is a good idea to consider it as an error.
@MohamedAarab I'd say "Not found" is an error. You expect something to be there but it isn't. What we're trying to replicate here is 404 - Not found which falls under errors. It might help the client understand why something is missing rather than actually having the value null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.