Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context link is not capturing reactive var changes #7338

Closed
sergio-bravo opened this issue Nov 17, 2020 · 2 comments
Closed

Context link is not capturing reactive var changes #7338

sergio-bravo opened this issue Nov 17, 2020 · 2 comments
Assignees

Comments

@sergio-bravo
Copy link

sergio-bravo commented Nov 17, 2020

I created a language link:

const languageLink = setContext((request, previousContext) => ({
  ...previousContext,
  headers: {
    ...previousContext.headers,
    "Accept-Language": getLanguage(),
  },
}));
export const getLanguage = (defaultLanguage?: string): string => {
  return languageVar() || defaultLanguage || "en";
};

It basically works and I can see the header is added to the request.

Problem: I expect cached queries to update automatically because the context has changed, but the new queries do not fire.

benjamn added a commit that referenced this issue Nov 20, 2020
In the context of a larger cache read operation, accessing a reactive
variable automatically associates the variable with the cache, so future
updates to the variable can be broadcast to the cache.

Outside this context, it may not be possible for the variable to discover
the currently active cache. For example, there is no currently active
cache in asynchronous ApolloLink code (#7338).

Since we recently added a `reactiveVar.forgetCache(cache)` in #7279, I
think it makes sense to have a manual way to do the opposite, which I have
decided to call `reactiveVar.attachCache(cache)`.

Although this attachment is manual at the moment, in a hypothetical future
where the client/cache manage the persistence of reactive variables, it
should be easy to attach the appropriate cache to those variables when
they are first initialized, which would enable reactive updates from
literally anywhere, with no additional effort by the developer.
@benjamn benjamn self-assigned this Nov 20, 2020
benjamn added a commit that referenced this issue Nov 20, 2020
In the context of a larger cache read operation, accessing a reactive
variable automatically associates the variable with the cache, so future
updates to the variable can be broadcast to the cache.

Outside this context, it may not be possible for the variable to discover
the currently active cache. For example, there is no currently active
cache in asynchronous ApolloLink code (#7338).

Since we recently added a `reactiveVar.forgetCache(cache)` in #7279, I
think it makes sense to have a manual way to do the opposite, which I have
decided to call `reactiveVar.attachCache(cache)`.

Although this attachment is manual at the moment, in a hypothetical future
where the client/cache manage the persistence of reactive variables, it
should be easy to attach the appropriate cache to those variables when
they are first initialized, which would enable reactive updates from
literally anywhere, with no additional effort by the developer.
@hwillson
Copy link
Member

Addressed by #7350 - thanks!

@codazzo
Copy link

codazzo commented Dec 20, 2021

I suspect this issue may not have been fixed, as suggested in #7350 (comment)

I have created a reproduction repo at https://github.com/codazzo/apollo-client-reactive-context-repro
What do you think @hwillson? I might be missing something.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
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

4 participants