I’m learning to use LocalCacheMutations and @apollo_client_ios_localCacheMutation
and would like feedback if I’m using the concepts properly.
If I’m trying to “build” some Data model, the initialization is a bit cumbersome. Imagine this example:
query collectionsLocalCacheMutation @apollo_client_ios_localCacheMutation {
collections {
edges {
node {
id
name
}
}
}
}
Then if we want to directly add a new edge/node there would be code as follows
func addEdge(data: inout CollectionsLocalCacheMutation.Data) {
var items = data.collections?.edges ?? []
var newEdge = CollectionsLocalCacheMutation.Data.Collections.Edge(data: DataDict([:], variables: nil))
newEdge.node = CollectionsLocalCacheMutation.Data.Collections.Edge.Node(data: DataDict([:], variables: nil))
newEdge.node.id = "100"
newEdge.node.name = "Sally"
items.append(newEdge)
data.collections?.edges = items
}
Creating those empty DataDicts is annoying. This can be solved by adding an extension on the local cache mutation. Perhaps such a initializer belongs in the classes proper (as they are mutable)?
extension CollectionsLocalCacheMutation.Data.Collections.Edge {
init() {
self.init(data: DataDict([:], variables: nil))
}
}
The 0.51 code was a bit “safer” in my opinion. The above, while type-safe, doesn’t ensure that all the expected fields are set (node,id, node.name, etc). The equivalent code in 0.51 looked like:
let newEdge = GetCollectionsQuery.Data.Collections.Edge(
id: collection.id,
node: GetCollectionsQuery.Data.Collections.Edge.Node(
id: collection.id,
name: collection.name
)
)
The signatures of the initializers forced one to set all the expected properties. And if the query ever changed (adding/removing fields) the above wouldn’t compile… leading you right to where code needed to be updated. The 1.0 pattern misses out of these features (unless I’m missing something).