question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return data inside GraphQL Errors

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
iartemievcommented, Jan 13, 2021

I’m not using AppSync but a custom GraphQL endpoint with API Gateway and Lambda.

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 when errors.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.

1reaction
jagonzalrcommented, Jan 14, 2021

@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:

export const handler = async event => {
  try {
    const body = JSON.parse(event.body)
    const reply = await executeGraphql(body)
    return { statusCode: 200, reply }
  } catch (err) {
    return { statusCode: 400, err }
  }
}

Thank you very much for all your help.

I will be closing this ticket now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found