-
Notifications
You must be signed in to change notification settings - Fork 717
-
Notifications
You must be signed in to change notification settings - Fork 717
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
Cache mutations are somewhat painful to work with #3327
Comments
Thanks for the feedback @BrentMifsud. We've had a lot of similar feedback from people on this feature, so you are not alone. We do intend to deprecate In the mean time, the easiest way to work with this is to put the |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better. |
@AnthonyMDev Thanks for the response. Do you have any examples of this in practice? I have added the local cache mutation directive on my fragment, but there does not seem to be any option to insert a fragment.
Here is my codegen config (in case im missing something):
|
Ah, I see. This is still a bit difficult to do. Which is why we want to make the changes I mentioned above. Check out the docs here. You'll want to use the fragment AnnouncementsListFragment @apollo_client_ios_localCacheMutation on Query {
announcements(locationId: $locationID) {
...AnnouncementFragment
}
} When mutating the cache, you'll need to know the cache ID of the entity that has the Try this: var myNewAnnouncement: AnnouncementFragment
store.withinReadWriteTransaction { transaction in
transaction.updateObject(
ofType: AnnouncementsListFragment.self,
withKey: "QUERY_ROOT",
variables: ["locationID": "myLocationID"]
) { (announcements inout AnnouncementsListFragment)
announcements.append(myNewAnnouncement)
}
} There is definitely a lot to be desired with the current APIs for this. Hopefully this will get you where you need to be for now, but we definitely want to improve on this. |
Ahhh I see. I never thought of making an array into a fragment. Will try that. |
Use case
Today if I want my query to have a local cache mutation, I need to do the following:
The result of this is that codegen creates duplicate types:
If I ever wanted to use a local cache mutation to insert a new item, I can't simply do:
instead I have to do the following:
Alternatively, I can result to using the
_fieldData
or__data
initializers to do this in one line, but this is not type safe and can fail.Describe the solution you'd like
Ideally, the code gen should be smart enough to use a single type for both the local cache mutation and the query, so that it is easy to perform cache mutations. Or at the very least, generate convenience initializers in the cache mutations that can utilize the query data types.
Perhaps any queries marked with
@apollo_client_ios_localCacheMutation
should have an equivalent query and cache mutation generated together with the same types.The text was updated successfully, but these errors were encountered: