Hey everyone, i have a create-react-app project with @apollo/client ^3.5.0.
My backend is written with graphene, and when i encounter an error that is not related to graphql or validation (say, a user tried creating a new account when that account already exists) i send a response with status code 200 in the following structure:
"details": "account already exists.",
The problem is that apollo never picks up the error property, it won’t show up in the onError function.
if my backend crashes and i return a 500 status code with the same data structure and when that happens i do get the error property in the onError function.
i read that the status code for a graphql response should be 200, so i’m not sure what i’m missing here.
If you have control over the server response, I’d recommend modifying it to be a valid GraphQL response. When an error is encountered in a GraphQL server, the GraphQL spec expects GraphQL responses to optionally contain a
data key (if the error happened during execution), which might be
null, and an
errors property which is a list of errors. Your response contains an
error property so Apollo is unable to pick this up since the response doesn’t adhere to the GraphQL spec.
Instead, return your error as such:
"message": "account already exists",
Note the use of
extensions here to convey the code-friendly error code.
FYI, Apollo can handle non-GraphQL errors, but it relies on the status code being a non 2xx status code to handle it. Since you’re both returning an invalid GraphQL response and a 200 status code, Apollo is unable to pick it up because it thinks your response is valid.
Hope this helps!