-
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
Apollo Client 3 reloading query after mutation update #6760
Comments
experiencing the same issue |
I'm experienced something similar, that might be connected to your behaviour. It feels like an errorneous behaviour of the cache. Your mutation is returning data on the response of the request. The query fetchPolicy is set to The core problem is probably the same in my situation. My mutation is not responding with data. Instead of that, I'm just using simple |
@danfernand One of the things that changed in AC3 is that Although I have a specific recommendation to achieve your desired behavior (verified using your reproduction—thanks!), there's a bit of history here that I think is worth understanding. PR #6221 was a big refactoring (released in However, PR #6712 did more than just revert #6353, because Long story short, if you use export default function Todos() {
const { loading, error, data } = useQuery(GET_ALL_TODOS, {
fetchPolicy: "cache-and-network",
nextFetchPolicy: "cache-first",
variables: {
_size: 10
}
});
...
} This means the very first query will read from the cache and make a network request, but subsequent queries will make a network request only if the cache data has become incomplete. |
Thank you for your response! Will give nextFetchPolicy a whirl! Apollo has made using graphql amazing. Great job! |
thanks @benjamn , adding |
I added the Did someone experience something similar or can point me in the right direction? |
@benjamn Can you confirm that such behaviour is expected? Let's say that I have two separate components that run two queries like below: ComponentA query A {
user {
id
role
}
} ComponentB query B {
user {
id
role
name
}
products {
// a lot of fields...
}
} When I render
So by |
We want variable changes to trigger refetch from network, but not changes to the subtree causes by other mutations. With this example, if the mySizeProp changes will it refetch from the network?
|
@benjamn I think it would be nice to add this example to the documentation in the section about the update |
This should really be part of the migration guide 🙏 Went down a lot of rabbit holes to find this issue! https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/ |
Can someone say anything about how to handle optimsitic responses? |
We have optimistic responses in our apps and they did not change at all. Two things that I can think of is optimistic payload is incorrect and thus cache is not being updated. There should be an error on the console for this. Another thing is that Apollo client 3 cache cannot cache object if there is no _id or id fields. There would be a console info for this! |
@danfernand The optimistic responses are not changing. The problem I tried to explain is about the active watchQueries. They are not updated after a mutation with optimisticResponse+updateFunction. The watchQuery with |
I have the following situation: query A {
rootField {
field {
subfield {
...
}
}
}
} where When I call writeFragment the query refetches. I added I would like to only modify the |
I just upgraded from 3.0.2 to 3.2.1 and am experiencing the same issue, for both |
I am having a problem related to what @sinn1 mentioned above
This seems like a breaking change not mentioned anywhere? Or maybe I'm missing something. But when upgrading to v3, I have a lot of queries being refetched 2, 3, 4 times because a subsequent query fetches only a subset of the data. But I'm fetching data subsets everywhere. Example:
useGetItem triggers useGetAccount to get fetched again, with "cache-first". only way to solve it is using ""cache-first" + "cache-only" but that's not the behaviour I want when e.g. the variables change |
I experience the same thing. Queries same |
* Update Group Single Screen (#339) * Updates default messaging for Group Cards (#338) * Tests written Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Add new "pen" icon * Update a handful of styles on buttons * Linter * Border Width now set to 2px for legibility * Updated button style on Group Single screen * Fixes Group Member avatar layout issues * Removed console.log Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Edit group screen (#341) * Update Resources and set up Group Members card * Components for displaying a Group Member * Stories written for all components * Component for GroupMember * UI element for GroupMemberStatusBadge * Check in * Resolves error where Modal wasn't working * Finalize the Edit Group Member * Add a new group member modal Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Quick Updates * Caches Group Member Search Results * Manually update cache for Group Member _updates_ * Couple of SquareAvatar missing props * Updated the caching logic @see apollographql/apollo-client#6760 * New API updates * Update icons.js Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com>
* Update Group Single Screen (#339) * Updates default messaging for Group Cards (#338) * Tests written Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Add new "pen" icon * Update a handful of styles on buttons * Linter * Border Width now set to 2px for legibility * Updated button style on Group Single screen * Fixes Group Member avatar layout issues * Removed console.log Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Edit group screen (#341) * Update Resources and set up Group Members card * Components for displaying a Group Member * Stories written for all components * Component for GroupMember * UI element for GroupMemberStatusBadge * Check in * Resolves error where Modal wasn't working * Finalize the Edit Group Member * Add a new group member modal Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Quick Updates * Caches Group Member Search Results * Manually update cache for Group Member _updates_ * Couple of SquareAvatar missing props * Updated the caching logic @see apollographql/apollo-client#6760 * New API updates Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com>
Hi, |
* Feature manage group (#365) * Update Group Single Screen (#339) * Updates default messaging for Group Cards (#338) * Tests written Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Add new "pen" icon * Update a handful of styles on buttons * Linter * Border Width now set to 2px for legibility * Updated button style on Group Single screen * Fixes Group Member avatar layout issues * Removed console.log Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Edit group screen (#341) * Update Resources and set up Group Members card * Components for displaying a Group Member * Stories written for all components * Component for GroupMember * UI element for GroupMemberStatusBadge * Check in * Resolves error where Modal wasn't working * Finalize the Edit Group Member * Add a new group member modal Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Quick Updates * Caches Group Member Search Results * Manually update cache for Group Member _updates_ * Couple of SquareAvatar missing props * Updated the caching logic @see apollographql/apollo-client#6760 * New API updates * Update icons.js Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Update Group Single Screen (#344) * Update Group Single Screen (#339) * Updates default messaging for Group Cards (#338) * Tests written Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Add new "pen" icon * Update a handful of styles on buttons * Linter * Border Width now set to 2px for legibility * Updated button style on Group Single screen * Fixes Group Member avatar layout issues * Removed console.log Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Edit group screen (#341) * Update Resources and set up Group Members card * Components for displaying a Group Member * Stories written for all components * Component for GroupMember * UI element for GroupMemberStatusBadge * Check in * Resolves error where Modal wasn't working * Finalize the Edit Group Member * Add a new group member modal Co-authored-by: Caleb Panza <caleb.panza@icloud.org> * Quick Updates * Caches Group Member Search Results * Manually update cache for Group Member _updates_ * Couple of SquareAvatar missing props * Updated the caching logic @see apollographql/apollo-client#6760 * New API updates Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> * Email group button (#400) * first commit * Button Spacing * added route Co-authored-by: asleepingpanda <45076058+asleepingpanda@users.noreply.github.com> * Adds layout and route for Emailing Group (#399) * Adds layout and route for Emailing Group * Updated Label * Group Member Filters (#401) * Group Member Filters Active, Pending, Inactive * Mobile styling * Feature - Rich Text Editor (#402) * Quill React package and storybook * Adds the Rich Text Editor to the Email Composer * Font Size updates * Feature email composer confirmation (#404) * Email Confirmation dialogue * Quick path routing clean up * Select email recepients (#405) * first commit * Group Member Selection local state * Update GroupEmailComposer.js * Adds Email Recipients Modal * Adds Email Recipient Modal * commit for review * Update UI to be inline * Deprecated modal * Remove console.log's Co-authored-by: Lis Alfonso <46769629+codingwithlis@users.noreply.github.com> * Email Recipient/Select All update * Email a specific Group Member * Email Confirmation Screen * [CFDP-1896] Connected EmailComposer to API * patch - default campus value * Fixed styling on Group Member Detail modal * Fixed Styling for Sender Email Co-authored-by: Caleb Panza <caleb.panza@icloud.org> Co-authored-by: dzwood <46049974+dzwood@users.noreply.github.com> Co-authored-by: Lis Alfonso <46769629+codingwithlis@users.noreply.github.com>
Writing interaction steps to the cache caused the GET_EDIT_CAMPAIGN_DATA query to be re-fetched. The results of re-fetch then overwrote the local changes for pending interaction step changes. This re-fetch happened because in apollo-client@3 cache-and-network fetch policies re-run whenever there is a cache update that affects the query. For more information, see: apollographql/apollo-client#6760 (comment)
Writing interaction steps to the cache caused the GET_EDIT_CAMPAIGN_DATA query to be re-fetched. The results of re-fetch then overwrote the local changes for pending interaction step changes. This re-fetch happened because in apollo-client@3 cache-and-network fetch policies re-run whenever there is a cache update that affects the query. For more information, see: apollographql/apollo-client#6760 (comment) Adapts to two apollo client changes: 1. readFragment returns null for insufficient data instead of throwing MissingFieldError exceptions 2. writeFragment now only writes to fields present in the fragment itself Reviewed-by: @ben-pr-p
UPDATE 9/27/22: This doesn't work anymore. This got so much more complicated than it needs to be: -- Huge shoutout to @stangerjm for their proposed fix. Documenting our fix here:
export const client = new ApolloClient({
link,
cache,
defaultOptions: {
watchQuery: {
nextFetchPolicy: 'cache-first',
},
},
}); |
As other people have pointed out, using |
Would love to see an update from official maintainers, such as @benjamn, on this matter. The documentation remains incomplete, migration guide makes no mention of fetch policy behavior, and the community cannot help without word about what configuration is necessary to achieve @twittwer's note on optimistic response no longer working under |
@benjamn just to give an update, upgrading to v3.6.0 did not resolve the issue for me. What's even more problematic for me is that my I currently have to use a |
in my case @sinn1 comment helped me a lot debugging the issue of refetch after the mutation and cache update. The issue was that the mutation query results keys weren't the same ones as from the query causing a network fetch in order to properly update the cache. Thanks @sinn1. Why would we have to get back the exact same result keys from the mutation and increase the size of the response? |
I’ve been wrestling with this issue for a few weeks..to no avail. I updated Apollo Client to What I'm trying to accomplish... Load news feed by calling getFeed loads…
Does anyone know any workarounds to this issue? I have tried many combinations of |
I was experiencing the same issue with arrays that had different IDs after an update. I fixed this issue by adding the same fields in the mutation as the query. According to the documentation here: https://www.apollographql.com/docs/kotlin/caching/troubleshooting/
|
@leymytel thanks for the reply. In my case, the I'm getting lost on the "missing a single field". I have no fields missing that I can see. The upvote returned matches the upvote queried for in getFeed.
|
Hi everyone 👋 I've gone ahead and called this out in the migration docs for posterity via #10714. I'm going to close this issue out as I see no other actionable items, but please feel free to open a new issue should the need arise. Thanks! |
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. |
Hello Apollo team. We have an existing app that we are migrating from Apollo Client 2 to 3. We noticed an issue that is affecting our upgrade. After an apollo mutation modifying an entity that previously a query has loaded we are noticing we are setting loading to true if the fetch policy is set to cache-and-network.
We use cache-and-network to guarantee that our app is up to date with the backend and expect the cache to be updated with the network silently.
I am not sure if the behavior is expected since its different than AC 2.
Below I have created 2 code sandboxes that are updating a graphql server created with FaunaDB to show the issue.
Apollo Client 3 - https://codesandbox.io/s/funny-https-zb1hf
Apollo Client 2 - https://codesandbox.io/s/ecstatic-cori-t42ce
When updating completed by selecting the opposite value from a todo the whole page is reloading due to using loading from the query to show a loading state.
Both codesanbox examples are using the same graphql server.
I looking all over to confirm that this AC3 behavior is expected but cannot find anything to that regard.
Thank you for your time.
PS. Great job on the ecosystem! Once we get past this its gonna be awesome!
The text was updated successfully, but these errors were encountered: