apollo-link-error networkError undefined

Created on 11 Feb 2018  ·  9Comments  ·  Source: apollographql/apollo-link

networkError is undefined when a network error occurs

[email protected]
[email protected]
[email protected]

The regression happened when upgrading [email protected] to [email protected]

bug

All 9 comments

Thank you for the issue! Can you create a reproduction repo in code sandbox?

Ideally with a fetch that returns mock data.

@evans sure, I can make a codesandox, but can you please explain what do you mean by a fetch that returns mock data?

@bogdansoare Thank you so much! I should have linked you in the first post. Sorry about that. 🤦‍♂️ fetchMock is what we use to test apollo-link-http and I think it would be super useful to the sandbox reproduction.

I'm having this same issue as well. I get networkError undefined

Here is my code.

const errorLink = onError(operation => {
  const { networkError, graphQLErrors } = operation;
  console.log('graphQLErrors', graphQLErrors);
  console.log('networkError', networkError);

  if (networkError) {
    if (networkError.statusCode === 500) {
      console.log('Redirect to Login due to server disconnection.');
      // return logout();
    }

    console.log('networkError', networkError);
    if (networkError.statusCode === 401) {
      console.log('Redirect to Login due to unauthorize request.');
      // return logout();
    }
  }

  if (graphQLErrors && (graphQLErrors.auth === 'Invalid token') || (graphQLErrors.auth === 'Auth token expired') ) {
    // return logout();
  }
});

const AuthLink = (operation, forward) => {
  const token = store.get('auth-token');

  if (token) {
    operation.setContext(context => {
      let headers = context.headers;

      if (token) {
        headers = {
          ...context.headers,
          authorization: `Bearer ${token}`,
        };
      }

      return {
        ...context,
        headers,
      };
    });
  }

  return forward(operation);
};

const httpLink = new HttpLink({ uri: HTTP_URL });

const HTTPLink = ApolloLink.split(
  operation => {
    const operationAST = getOperationAST(
      operation.query,
      operation.operationName,
    );
    return !!operationAST && operationAST.operation === 'subscription';
  },
  new WebSocketLink({
    uri: WS_URL,
    options: {
      reconnect: true, //auto-reconnect
    },
  }),
  httpLink,
  // new BatchHttpLink({ uri: HTTP_URL, credentials: 'same-origin' }),
  env === 'developement' && apolloLogger,
);

const client = new ApolloClient({
  endpoint: HTTP_URL,
  link: from([errorLink, AuthLink, HTTPLink]),
  cache: new InMemoryCache({
    fragmentMatcher,
    dataIdFromObject: object => object.id,
  }),
});

Same here, any news ?

Nothing yet. We are currently hacking it by using graphQLErrors instead and looking for the graphql response, but this isn't ideal.

if (
    graphQLErrors &&
    (graphQLErrors.auth === 'Invalid token' ||
      graphQLErrors.auth === 'Auth token expired')
  ) {
    return logout();
  }

In angular 7, if i add any interceptors. i get this error

Since this issue is really outdated I am closing it but if you are still concerned about this feel free to reopen and I'll get back to you asap.

Was this page helpful?
0 / 5 - 0 ratings