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

How to use 2 terminating Links? #1280

@vendramini

Description

@vendramini

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions