Skip to content
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

Caching callbacks are not working when we use combine in iOS Swift #3429

Closed
ArunVicky001 opened this issue Aug 26, 2024 · 5 comments
Closed
Labels
awaiting response question Issues that have a question which should be addressed

Comments

@ArunVicky001
Copy link

ArunVicky001 commented Aug 26, 2024

Question

I am using in memory caching, my code is not capturing the second call back.

func myRequest<T, Q: GraphQLQuery>(query: Q) -> Future<Response<T>, CYGErrorType>? where T: RootSelectionSet {
        return Future<Response<T>, CYGErrorType> { promise in
            // Call fetchValue function
            guard let futureFetchValue = self.fetchValue(query: query, cachePolicy: self.model.cachePolicy) as Future<Response<T>, CYGErrorType>? else {
                promise(.failure(.default))
                return
            }
            
            // Handle the result of fetchValue
            futureFetchValue
                .sink(receiveCompletion: { completion in
                    switch completion {
                    case .finished:
                        break
                    case .failure(let error):
                        promise(.failure(error))
                    }
                }, receiveValue: { result in
                    print("API>> API>> response")
                    promise(.success(result))
                })
                .store(in: &self.cancellable)
        }
    }
private func fetchValue<T, Query: GraphQLQuery>(query: Query, cachePolicy: CachePolicy) -> Future<Response<T>, CYGErrorType>? where T: RootSelectionSet {
       return Future<Response<T>, CYGErrorType> { promise in
           // Check internet connection
           guard ConnectionManager.isReachable else {
               promise(.failure(CYGErrorType.noInternetConnection))
               return
           }

           // Ensure the Apollo client is available
           guard let apolloClient = self.apollo else {
               promise(.failure(CYGErrorType.default)) // Handle the missing Apollo client as an error
               return
           }

           print("API>> hit")

           // Perform the GraphQL fetch operation
           apolloClient.fetch(query: query, cachePolicy: cachePolicy) { result in
               switch result {
               case .success(let graphQLResult):
                  
                   // Validate the response from GraphQL
                   let validateResponse = graphQLResult.isValid(query: query)
                   switch validateResponse {
                   case .success:
                       guard let data = graphQLResult.data as? T else {
                           promise(.failure(CYGErrorType.default))
                           return
                       }
                       
                       let response = Response(value: data, response: nil, error: nil)
                       promise(.success(response))
                       print("API>> response")

                       
                   case .failureWithError(let error):
                       // Handle validation error
                       promise(.failure(error))
                   }
               case .failure(let error):
                   // Parse the error and fail the promise
                   let cygError = self.graphQLErrorParsing(error: error, queryName: Query.operationName)
                   promise(.failure(cygError))
               }
           }
       }
   }

output:

 print("API>> hit")
 print("API>> response")
 print("API>> API>> response")
 print("API>> response")

Is working on a Swift function that involves handling GraphQL queries using Combine's AnyPublisher. They are encountering an issue where their function is not capturing the second callback as expected.

@ArunVicky001 ArunVicky001 added the question Issues that have a question which should be addressed label Aug 26, 2024
@apollographql apollographql deleted a comment from flyinghead Aug 26, 2024
@ArunVicky001
Copy link
Author

@calvincestari can you check this question?

@calvincestari
Copy link
Member

Hi @ArunVicky001 - I can't see anything obviously wrong with the code, it all appears correct. There is no logging in the mapError closure nor the failureWithError case so it is possible that is the code path for the second fetch response.

Given that you're seeing print("API>> response") twice it seems as though the Apollo iOS code is functioning as expected and the problem is in your custom code. I think the best debugging next would be using breakpoints to see what happens in the above code when the second response callback is received.

@ArunVicky001
Copy link
Author

ArunVicky001 commented Aug 27, 2024

Hi @calvincestari Thanks for responding me, I had tested this code apollo client gives response twice,

The second callback from fetchValue is not being captured from myRequest. I’m expecting to see both API responses in the output, but the second one is missing. It seems like Combine Future is only support first callback not the second one, but I’m not sure how to properly handle it.

Why is fetchValue not capturing the second callback? How can I ensure that both callbacks are properly captured and handled by using combine future because we were integrated combine not the escaping closure, Any help or suggestions on how to resolve this issue would be greatly appreciated!

Note: I had edited the code

@AnthonyMDev
Copy link
Contributor

I believe Future is not the data structure you want to use here, as it only supports a single event being published. I'd recommend using either an AsyncStream or a Combine publisher that supports multiple events.

That said, this is not an issue with apollo-ios itself and there is no actionable work her for this project. I'm going to close this issue.

@AnthonyMDev AnthonyMDev closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2024
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

4 participants
@calvincestari @AnthonyMDev @ArunVicky001 and others