-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathDefaultInterceptorProvider.swift
More file actions
59 lines (51 loc) · 2.23 KB
/
DefaultInterceptorProvider.swift
File metadata and controls
59 lines (51 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#if !COCOAPODS
import ApolloMigrationAPI
#endif
/// The default interceptor provider for typescript-generated code
open class DefaultInterceptorProvider: InterceptorProvider {
private let client: URLSessionClient
private let store: ApolloStore
private let shouldInvalidateClientOnDeinit: Bool
/// Designated initializer
///
/// - Parameters:
/// - client: The `URLSessionClient` to use. Defaults to the default setup.
/// - shouldInvalidateClientOnDeinit: If the passed-in client should be invalidated when this interceptor provider is deinitialized. If you are recreating the `URLSessionClient` every time you create a new provider, you should do this to prevent memory leaks. Defaults to true, since by default we provide a `URLSessionClient` to new instances.
/// - store: The `ApolloStore` to use when reading from or writing to the cache. Make sure you pass the same store to the `ApolloClient` instance you're planning to use.
public init(client: URLSessionClient = URLSessionClient(),
shouldInvalidateClientOnDeinit: Bool = true,
store: ApolloStore) {
self.client = client
self.shouldInvalidateClientOnDeinit = shouldInvalidateClientOnDeinit
self.store = store
}
deinit {
if self.shouldInvalidateClientOnDeinit {
self.client.invalidate()
}
}
open func interceptors<Operation: GraphQLOperation>(
for operation: Operation
) -> [any ApolloInterceptor] {
return [
MaxRetryInterceptor(),
CacheReadInterceptor(store: self.store),
NetworkFetchInterceptor(client: self.client),
ResponseCodeInterceptor(),
MultipartResponseParsingInterceptor(),
jsonParsingInterceptor(for: operation),
AutomaticPersistedQueryInterceptor(),
CacheWriteInterceptor(store: self.store),
]
}
private func jsonParsingInterceptor<Operation: GraphQLOperation>(for operation: Operation) -> any ApolloInterceptor {
if Operation.hasDeferredFragments {
return IncrementalJSONResponseParsingInterceptor()
} else {
return JSONResponseParsingInterceptor()
}
}
open func additionalErrorInterceptor<Operation: GraphQLOperation>(for operation: Operation) -> (any ApolloErrorInterceptor)? {
return nil
}
}