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

InMemoryCacheConfig interface has no cacheRedirects property #5983

Closed
alexandr2110pro opened this issue Feb 23, 2020 · 9 comments
Closed

InMemoryCacheConfig interface has no cacheRedirects property #5983

alexandr2110pro opened this issue Feb 23, 2020 · 9 comments
Assignees
Milestone

Comments

@alexandr2110pro
Copy link

alexandr2110pro commented Feb 23, 2020

Intended outcome:

I am trying to use cache redirects as recommended in Apollo Client 3.0 documentation
and I guess it should work as as in the docs. Or docs should change to match the actual implementation

Actual outcome:

The tsc complains that InMemoryCacheConfig has no cacheRedirects property.

Screenshot 2020-02-23 at 19 33 14
Screenshot 2020-02-23 at 19 33 30
Screenshot 2020-02-23 at 19 33 35

How to reproduce the issue:

Install @apollo/client at version 3.0.0-beta.37, follow the documentation, see the error.
Versions

    @apollo/client: ^3.0.0-beta.37 => 3.0.0-beta.37 
    apollo: ^2.23.0 => 2.23.0 
    apollo-link-error: ^1.1.12 => 1.1.12 
@benjamn benjamn self-assigned this Feb 24, 2020
@benjamn benjamn added this to the Release 3.0 milestone Feb 24, 2020
@benjamn
Copy link
Member

benjamn commented Feb 24, 2020

The cacheRedirects API has been removed in AC3, since its use cases are much better realized using field policies (and specifically read functions that return Reference objects): https://deploy-preview-5677--apollo-client-docs.netlify.com/docs/react/v3.0-beta/caching/cache-field-behavior/

Sorry for the misleading documentation! It's still a work in progress (hence the "beta" tag), and we will definitely fix this before the final release.

@alexandr2110pro
Copy link
Author

@benjamn

Sorry for the misleading documentation! It's still a work in progress (hence the "beta" tag), and we will definitely fix this before the final release.

That's understandable. Thanks for the link!

@benjamn
Copy link
Member

benjamn commented Feb 25, 2020

I'll leave this issue open until we fix the underlying documentation issue. Feel free to ask any questions you have about the new API in the meantime!

@cbergmiller
Copy link

@benjamn could you provide a minimal FieldPolicy example for a User query that should return a UserType object by ID, so that the user object is retrieved from the cache if it is found there? I am not sure how to instantiate a Reference object.

@benjamn
Copy link
Member

benjamn commented Feb 27, 2020

Something like this?

new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        user(existingDataFromCache: Reference | undefined, { args, toReference }) {
          return existingDataFromCache || toReference({
            __typename: "User",
            id: args.id,
          });
        },
      },
    },
  },
})

I think the piece you're missing is options.toReference (and options.args perhaps).

You don't necessarily have to pay any attention to existingDataFromCache, if this is a purely client-only field, meaning you won't be writing any Query.user data from the server into the cache:

new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        user(_, { args, toReference }) {
          return toReference({
            __typename: "User",
            id: args.id,
          });
        },
      },
    },
  },
})

@julia-suarez-deel
Copy link

Hello, I'm a bit confused with the new API, is there a way I can do something like "look in the cache for this id (I suppose this part is working with the provided example), if it's not there, then query the server"? I always wanted this behavior in the old version of apollo but right now I haven't been able to achieve it. For example, sometimes you will go to the list page of a CRUD and then you have all the items in the cache when you go to the detail page, but if you go to the detail page of an item directly (no list page first) then the details of the item are not in the cache and I want to query directly. Can you guide me on how to achieve this?

@darkbasic
Copy link

This is what the previous example is about... Basically if you return undefined it will fetch from the server, otherwise it will use the returned result.

@julia-suarez-deel
Copy link

Oh great, thanks for explaining :) I wasn't sure if this was the default behavior.

@hwillson
Copy link
Member

Updated docs showing the removal of cacheRedirects is available in #6429, and will be published soon. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants