-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Race condition for similar queries, where the merge isn't reached #10359
Comments
Hi @tim-rellify 👋🏻 thanks so much for opening this issue! As you can imagine, verifying this particular behavior may be quite time-consuming for the team so it's not likely we'll be able to take a look at this in the near future. If we had a runnable reproduction, though, it may help expedite a resolution. I had one question:
Can you elaborate on that, preferably with the |
Sure. And thanks for the response! :-) |
I could workaround the issue by calling the "reobserve" function of the watch query in a useEffect, if data is undefined and the watch query was called and loaded. After debugging the apollo client itself, I could observe, that the internal merge functionality seems to be processed correctly. |
any updates? found same issue in same condition: multi different queries inflight which with different DocumentNode but same schema, this schema have individual cache(type policy), sometimes the cache not update after all queries completed. type Test = {
a: Record<string, string> | null
b: Record<string, string> | null
c: Record<string, string> | null
}
const documentA = gql`
query test() {
a
b
}
`
const documentB = gql`
query test() {
a
c
}
` query results: // query a
{
a: {}
b: {}
}
// query b
{
a: {}
c: {}
} origin cache: {
a: null
b: {}
} cache after queries complete: // nothing changed
{
a: null
b: {}
} |
Were these the only requests for your type "Test"? If no, did you set
Because otherwise, another request would overwrite the response of another. By default, these aren't merged! |
thanks for reply~~ first question is yes, for the type policy, i have tried two types
merge(existing, incoming, opts) {
// this is business logic to distinguish two queries(just like query a/b modify same cache) by variables
console.log(
(opts.variables?.filter ? 'activity' : 'collection') +
' merge-----------------'
);
console.log(existing, incoming, '-----------------');
return opts.mergeObjects(existing, incoming);
} |
OMG!!!!! i have two queries, very simple: const Page = () => {
useA(); // same __typename, different documentNode
useB(); // same __typename, different documentNode
return null
} and console the merge function call, there are three behaviors when first loaded:
maybe is the cache system bug(the __typename schema had a individual type policy) |
see more in #10587 , found the root cause |
After some minor tests and removing the workaround, this seems to be solved with #10587 / version 3.7.11. Thanks to everyone, involved here! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Intended outcome:
I'm working on a react application with several components, that execute different queries for the same type but with different fields in parallel (on load of the whole react application).
To merge the different fields of the different query responses for the same type, "merge: true" is used as type policy for the affected type when initializing the apollo in memory cache.
A simple example:
type Article {
id
name
url
content
}
And I want to implement a NameComponent, UrlComponent and ContentComponent in react. Each of these components requests the corresponding field in a separate query with the help of a watch query hook.
After all components are loaded and all hooks return a data value, the Apollo cache should contain the article with all requested fields.
{
id: 123
name: 'My article'
url: 'github.com'
content: 'Cool article about github'
}
Actual outcome:
Generally, this runs fine. But sometimes, the response of one of the queries is missing in the cache.
How to reproduce the issue:
I couldn't reproduce the above described issue in very simple use cases. In my use case, I run about 5 different queries for the same type along with other queries for other types. But I observed the following:
I also thought, that this might be related to #9502, but that didn't have any effect after I applied the changes there.
Versions
System:
OS: Windows 10 10.0.22000
Binaries:
Node: 18.12.1 - C:\Program Files\nodejs\node.EXE
Yarn: 3.3.0 - ~\Documents\GitHub\myProject\node_modules.bin\yarn.CMD
npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22000.120.0), Chromium (108.0.1462.42)
npmPackages:
@apollo/client: ^3.7.2 => 3.7.2
The text was updated successfully, but these errors were encountered: