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

How to use 2 terminating Links? #1280

Open
vendramini opened this issue May 29, 2020 · 0 comments
Open

How to use 2 terminating Links? #1280

vendramini opened this issue May 29, 2020 · 0 comments

Comments

@vendramini
Copy link

Hello everyone.

I read the docs about terminating links, but what about the situations which we need to use the behavior of different terminating links?

My case is: I'm using apollo-upload-client for upload and I would like to use apollo-link-batch-http to improve performance request.

So this code says that I can't have 2 terminating links:

export default function createApolloClient(initialState, ctx)
{
    // The `ctx` (NextPageContext) will only be present on the server.
    // use it to extract auth headers (ctx.req) or similar.
    const httpLink = createUploadLink({
        uri: `${process.env.NEXT_PUBLIC_API_URL}/graphql`,
        credentials: 'same-origin',
        fetch,
    });

    const batchLink = new BatchHttpLink(
        {
            uri: `${process.env.NEXT_PUBLIC_API_URL}/graphql`,
            credentials: 'same-origin',
            fetch,
        }
    );

    const authLink = setContext((_, { headers }) =>
    {
        // get the authentication token from local storage if it exists
        const token = Cookies.get('token');
        // return the headers to the context so httpLink can read them
        return {
            headers: {
                ...headers,
                authorization: token ? `Bearer ${ token }` : "",
            }
        }
    });

    return new ApolloClient({
        ssrMode: Boolean(ctx),
        link: from([authLink, batchLink, httpLink]),
    })
}

What is the best approach in this kinda of problem?

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