apollo setup
import Foundation
import Apollo
class Network {
static let shared = Network()
private(set) lazy var apollo = ApolloClient(url: URL(string: "http://localhost:4000/graphql")!)
}
querying data and storing in state
@State var user: UserQuery.Data.User?
func loadUser() {
Network.shared.apollo.fetch(query: UserQuery(username: username)) { result in
switch result {
case .success(let graphQLResult):
if let result = graphQLResult.data?.user {
user = result
}
case .failure(let error):
print("Error: \(error)")
}
}
}
var body: some View {
ScrollView(showsIndicators: false) {
...
}
.onAppear(perform: loadUser)
}
function to perform follow mutation & update cache (is inside the scrollview above)
Button(user.followingUser ? "Unfollow" : "Follow") {
Network.shared.apollo.perform(mutation: FollowUserMutation(userFollowingId: user.uuid)) { result in
switch result {
case .success(_):
Network.shared.apollo.store.withinReadWriteTransaction({ transaction in
try transaction.update(query: UserQuery(username: user.username)) { (data: inout UserQuery.Data) in
if (data.user?.followingUser == true) {
data.user?.totalFollowers -= 1
} else {
data.user?.totalFollowers += 1
}
data.user?.followingUser.toggle()
}
})
case .failure(let error):
print("Error: \(error)")
}
}
}
on the user page I have a button to perform the follow/unfollow mutation & then update the local cache. mutation works as expected, and even the cache update works, but it only updates on the ui whenever i switch views, rather than immediatley when the button is pressed.
i'm new to both swift and apollo-ios so I have no idea why the cache update isnt working as it would in apollo-react, or if it's even possible
with apollo-ios v0.53.0