Return data inside GraphQL Errors
See original GitHub issueIs your feature request related to a problem? Please describe.
When making a GraphQL call using API.graphql
if there is an error we can’t get the actual error because the data
value is and empty object.
Describe the solution you’d like When returning the error add the data from the response that comes from Axios.
Describe alternatives you’ve considered
I’ve considered changing the status code when certain errors happen to 200 and manually handle those cases on the frontend. By doing this we wouldn’t need to do that since the error message will be available on the catch
Additional context
try {
const operation = graphqlOperation(query, params)
const response = await API.graphql(operation)
return response
} catch (err) {
// err doesn't include the actual error from GraphQL
return err
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Error handling - Apollo GraphQL Docs
Built-in error codes ; GRAPHQL_VALIDATION_FAILED. The GraphQL operation is not valid against the server's schema. ; BAD_USER_INPUT. The GraphQL operation includes ...
Read more >How to return both error and data in a graphql resolver?
A resolver can either return data, or return null and throw an error. It cannot do both. To clarify, it is possible to...
Read more >Handling GraphQL errors like a champ with unions and ...
Error handling can be frustrating in GraphQL. This post shows you how to use unions and interfaces to handle errors in a more...
Read more >Error Handling in GraphQL -- newline - Fullstack.io
To add errors to your data, you need to use the Union type (a.k.a. Result ) in your GraphQL schema. Also, the logic...
Read more >A Guide to GraphQL Errors
The general philosophy at play is that Errors are considered exceptional. Your user data should never be represented as an Error. If your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Got it. My apologies for assuming.
For GraphQL servers (AppSync, Apollo server, etc.) the common pattern is to always return a 200 status code. The client then checks whether the
errors
array is non-empty to infer if any GraphQL errors have occurred. The Amplify API category is structured around this assumption.API.graphql
will throw whenerrors.length > 0
and the status code is 200. Any other response will be treated as an Axios network request error as you pointed out in your OP.Typically you’ll only see something other than 200, e.g., 500 when the server itself encounters an error.
@iartemiev I can confirm that using status code 200 instead of 400 the Amplify API category will successfully get the errors array from the backend. For reference the updated Lambda with GraphQL ends up like this:
Thank you very much for all your help.
I will be closing this ticket now.