ios - 如何在 Apollo GraphQL 0.49.1 for iOS 中添加标题
问题描述
我正在使用这段代码,但这段代码给了我这样的错误
- 在范围内找不到“HTTPNetworkTransport”
- 在范围内找不到类型“HTTPNetworkTransportDelegate”
final class Network {
static let shared = Network()
private lazy var networkTransport: NetworkTransport = {
let transport = HTTPNetworkTransport(url: URL(string: "https://exampe.com/grapghql")!)
transport.delegate = self
return transport
}()
private(set) lazy var apollo = ApolloClient(networkTransport: self.networkTransport)
}
extension Network: HTTPNetworkTransportDelegate {
func networkTransport(_ networkTransport: NetworkTransport, shouldSend request: URLRequest) -> Bool {
return true
}
func networkTransport(_ networkTransport: NetworkTransport, willSend request: inout URLRequest) {
let token = ""
var headers = request.allHTTPHeaderFields ?? [String: String]()
headers["Authorization"] = "Bearer \(token)"
request.allHTTPHeaderFields = headers
}
}
解决方案
我认为这段代码绝对可以帮助你。
您可以通过这种方式创建您的客户端。
import Foundation
import Apollo
class Network {
static let shared = Network()
var apollo: ApolloClient {
// The cache is necessary to set up the store, which we're going to hand to the provider
let cache = InMemoryNormalizedCache()
let store = ApolloStore(cache: cache)
let accessToken = "SET_AUTHORIZATION_TOKEN" // TODO: - "Authorization_TOKEN"
let authPayloads = ["Authorization": "Bearer \(String(describing: accessToken))"]
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = authPayloads
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
let client = URLSessionClient(sessionConfiguration: configuration, callbackQueue: nil)
let provider = NetworkInterceptorProvider(store: store, client: client)
let endpointURL = URL(string: "YOUR_ENDPOINT_URL")!// TODO: - "YOUR_ENDPOINT_URL"
let requestChainTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: endpointURL)
// Remember to give the store you already created to the client so it
// doesn't create one on its own
return ApolloClient(networkTransport: requestChainTransport, store: store)
}
}
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(),
CacheReadInterceptor(store: self.store),
NetworkFetchInterceptor(client: self.client),
ResponseCodeInterceptor(),
JSONResponseParsingInterceptor(cacheKeyForObject: self.store.cacheKeyForObject),
AutomaticPersistedQueryInterceptor(),
CacheWriteInterceptor(store: self.store)
]
}
}
推荐阅读
- android - 在 Flutter 中的图像上添加水印/文本 - Dart
- php - 在php中显示默认浏览器错误消息
- meson-build - 为 flatpak 应用程序定义图标
- node.js - 打字稿错误——错误:找不到模块'typescript/tsc.js'
- html - 将 html 页面链接到 chromebook 上的其他 html 页面
- node.js - 如何检查 new_chat_members 上的电报组邀请链接参数
- python - 存储或将值覆盖到特定键 python
- reactjs - 对于 ag-grid,是否可以访问旧的 rowData?
- homebrew - 由于 Cask 错误,无法运行 `brew doctor` 或 `brew list --cask`
- python - OSError:libespeak.so.1:无法打开共享对象文件:安装espeak时没有这样的文件或目录