How to get error body using React Apollo Link Rest

2.4k Views Asked by At

I am creating an React app using Apollo Graphql and I am having some trouble with the Apollo Link Rest. The problem is that in the middle of my app I have to make a REST request, however this request can be sucessful and return a status code = 200 or it can fail, returning a status code = 400.

It is important for my app to show the end user an error message that is returned in this request's body in case of a 400 error. However when an error happens the Apollo Link Rest just throws an exception, but it doesn't return response body back to me.

Is there a way for me to get the response body, when there is an error with the Apollo Link Rest? I thought that I could get it from the result variable, but since the Apollo throws an exception, this variable never changes.

Here is my code:

    const result = await context.client.query<MyQuery>({
      query: MyQuery,
      variables: {
        input: {
          companyId: variables.companyId,
        },
      },
    });
query MyQuery($input: MyQueryInput!) {
  myQuery(input: $input) @rest(
    type: "QueryResponse",
    method: "POST"
    path: "v1/my-query"
  ) {
    id
    minimumPreparationTime
    maximumPreparationTime
    extras {
      id
      optional
      key
    }

    warning
    error {
      id
      type
      message
    }
  }
}
2

There are 2 best solutions below

4
Greg Brodzik On

You can use apollo-link-error to catch and handle server errors, network errors, and GraphQL errors. You should be able to conditionally set response.errors if needed.

    import { onError } from "apollo-link-error";

    const link = onError(({ graphQLErrors, networkError, response, operation }) => 
    {
       if (graphQLErrors)
       graphQLErrors.map(({ message, locations, path }) =>
           console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: 
            ${path}`,
          ),
        );

     if (networkError) console.log(`[Network error]: ${networkError}`);

    response.error = // set error property here 
});

It may also be worth noting Apollo exposes an errorPolicy option, for which the default is none. The policy can be modified to all which, according to the docs,

is the best way to notify your users of potential issues while still showing as much data as possible from your server. It saves both data and errors into the Apollo Cache so your UI can use them.

0
Martin Freytes On

Extending Greg comment and apollo-link-error, you can retrieve the error body from networkError.result.message.