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

Problems with interceptor #3468

Closed
Jensileus opened this issue Oct 31, 2024 · 3 comments
Closed

Problems with interceptor #3468

Jensileus opened this issue Oct 31, 2024 · 3 comments
Labels
question Issues that have a question which should be addressed

Comments

@Jensileus
Copy link

Question

I have some problems with my Network.swift that is the apollo clent and handler,
//Network.swift
import Foundation
import Apollo
import FixiAPI

enum NetworkError: Error {
case userNotFound
case networkFailure
case missingToken
}

class Network {
static let shared = Network()
private let authManager = AuthenticationManager()

private(set) lazy var apollo: ApolloClient = {
    let url = URL(string: "https://api.gg.gg/graphql")!
    let store = ApolloStore()
    let interceptorProvider = DefaultInterceptorProvider(store: store)

    // Correct way to add interceptors:
    TokenInterCeption.interceptors = { [authManager] operation in
        return [TokenInterCeption(authManager: authManager)]
    }

    let transport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
    return ApolloClient(networkTransport: transport, store: store)
}()

private init() {}

func fetchUserContext(completion: @escaping (Result<UserContextQuery.Data, Error>) -> Void) {
    apollo.fetch(query: UserContextQuery()) { result in
        switch result {
        case .success(let graphQLResult):
            if let userContext = graphQLResult.data {
                completion(.success(userContext))
            } else {
                completion(.failure(NetworkError.userNotFound))
            }
        case .failure(let error):
            completion(.failure(error))
        }
    }
}

}
import Foundation
import Apollo

class Tokeninterceptor: ApolloInterceptor {
private let authManager: AuthenticationManager

init(authManager: AuthenticationManager) {
    self.authManager = authManager
}

func interceptAsync<Operation: GraphQLOperation>(
    chain: RequestChain,
    request: HTTPRequest<Operation>,
    response: HTTPResponse<Operation>?,
    completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {

    if let token = authManager.getAccessToken() {
        request.addHeader(name: "Link", value: token)
    }

    chain.proceedAsync(request: request, response: response, interceptor: self, completion: completion)
}

} this was a mix but the main problem is this: Type 'Tokeninterceptor' does not conform to protocol 'ApolloInterceptor' <- why?

@Jensileus Jensileus added the question Issues that have a question which should be addressed label Oct 31, 2024
@Jensileus
Copy link
Author

We try again: // Network.swift
import Foundation
import Apollo
import FixiAPI

enum NetworkError: Error {
case userNotFound
case networkFailure
case missingToken
}

class Network {
static let shared = Network()
private let authManager = AuthenticationManager()

private(set) lazy var apollo: ApolloClient = {
    let url = URL(string: "https://xx.xxx.one/graphql")!
    let store = ApolloStore()
    
    let userManagerInterceptor = UserManagementInterceptor(authManager: authManager) // Create interceptor instance

    let interceptorProvider = DefaultInterceptorProvider(store: store, interceptors: [userManagerInterceptor]) // Add to provider

    let transport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
    return ApolloClient(networkTransport:

transport, store: store)
}()

private init() {}

func fetchUserContext(completion: @escaping (Result<UserContextQuery.Data, Error>) -> Void) {
    apollo.fetch(query: UserContextQuery()) { result in
        switch result {
        case .success(let graphQLResult):
            if let userContext = graphQLResult.data {
                completion(.success(userContext))
            } else {
                completion(.failure(NetworkError.userNotFound))
            }
        case .failure(let error):
            completion(.failure(error))
        }
    }
}

}
// UserManagementInterceptor.swift (updated)
import Foundation
import Apollo

class UserManagementInterceptor: ApolloInterceptor {
private let authManager: AuthenticationManager

init(authManager: AuthenticationManager) {
    self.authManager = authManager
}

func interceptAsync<Operation: GraphQLOperation>(
    chain: RequestChain,
    request: HTTPRequest<Operation>,
    response: HTTPResponse<Operation>?,
    completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void

) {
    // Access token from AuthenticationManager
    guard let token = authManager.getAccessToken() else {
        chain.handleErrorAsync(NetworkError.missingToken,
                               request: request,
                               response: response,
                               completion: completion)
        return
    }

    request.addHeader(name: "Authorization", value: "Bearer \(token)")
    chain.proceedAsync(request: request,
                      response: response,

                      interceptor: self,
                      completion: completion)

}

}
so i made a own? But is this built in?

@calvincestari
Copy link
Member

this was a mix but the main problem is this: Type 'Tokeninterceptor' does not conform to protocol 'ApolloInterceptor' <- why?

@Jensileus, Xcode is able to add the stubs to your code for ApolloInterceptor conformance, I suggest you use that.

But is this built in?

I don't understand the question - is what built in?

@calvincestari calvincestari closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 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
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants