问题
Take the following queries:
query Foo {
foo {
id
bar(id: 1) {
id
baz
}
}
}
query Bar {
bar(id: 1) {
id
baz
}
}
Sometimes, running the 2nd query gets me a cached version of bar
. Other times, it doesn't, but I'm not sure if this is because the query is run multiples times or because that's the default behaviour of the Apollo client in React.
回答1:
No, it doesn't (as of Nov 2019, at least). To put the bar
object in the cache when running the Foo
query, you need to create the in-memory cache like this:
import { InMemoryCache } from 'apollo-cache-inmemory';
const cache = new InMemoryCache({
cacheRedirects: {
Query: {
bar: (_, args, { getCacheKey }) =>
getCacheKey({ __typename: 'Bar', id: args.id })
},
},
});
Also see:
- Configuring the cache
- Cache redirects
- Why do we need cacheRedirects at all?
来源:https://stackoverflow.com/questions/58842929/does-the-apollo-client-cache-nested-objects-in-react