ApolloLink().split not working - [Network error]: Invariant Violation: request is not implemented

Hi folks,
I’m working on implementing my first split link.

Currently, we use BatchHttpLink which looks like this:

  const httpLink = new BatchHttpLink({
    uri: API_URI,
  });

To have a split link, I refactored it to something like this:

  const httpLink = new ApolloLink().split(
    (operation) => true,
    new BatchHttpLink({
      uri: API_URI,
    }),
    new BatchHttpLink({
      uri: API_URI,
    })
  );

However after doing that, queries do not work anymore. In console, Apollo Client logs:

[Network error]: Invariant Violation: request is not implemented

@apollo/client version: 3.5.6

Does anyone have an idea what might be going wrong here? Thanks

Hello! What you have here should work fine, and I bet there’s a minor syntax error sneaking around. I’ve forked one of the Apollo code sandboxes to use this logic and it seems to run without any issue. You might be able to identify your issue by comparing it against your source :crossed_fingers:

Hey Stephen,
thanks for your reply. It turned out that I used new ApolloLink().split and you used new ApolloLink.split. Yours works, but it gives me a TypeScript error:

Aha you’re right! I goofed, and I’ve checked the source, which has shown a great opportunity to expand the docs.

Your original issue was due to the fact that if you initialize a plain old ApolloLink instance (as opposed to a predefined subclass such as RetryLink), you need to provide a request handler function to the constructor. Your link chain consists of two links: first the empty ApolloLink, followed by the split link created by calling ApolloLink().split().

It turns out, if you want your link chain to start with a split link, you can call ApolloLink.split() instead of new ApolloLink().split(). This is because ApolloLink defines two split methods, one of which is static. The static method returns a split link with no unnecessary empty ApolloLink in front of it, which is what my example was (accidentally but helpfully) doing.

All of that to say is, if you cut the new from the line you have there, you should get exactly what you need: a split link at the start of your link chain.

1 Like