Skip to content

Instantly share code, notes, and snippets.

View gist:06864f6fedab3a99dfe7bde7906eb07b

In our application, we have a need to wrap Apollo Client, amongst other reasons we do this to pass default link context to operations. This works well for operations originating from the top-level APIs, but it doesn't for the subscribeToMore function that gets created by useQuery.

We could augment useQuery further and also wrap each subscribeToMore function object that's created, but it struck us that the relationship between the useQuery invocation and the created subscribeToMore function might actually imply that it should inherit the link context of the useQuery invocation. Below you can find a naive patch that does this.

// src/core/ObservableQuery.ts  

public subscribeToMore< 
 TSubscriptionData = TData, 
View gist:1393cc81fd229065420a98e82119510f

When the context is patched outside of apollo-client, then subscribeToMore doesn't inherit this context by Query with the current solution.

Our application wraps all queries and extends their context with services and some extra data. However, it isn't possible to easily wrap subscribeToMore, because it is made with its own empty context inside the ObservableQuery. For this reason, when we want to use these data inside the subscription, we are not able to do so.

Currently, the only way is to use links, but would the following change be a valid resolution of this problem ?

// src/core/ObservableQuery.ts