1

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.

DrakeAnglin
  • 718
  • 1
  • 4
  • 12

1 Answers1

3

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)
        ]
    }
}
DrakeAnglin
  • 718
  • 1
  • 4
  • 12
  • Don't know how but this works. Does anyone know what's the problem here ? – Kesava May 30 '23 at 07:33
  • In DefaultInterceptorProvider, there is an option called "shouldInvalidateClientOnDeinit" and this is true as default. And as i understand it means when your class denitialized, it will invalidate the session. I think the problem is you call create client or Graphql class(that you created) incorrect place. you can check with adding deinit in Graphql. For example in my project i was creating client on appear method in swiftui view. but then changed with creating an observable object and in it create client. then onAppear i fetched my query – Gorkem Sevim Jul 01 '23 at 20:28