apollo-ios

Constant sessionInvalidated on Apollo iOS


I am looking to add GraphQL to our current iOS app. Every time I make a request, it fails with a "sessionInvalidated" message.

I have followed along with the getting started documentation, and followed along with the Advanced Networking Configuration documentation here, so I could add the authorization header to the requests.

I am attempting to create a class with an Apollo Client like this

class GraphQL {
    private(set) lazy var apolloClient: ApolloClient = {
        let userDefaults = UserDefaults.init(suiteName: SUITE_NAME)
        let auth = userDefaults?.string(forKey: "authToken") ?? ""
        let authPayloads = ["Authorization": "Bearer \(auth)"]
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = authPayloads
        let client = URLSessionClient(sessionConfiguration: configuration)
        let endpointURL = URL(string: ENDPOINT)!
        let store = ApolloStore(cache: InMemoryNormalizedCache())
        let interceptorProvider = DefaultInterceptorProvider(client: client, store: store)
        let networkTransport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: endpointURL)
        
        return ApolloClient(networkTransport: networkTransport, store: store)
    }()
    
    func getAOS(station: String?, completion: @escaping (GraphCountData?, Error?) -> ()) -> Cancellable {
        apolloClient.fetch(query: GetAOSQuery(goalsFilter: .some(FilterFindOneGoalsInput(sTATION: station ?? "")), aosFilter: .some(FilterCountAosInput(station: station ?? "")))) { result in
            switch result {
            case .success(let response) :
                if let data = response.data {
                    completion(GraphCountData(value: data.value!, goal: data.goal!.value!), nil)
                } else if let errors = response.errors {
                    debugPrint(errors)
                }
            case .failure(let error) :
                debugPrint(error.localizedDescription)
                completion(nil, error)
            }
        }
    }
}

Every time I make a request with the getAOS function, it fails and gives me the error "sessionInvalidated." The token is getting added as I expect, but I have no idea what could be causing the issue as every run hits the .failure case.


Solution

  • I ended up solving this by not using the DefaultInterceptorProvider for the interceptor provider. I had to make a more customized one similar to the documentation:

    struct NetworkInterceptorProvider: InterceptorProvider {
        private let store: ApolloStore
        private let client: URLSessionClient
        
        init(store: ApolloStore, client: URLSessionClient) {
            self.store = store
            self.client = client
        }
        
        func interceptors<Operation: GraphQLOperation>(for operation: Operation) -> [ApolloInterceptor] {
            return [
                MaxRetryInterceptor(),
                CacheReadInterceptor(store: self.store),
                NetworkFetchInterceptor(client: self.client),
                ResponseCodeInterceptor(),
                JSONResponseParsingInterceptor(),
                AutomaticPersistedQueryInterceptor(),
                CacheWriteInterceptor(store: self.store)
            ]
        }
    }