Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

onError giving me terminating link and hence, from throws LinkError #1259

Open
joelbiffin opened this issue Mar 25, 2020 · 0 comments
Open

Comments

@joelbiffin
Copy link

joelbiffin commented Mar 25, 2020

I'm using apollo-link-error in combination with apollo-link-http and apollo-link. I'm having issue when testing my withApollo function.

When running the code in the browser, I get the desired effects - successful logging to Sentry when it's supposed to only.

When running the jest test which worked successfully before adding the onError middleware, I received a terminating link warning.

As far as I was aware, onError is not a terminating link - so I am unsure why I receive this warning and furthermore why this fails my unaltered test?

In my test I am mocking out apollo-link-error in __mocks__ like so:

const apolloLinkError = jest.genMockFromModule('apollo-link-error');

apolloLinkError.onError = (callback) => {
  const fakeLink = function(response) {
    return callback(response);
  };

  fakeLink.concat = (next) => next;
  return fakeLink;
};

module.exports = apolloLinkError;

Initially I was using a standard jest.mock('apollo-link-error') but realised I would need to be more specific.

Expected Behavior
To raise no warning message and for onError to produce a non-terminating link

Actual Behavior

Error: You are calling concat on a terminating link, which will have no effect

A simple reproduction
The structure of my code is:

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import withApollo from 'next-with-apollo';
import { createHttpLink as CreateHttpLink } from 'apollo-link-http';
import { onError } from 'apollo-link-error';
import { ApolloLink, from } from 'apollo-link';

const withApollo((args) => {
  let links = [];

  if (isServer) {
    const afterwareLink = new ApolloLink((operation, forward) => forward(operation).map(responseHandler);
    links.push(afterwareLink);
  }

  const errorHandler = ({ graphQLErrors, networkError, operation }) => {
    if (networkError) Sentry.captureMessage(networkError);
    else if (!!graphQLErrors.length) Sentry.captureMessage(graphQLErrors);
  }

  links.push(onError(errorHandler));
  links.push(new CreateHttpLink(linkConfig));

  return new ApolloClient({ ssrMode: isServer, link: from(links), cache: new InMemoryCache().restore(state)});
})
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant