2

I'm using the GraphQL Apollo Client. And I'm trying to retrieve the results from a query with errors.

GraphQL response

How do I retrieve the data, even when an error is returned in the same response?

This automatically catches the error. Instead, I want it to access the data.

this.client.query({
  query: gql(query),
  variables: variables
}).then(function ({ data }) {
  return data;
}).catch(error => console.log(error));

Couldn't find anything in Apollo docs about this. Any ideas?

  • Can you include a snippet of your schema? Because the error message indicates that `custom_attributes` is of a `String` scalar type. – Hasan Sh Nov 07 '18 at 20:45
  • This is not really relevant for me anymore, but if you want to ignore errors in Apollo you could probably use the errorPolicy option. See https://www.apollographql.com/docs/react/features/error-handling.html#policies for more details. – Alexander Schoonderwaldt Nov 09 '18 at 07:58

1 Answers1

1

if you stumble here in future

In Apollo-client there are various error types as follow: 1. GraphQL Errors, 2. Server Errors, 3. Transaction Errors, 4. UI Errors, 5. Apollo Client Errors. As @ Alexander Schoonderwaldt you can learn about this errors and error policy here

You can handle/catch graphql errors using code below. If the values returns undefined, its no longer a graphql error. You need to investigate. You may have network error that returns Error CONNREFUSED or others.

    .query({
      query: myquery
    })
    .then(({ data }) => {
      console.log(data.user);
      return { loggedInUser: data };
    })
    .catch(errors => {
      // Fail gracefully
      console.log(errors.message); // log grapgql error
      return { loggedInUser: {} };
    });



Ngatia Frankline
  • 2,897
  • 2
  • 20
  • 19