web: Add custom concurrent Apollo link
Created by: vovakulikov
Closes part of https://github.com/sourcegraph/sourcegraph/issues/24236
Context
This PR adds a custom Apollo link that implements concurrency request logic. Previously we used our own custom hook to load BE insights in parallel by two insights at the time - useParallelRequest. With this PR we will load them via this custom Apollo link which allows us to use Apollo cache for the dashboard, directory, and homepage pages.
How it works
Now useQuery
and useMutation
hooks have a typed context.
interface ApolloContext {
concurrent?: boolean;
concurrentKey?: string;
}
With concurrent:true
query will be intercepted by ConcurrentRequestsLink
. This link intercepts all hooks with concurrent:true
and runs them sequentially and in parallel by two. You also can specify concurrentKey: someKey
to group your requests. For example, you want to run 10 requests for one component and 8 requests for the other component and you don't want that request from the second component will be blocked by requests for the first component. You need to specify different concurrentKey
for these components queries and they will be run independently but still sequentially and in parallel by two.
At the moment in PR, we don't use this link for any requests but in the next PR for code insights, we will adopt this link for code insights BE loading.