apollo-link-error: graphQLErrors param is always undefined

Created on 19 Jan 2018  ·  4Comments  ·  Source: apollographql/apollo-link

Intended outcome: Logging graphQLError messages to the console

Error messages returned from failed queries never seem to make it to onError() as graphQLErrors is always undefined, but I'm always given a networkError. I can find the failed request with the response error messages in chrome's network tab. Am I missing something?

// Log errors
const errorLink = onError(({ 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}`);
});

// Combine all links
const link = split(
  // split based on operation type: subscription or query/mutation
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },

  // Subscriptions Link
  from([
    // Log Apollo operations in development
    ...(__DEV__ ? [apolloLogger] : []),

    // Handle subscriptions, log errors in dev
    ...(__DEV__ ? [errorLink.concat(subscriptionsLink)] : [subscriptionsLink]),
  ]),


  // Uploads/http Link
  from([
    // Log Apollo operations in development
    ...(__DEV__ ? [apolloLogger] : []),

    // Handle http/file uploads, log errors in dev
    ...(__DEV__ ? [errorLink.concat(uploadLink)] : [uploadLink]),
  ]),
);

bug

Most helpful comment

Any progress on this ?

All 4 comments

same probleme here. We would like to have a global onError function.
Currently we are passing onError function to every subscribeToMore function:

this.unsubscribe = data.subscribeToMore({
        document: ...,
        variables: {...}       
        updateQuery: (previous, { subscriptionData }) => {
          ...
        },
        onError: err => {
          console.log({ err })
        },
      })

@goldo exactly, that pattern doesn't scale well when you really just want to log everything, hence the error link!

What I find confusing about the docs is this code:

const withDataAndErrors = graphql(MY_MIXED_QUERY, {
  options: {
    errorPolicy: 'all'
  }
});

Does the error policy on a per-query basis somehow impact how onError() gets it's messages? Or maybe there are some new params to GraphQL-Tools/Apollo Server to enable error logging?

@tim-soft is it possible the fetch library that you're using is treating HTTP 400 status as a network error? I've come across a few fetch modules like that.

Any progress on this ?

Was this page helpful?
0 / 5 - 0 ratings