How to use 2 terminating Links?
vendramini opened this issue · comments
André Vendramini commented
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?