question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`InMemoryCacheConfig` interface has no `cacheRedirects` property

See original GitHub issue

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 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
benjamncommented, 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,
          });
        },
      },
    },
  },
})
2reactions
benjamncommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the Apollo Client cache - Apollo GraphQL Docs
This article describes cache setup and configuration. To learn how to interact with cached data, see Reading and writing data to the cache....
Read more >
When to use Apollo's cacheRedirects? - Stack Overflow
It seems obvious and intuitive that given a query like the one below that we're fetching a single Book object by it's id...
Read more >
apollo-cache-inmemory@1.6.6 - jsDocs.io
Documentation for npm package apollo-cache-inmemory@1.6.6 - jsDocs.io.
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
As of Apollo Client 3.0, the InMemoryCache class is provided by the @apollo/client package. No additional libraries are required.
Read more >
Basics of Caching Data in GraphQL Apollo React Client
no -cache policy does not read, nor does it write to the cache with the response. It will always make a request using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found