-
Notifications
You must be signed in to change notification settings - Fork 739
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
Add documentation for inserting data into the cache #2862
Comments
Great question! What about removal from a list? How would you approach this with the new mechanism? |
We've realized that there is a gap in our documentation currently around this process. We'll be using this issue to track the work to add that documentation, which should then ultimately answer your questions. Thanks for your patience with us here. |
Hey guys! Any update regarding the documentation about these kind of cache manipulations? |
I'm not sure what has been added to the cache documentation since this issue was created but this is still the current documentation for writing to the cache.
Apollo Client probably does have additional cache features that Apollo iOS does not share but I'm not 100% familiar with what those features are. The current mechanism to update the Apollo iOS cache is still through a local cache mutation. |
Hi @calvincestari! Thank you for your response. # schema.graphql
type List {
id
name
}
query {
lists: [List!]!
} # operations.graphql
query getLists {
lists: lists {
...ListFragment
}
}
fragment ListFragment on List {
id
name
} #localMutations.graphql
query ListsLocalCacheMutation @apollo_client_ios_localCacheMutation {
lists {
...ListFragment
}
} My public static func cacheKeyInfo(for type: ApolloAPI.Object, object: ApolloAPI.ObjectData) -> CacheKeyInfo? {
if let id = object["id"] as? String {
return CacheKeyInfo(id: id)
}
if let id = object["id"] as? Int {
return CacheKeyInfo(id: String(id))
}
return nil
} Removing a deleted items from a cached collectionTo handle the deletion, it seems ok. I just update my collection by removing the apollo.perform(mutation: DeleteListMutation(id: listId)) { [weak self] result in
self?.handleGraphQLResult(result) { _ in
self?.apollo.store.withinReadWriteTransaction { transaction in
let cacheMutation = ListsLocalCacheMutation()
try transaction.update(cacheMutation) { (data: inout ListsLocalCacheMutation.Data) in
data.lists.removeAll(where: { $0.id == listId })
}
}
}
} Adding a freshly created item to a cached collectionNow, it's the adding I'm not sure. Usually, with the web client, we work with object cache references. But in these case, I haven't been able to just add a reference to the newly created list. apollo.perform(mutation: CreateListMutation(name: name)) { [weak self] result in
self?.handleGraphQLResult(result) { data in
if let newList = data?.list {
self?.apollo.store.withinReadWriteTransaction { transaction in
let cacheMutation = ListsLocalCacheMutation()
try transaction.update(cacheMutation) { (data: inout ListsLocalCacheMutation.Data) in
data.lists.append(ListsLocalCacheMutation.Data.List(_dataDict: newList.__data))
}
}
}
}
} As you can see, I had to recreate an object of the proper type If we can agree on the proper way to do these two things, I think these examples would be a nice addition to the documentation. |
I believe that's the correct way to do it. There are no cache API functions that let you insert the object by cache reference unfortunately. One piece of advice is to be careful with the underscored methods and properties, such as |
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. |
Question
Question
With Apollo version
1.0.x
, what is the best way to add a new object in the cache of the result of a query ?Context :
I have a screen with favorite tracks and I want to update this list whenever a track has been removed or added in favorite from an another page.
A bit like this example from demystifying cache normalization.
in code it gives :
Coming back to iOS, before the apollo version
0.x
there was a methodtransaction.update(query: query
that could do the job (example here) but now with version1.0.x
we have to make a dedicated query marked as@apollo_client_ios_localCacheMutation
to write into the cache.I have 3 queries (listed at the end) :
query GetFavoriteTracks($first: Int!, $after: String)
query GetFavoriteTracks($first: Int!, $after: String) @apollo_client_ios_localCacheMutation
mutation StoreUserFavoriteTracks($trackId: ID!)
How ?
I'd like to update
query GetFavoriteTracks($first: Int!, $after: String)
cache whenmutation StoreUserFavoriteTracks($trackId: ID!)
has been done.It would go for something like this :
At this point if I want to write in the
GetFavoriteTracksQuery
apparently I have no choice than using theGetFavoriteTracksLocalCacheMutation
query :try transaction.update(GetFavoriteTracksQueryLocalCacheMutation(first: 10, after: .some(cursor))) { data in
I'm a bit stucked here to add or remove a track object from the data track list which has the following type :
[GetFavoriteTracksQueryLocalCacheMutation.Data.Me.Favorites.Tracks.Edge]
-> How can I easily take the favoritedTrack defined upper and add it to the list ?
Queries used
The text was updated successfully, but these errors were encountered: