apollo-ios updating cache not working as unless re-rendering view

339 views Asked by At

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

1

There are 1 answers

0
bjorn.lau On

Well, to me it seems pretty obvious that your UI don't update because the only time you tell it to update is with .onAppear(perform: loadUser)

Try one of these two approaches when you get a success from FollowUserMutation:

  1. Call loadUser, which will fetch again and update your state which should update your UI.

  2. Set user.totalFollowers to whatever you are also updating in the cache, and since you are now updating the @State variable directly it should update your UI.

@State is not just automatically realizing that something in your Apollo cache updates, you need to update the state yourself, and then everything that uses the state will update its UI.