Apollo-client: How to compose wss and upload links?

Created on 23 Oct 2018  路  3Comments  路  Source: apollographql/apollo-client

Hi, there!

I'm trying to combine upload-link and websocketlink using apollolink.

import { ApolloClient } from 'apollo-boost';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { WebSocketLink } from 'apollo-link-ws';
import { createUploadLink } from 'apollo-upload-client';
import { concat } from 'apollo-link';

import { apiPath, wssPath } from './apiPath';

const client = new ApolloClient({
    link: concat(
        new WebSocketLink({
            uri: `${ wssPath }/graphql`,
            options: { reconnect: true }
        }),
        createUploadLink({ uri: `${ apiPath }/graphql` })
    ),
    cache: new InMemoryCache()
});

export default client;

but i'm receiving error from uploadlink and from apollo (You are calling concat on a terminating link, which will have no effect).

Please, help me..

Most helpful comment

Thanks! I found answer.

In my case that works:

import { ApolloClient } from 'apollo-boost';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { WebSocketLink } from 'apollo-link-ws';
import { createUploadLink } from 'apollo-upload-client';
import { RetryLink } from 'apollo-link-retry';
import { getMainDefinition } from 'apollo-utilities';

const client = new ApolloClient({
    link: new RetryLink().split( // YOHOOOOOO!!! FIXED
        sys => {
            let { operation } = getMainDefinition(sys.query);
            console.log(operation);
            return operation === "subscription";
        },
        new WebSocketLink({
            uri: `${ wssPath }/graphql`,
            options: { reconnect: true }
        }),
        createUploadLink({ uri: `${ apiPath }/graphql` })
    ),
    cache: new InMemoryCache()
});

All 3 comments

See terminating links in the docs.

I don't really know the answer, but you can use two different terminating links by using ApolloLink.split; see Composing Links - Directional Composition.

Thanks! I found answer.

In my case that works:

import { ApolloClient } from 'apollo-boost';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { WebSocketLink } from 'apollo-link-ws';
import { createUploadLink } from 'apollo-upload-client';
import { RetryLink } from 'apollo-link-retry';
import { getMainDefinition } from 'apollo-utilities';

const client = new ApolloClient({
    link: new RetryLink().split( // YOHOOOOOO!!! FIXED
        sys => {
            let { operation } = getMainDefinition(sys.query);
            console.log(operation);
            return operation === "subscription";
        },
        new WebSocketLink({
            uri: `${ wssPath }/graphql`,
            options: { reconnect: true }
        }),
        createUploadLink({ uri: `${ apiPath }/graphql` })
    ),
    cache: new InMemoryCache()
});

the above paradigm should probably be added to the documentation.

Was this page helpful?
0 / 5 - 0 ratings