InMemoryCache: Data with null arguments not merged with data without arguments in
See original GitHub issueIntended outcome:
- write to cache for
items { ...}
with 1 item - write to cache for
items(foo: $foo) { .. }
with 2 items, with$foo
beingnull
orundefined
- read form cache for
items { ... }
- Expect result is 2
Actual outcome:
Result is 1
How to reproduce the issue:
https://codesandbox.io/s/vibrant-sun-fkcn5?file=/src/index.jsx:129-142
import { ApolloClient, InMemoryCache, gql } from "@apollo/client";
const client = new ApolloClient({
cache: new InMemoryCache()
});
client.writeQuery({
query: gql`
query {
items {
id
}
}
`,
data: {
items: [
{
id: 1,
__typename: "Item"
}
]
}
});
client.writeQuery({
query: gql`
query($foo: Int) {
items(foo: $foo) {
id
}
}
`,
data: {
items: [
{
id: 1,
__typename: "Item"
},
{
id: 2,
__typename: "Item"
}
]
}
});
const { items } = client.readQuery({
query: gql`
query {
items {
id
}
}
`
});
console.log(items.length);
In the app I’m working on. I can see from Apollo devtools that these two entries are lists using different keys.
Versions
"@apollo/client": "^3.4.13",
"graphql": "^15.6.0",
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
class InMemoryCache - Apollo GraphQL Docs
Writes data to the cache in the shape of a provided GraphQL query. ... merge functions, allowing incoming data to replace existing data,...
Read more >Data not merging, Apollo 3 pagination with field policies
Take a look at merging arrays of non-normalised objects in the Apollo Client documentation. You need to set a field policy for the...
Read more >Understanding Caching in Apollo Client 3 by Laura Beatris.
Learn GraphQL with Apollo Odyssey, our hands-on training and tutorial course platform - https://odyssey.apollographql.com/Learn from our ...
Read more >Apollo Cache in a Nutshell - E.Y. - Medium
An array of key arguments that help the cache avoid storing unnecessary duplicate data. The most important is the read & merge function....
Read more >Configuring the Cache – Angular - GraphQL Code Generator
To learn how to interact with cached data, see Reading and writing data to the ... ...other arguments... cache: new InMemoryCache(options) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@louisholley sadly not. Kinda like a workaround I’ve changed my code to always include an argument (in my case
limit: 20
for paging). Then combined with “null-arg” being set toundefined
.In short:
reads the same cache as
@layerssss did you ever find a solution to this?