Apollo GraphQL标头在iOS Swift中总是显示失败

Apollo GraphQL标头在iOS Swift中总是显示失败,swift,graphql,apollo-client,apollo-ios,Swift,Graphql,Apollo Client,Apollo Ios,在我的swift项目中,我第一次与Apollo GraphQL客户合作。按照他们文档中提到的所有步骤,在谷歌上搜索我的问题,但没有运气。我通过在我的请求URL中传递一个头来发出一个查询请求,但它总是给我一个失败消息,比如失败(Apollo.LegacyParsingInterceptor.LegacyParsingError.couldNotParseToLegacyJSON(数据:1839字节))。我已经提到了的答案,但这对我来说并不适用。任何帮助都将不胜感激 导入UIKit 进口阿波罗 cl

在我的swift项目中,我第一次与Apollo GraphQL客户合作。按照他们文档中提到的所有步骤,在谷歌上搜索我的问题,但没有运气。我通过在我的请求URL中传递一个头来发出一个查询请求,但它总是给我一个失败消息,比如失败(Apollo.LegacyParsingInterceptor.LegacyParsingError.couldNotParseToLegacyJSON(数据:1839字节))。我已经提到了的答案,但这对我来说并不适用。任何帮助都将不胜感激

导入UIKit

进口阿波罗

class Network {

static let shared = Network()

private(set) lazy var apollo: ApolloClient = {

    let cache = InMemoryNormalizedCache()
    let store1 = ApolloStore(cache: cache)
    let client1 = URLSessionClient()
    let provider = NetworkInterceptorProvider(store: store1, client: client1)
    
    let url = URL(string:"https://Storename.myshopify.com/admin/api/2021-04/graphql")!
    
    let requestChainTransport = RequestChainNetworkTransport(interceptorProvider: provider,endpointURL: url)
    
    return ApolloClient(networkTransport: requestChainTransport,
                        store: store1)
}()}


class RequestOrderDetailInterceptor: ApolloInterceptor {

func interceptAsync<Operation: GraphQLOperation>(
    chain: RequestChain,
    request: HTTPRequest<Operation>,
    response: HTTPResponse<Operation>?,
    completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
    
    request.addHeader(name: "X-Shopify-Access-Token", value: "MyToken")
    print( "Outgoing request: \(request)")
    chain.proceedAsync(request: request,
                       response: response,
                       completion: completion)
}

}

我看到你最近对其他人问题的评论,只是想知道你是否取得了成功?我在这里找到了你的评论


请直接对这个问题发表评论。我尝试了@ChamathJeevan给出的相同代码语法,但它对我不起作用,我不知道为什么。
class ResponseOrderDetailInterceptor: ApolloInterceptor {

enum ResponseLoggingError: Error {
    case notYetReceived
}

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

    request.addHeader(name: "X-Shopify-Access-Token", value: "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")

    defer {

        chain.proceedAsync(request: request,
                           response: response,
                           completion: completion)

    }

    guard let receivedResponse = response else {
        chain.handleErrorAsync(ResponseLoggingError.notYetReceived,
                               request: request,
                               response: response,
                               completion: completion)
        return

    }

        print("HTTP Response: \(receivedResponse.httpResponse)")

    if let stringData = String(bytes: receivedResponse.rawData, encoding: .utf8) {
        print("Data: \(stringData)")
    } else {
        print("Could not convert data to string!")
    }
}
struct NetworkInterceptorProvider: InterceptorProvider {

// These properties will remain the same throughout the life of the `InterceptorProvider`, even though they
// will be handed to different interceptors.
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(),
        LegacyCacheReadInterceptor(store: self.store),
        RequestOrderDetailInterceptor(),
        NetworkFetchInterceptor(client: self.client),
        ResponseOrderDetailInterceptor(),
        ResponseCodeInterceptor(),
        LegacyParsingInterceptor(cacheKeyForObject: self.store.cacheKeyForObject),
        AutomaticPersistedQueryInterceptor(),
        LegacyCacheWriteInterceptor(store: self.store)
    ]
}
 func getOrderDetails(){

 Network.shared.apollo.fetch(query: OrderDetailsByIdQuery(id: GraphQL.ID(rawValue: "Z2lkOi8vc2hvcGlmeS9PcmRlci8zNzcxODAxNjk4NDY5P2tleT1kNGRiMmVhODFlNGVlNzg1NzhhMDQ4ODI2Mzc4ZTkxMw==").rawValue))  {  result in

        print(result) 

        switch result {

        case .success(let graphQLResult):
           
                print("Oreder Details:",graphQLResult.data?)
            
        case .failure(let error):
            print("Error loading data \(error.localizedDescription)")
        }
    }