Vue-apollo: Best way to discover and handle GraphQL errors during a network request?

Created on 20 Mar 2018  路  4Comments  路  Source: vuejs/vue-apollo

Hi there! Thank you for your great work on this project, I have a quick question about the best way to discover GraphQL errors during a network request. Here's the scenario -

If I have the following GraphQL schema:

type Query {
  manifest(pickDate: String!): Manifest
}

And I send a request with an invalid query:

apollo: {
  manifest: {
    query: gql`
      query GetManifest($pickDate: String) { // Improper type for pickDate (should be String!)
        manifest(pickDate: $pickDate) {
          id
        }
      }
    `,
    variables: {
      pickDate: '2017-11-03'
    },
    error(error) {
      console.log(error)
    },
  }
},

I'll get the following error logged from the error callback:

screen shot 2018-03-19 at 6 30 54 pm

The failure of the request is due to the query defining the wrong type for pickDate, using String instead of the expected type String!.

I know this, because if I use apollo-link-error

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

I get the networkError and the graphQLErrors
screen shot 2018-03-19 at 6 29 48 pm

Question: Is there any way to discover specific GraphQL errors inside of the error callback? If not, are there any other ways you suggest to be notified of GraphQL errors from within a component? Could I tie the output of apollo-link-error into Vue somehow?

I've also tried to set errorPolicy to all as mentioned in the docs, but didn't get any changes

Any advice would be a huge help, thank you!

Most helpful comment

Use console.log({ error }) to print the full object to the console (by default the browser outputs only the error message and stack trace). You can then see all the properties of the error object:

image

All 4 comments

Use console.log({ error }) to print the full object to the console (by default the browser outputs only the error message and stack trace). You can then see all the properties of the error object:

image

Awesome!! This is exactly what I was looking for, thank you!

@Akryum This is great to inspect to inspect the error, but I am unsure how can I use this to check the statusCode?

error.networkError.statusCode doesn't work. Any ideas?

error ({ graphQLErrors, networkError }) {
if(networkError.statusCode == 401) {
this.$router.push({ path: "/login" });
}
this.$message(graphQLErrors.message)
}

this work for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alx13 picture alx13  路  4Comments

alsofronie picture alsofronie  路  3Comments

chadwtaylor picture chadwtaylor  路  3Comments

ScreamZ picture ScreamZ  路  4Comments

igaloly picture igaloly  路  3Comments