-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Subscription events trigger queries to be repeated unexpectedly when subscription returns same type of object but with subset of fields #8125
Comments
@insidewhy Thanks for reporting issues as you attempt to update to Apollo Client 3, instead of suffering in silence! In this case, you've hit on a common pitfall with list fields that are populated by multiple operations (queries/subscriptions/mutations), which can happen when one of the operations adds objects to the list that are missing some of the properties expected by other consumers of the list, leading to incomplete cache reads for those other consumers, leading to refetching in many cases (depending on I think the ultimate solution to this problem will involve some form of query transformation behind the scenes to ensure a given selection set includes all fields that might be needed by other consumers of that object. On a superficial level, this goes against the GraphQL principle of only fetching what you need, but I believe it would solve deeper problems, like this one. In the meantime, you have a couple of workaround options:
new InMemoryCache({
typePolicies: {
Quality: {
fields: {
cuddliness(existing = "not cuddly") {
return existing;
},
},
},
},
}) This will ensure the |
@benjamn I see, in several of our cases we have another query that only needs to fetch the In our case, objects aren't added to the list most of the time. When we have no objects added to the list, and the second request is for a subset of fields, re-querying shouldn't be necessary? Could this optimisation be added to the code? It's something we need so bad, that it's something we could contribute to apollo. |
Wouldn't it make sense for a request to not try to get fields it never required? I think we are experiencing a similar issue, we have a first request that get a list of people without much details (eg. picture and name), then, we trigger a request to get the detail of a single person (many fields, including picture and name). This second request seems to trigger a refetch of the first (I feel its similar to what is experienced with cats here, no?). In our case the list is never mutated at any time while these refetch happens. |
@cglacet Yep that's exactly it, even when the subscription doesn't add an element to the list, or even change the list in any way, the query still refetches. If the subscription added a new element to the list with less fields than the query requires, it's understandable. When the subscription doesn't change the list in any way, the re-query is completely unnecessary. |
So the logic behind the current implementation is to refetch a query when it seems like a field is missing? In other words, if the cache looks like this: {
"ROOT_QUERY": {
"findCats": {
" __ref": "Cat:1",
"__ref": "Cat:2"
},
"findCat({'id': 1})": {
"__ref": "Cat:1"
}
},
"Cat:1": {
"name": "name1",
"color": "color1"
},
"Cat:2": {
"name": "name2"
}
} The |
@cglacet My situation is like this... after the query {
"Cat:1": {
"__typename": "Cat",
"_id": "Mr Sweetkins",
"qualities": [{
"__typename": "Quality",
"_id": "loveablity",
"cuddliness": "so loveable"
}]
} Then after a subscription update, this cache entry might not get updated at all. since the subscription supplies this: {
"Cat:1": {
"__typename": "Cat",
"_id": "Mr Sweetkins",
"qualities": [{
"__typename": "Quality",
"_id": "loveability"
}]
}
} So after this update, the cache is identical. Yet still the query is refetched. I guess it's because the subscription only requests |
Ah ok, I see, its not the exact same case, but maybe the issue is the same. Let's wait and see 👍 |
Heh |
We have a query and related subscription that look like this:
i.e. the only difference between the fields requested from the query and subscription is that the subscription lacks
qualities.cuddliness
. Both of these return objects with__typename
ofCat
.Now we have two hooks happening in the same component:
Now, every time the server sends a new update to the subscription, the query is sent again, which is completely redundant. 10 updates to the subscription = 10 queries sent to the server. With apollo client 2 this worked as we expected, subscription events would not cause new queries to be sent to the server.
If we remove
cuddliness
fromQUERY
(so that the query is no longer requesting a superset of the fields in the subscription) then this behavior goes away and we only ever get one query sent.This is causing a lot of unnecessary queries to be sent from our client to our server since upgrading to apollo 3.
The text was updated successfully, but these errors were encountered: