Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit bedc62f

Browse files
committed
Migrate to interceptor-based architecture
1 parent 214b27e commit bedc62f

8 files changed

+113
-44
lines changed

ApolloDeveloperKit.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@
8888
5BB8BE2B2377CB3A004D93B3 /* URLSessionConfiguration+Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB8BE2A2377CB3A004D93B3 /* URLSessionConfiguration+Test.swift */; };
8989
5BBA278F2662F56100EBE185 /* DebuggableResultTranslateInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBA278E2662F56100EBE185 /* DebuggableResultTranslateInterceptor.swift */; };
9090
5BBA27982662FDAA00EBE185 /* DebuggableInterceptorProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBA27972662FDAA00EBE185 /* DebuggableInterceptorProvider.swift */; };
91+
5BBA27A02663149A00EBE185 /* DebugInitializeInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBA279F2663149900EBE185 /* DebugInitializeInterceptor.swift */; };
9192
5BBBF5E02340CAF500D64D7A /* MIMEType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBBF5DF2340CAF500D64D7A /* MIMEType.swift */; };
93+
5BBF34EA2663A04C003D606A /* DebuggableRequestChainNetworkTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBF34E92663A04C003D606A /* DebuggableRequestChainNetworkTransport.swift */; };
9294
5BBF4C8223F19DDF00FDC452 /* OperationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBF4C8123F19DDF00FDC452 /* OperationStore.swift */; };
9395
5BBF4C8423F24E6B00FDC452 /* OperationStoreControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BBF4C8323F24E6B00FDC452 /* OperationStoreControllerTests.swift */; };
9496
5BC65865234E9F790019CF57 /* ConsoleRedirection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BC65864234E9F790019CF57 /* ConsoleRedirection.swift */; };
@@ -247,7 +249,9 @@
247249
5BB8BE2A2377CB3A004D93B3 /* URLSessionConfiguration+Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLSessionConfiguration+Test.swift"; sourceTree = "<group>"; };
248250
5BBA278E2662F56100EBE185 /* DebuggableResultTranslateInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebuggableResultTranslateInterceptor.swift; sourceTree = "<group>"; };
249251
5BBA27972662FDAA00EBE185 /* DebuggableInterceptorProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebuggableInterceptorProvider.swift; sourceTree = "<group>"; };
252+
5BBA279F2663149900EBE185 /* DebugInitializeInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugInitializeInterceptor.swift; sourceTree = "<group>"; };
250253
5BBBF5DF2340CAF500D64D7A /* MIMEType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MIMEType.swift; sourceTree = "<group>"; };
254+
5BBF34E92663A04C003D606A /* DebuggableRequestChainNetworkTransport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebuggableRequestChainNetworkTransport.swift; sourceTree = "<group>"; };
251255
5BBF4C8123F19DDF00FDC452 /* OperationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationStore.swift; sourceTree = "<group>"; };
252256
5BBF4C8323F24E6B00FDC452 /* OperationStoreControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationStoreControllerTests.swift; sourceTree = "<group>"; };
253257
5BC65864234E9F790019CF57 /* ConsoleRedirection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleRedirection.swift; sourceTree = "<group>"; };
@@ -420,6 +424,7 @@
420424
5B3A83A522C87F3B002B4FFB /* WebServer */,
421425
5B2F84F022B4378B0025A211 /* ApolloDebugServer.swift */,
422426
5B2F84E622B432730025A211 /* DebuggableNetworkTransport.swift */,
427+
5BBF34E92663A04C003D606A /* DebuggableRequestChainNetworkTransport.swift */,
423428
5B2F84E422B4321E0025A211 /* DebuggableNormalizedCache.swift */,
424429
);
425430
path = Classes;
@@ -612,6 +617,7 @@
612617
5BBA27952662FD0C00EBE185 /* Interceptor */ = {
613618
isa = PBXGroup;
614619
children = (
620+
5BBA279F2663149900EBE185 /* DebugInitializeInterceptor.swift */,
615621
5BBA278E2662F56100EBE185 /* DebuggableResultTranslateInterceptor.swift */,
616622
5BBA27972662FDAA00EBE185 /* DebuggableInterceptorProvider.swift */,
617623
);
@@ -848,6 +854,7 @@
848854
5B9B3F85240600C9007E1A8B /* Socket.swift in Sources */,
849855
5B15067424F149960081B1E8 /* Schema+JSONEncodable.swift in Sources */,
850856
5B2F84E722B432730025A211 /* DebuggableNetworkTransport.swift in Sources */,
857+
5BBA27A02663149A00EBE185 /* DebugInitializeInterceptor.swift in Sources */,
851858
5BBA27982662FDAA00EBE185 /* DebuggableInterceptorProvider.swift in Sources */,
852859
5BCE500724F4441900310E26 /* KeyedStack.swift in Sources */,
853860
5BDB7DB62510318100BE4DE4 /* HTTPOutputStream.swift in Sources */,
@@ -877,6 +884,7 @@
877884
5B5EBB9E2510E32B00D0D025 /* InterfaceAddressIterator.swift in Sources */,
878885
5B4F460B2370A8540039495A /* DarwinFileDescriptorDuplicator.swift in Sources */,
879886
5BB8BE2B2377CB3A004D93B3 /* URLSessionConfiguration+Test.swift in Sources */,
887+
5BBF34EA2663A04C003D606A /* DebuggableRequestChainNetworkTransport.swift in Sources */,
880888
5BCF219F250F419400279D4E /* HTTPRequestContext.swift in Sources */,
881889
5B8983CE237A3E3A00C857D6 /* BackgroundTask.swift in Sources */,
882890
5BBF4C8223F19DDF00FDC452 /* OperationStore.swift in Sources */,

Example/iOS/AppDelegate.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
#if DEBUG
1919
let cache = DebuggableNormalizedCache(cache: InMemoryNormalizedCache())
2020
let store = ApolloStore(cache: cache)
21-
let interceptorProvider = DebuggableInterceptorProvider(LegacyInterceptorProvider(store: store))
22-
let underlyingNetworkTransport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
23-
let networkTransport = DebuggableNetworkTransport(networkTransport: underlyingNetworkTransport)
21+
let interceptorProvider = LegacyInterceptorProvider(store: store)
22+
let networkTransport = DebuggableRequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
2423
server = ApolloDebugServer(networkTransport: networkTransport, cache: cache)
2524
server.enableConsoleRedirection = true
2625
try! server.start(port: 8081)

Example/macOS/AppDelegate.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2424
#if DEBUG
2525
let cache = DebuggableNormalizedCache(cache: InMemoryNormalizedCache())
2626
let store = ApolloStore(cache: cache)
27-
let interceptorProvider = DebuggableInterceptorProvider(LegacyInterceptorProvider(store: store))
28-
let underlyingNetworkTransport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
29-
let networkTransport = DebuggableNetworkTransport(networkTransport: underlyingNetworkTransport)
27+
let interceptorProvider = LegacyInterceptorProvider(store: store)
28+
let networkTransport = DebuggableRequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
3029
server = ApolloDebugServer(networkTransport: networkTransport, cache: cache)
3130
server.enableConsoleRedirection = true
3231
try! server.start(port: 8081)

Sources/Classes/ApolloDebugServer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ extension ApolloDebugServer: DebuggableNormalizedCacheDelegate {
288288
// MARK: DebuggableNetworkTransportDelegate
289289

290290
extension ApolloDebugServer: DebuggableNetworkTransportDelegate {
291-
func networkTransport<Operation>(_ networkTransport: DebuggableNetworkTransport, willSendOperation operation: Operation) where Operation: GraphQLOperation {
291+
public func networkTransport<Operation>(_ networkTransport: DebuggableNetworkTransport, willSendOperation operation: Operation) where Operation: GraphQLOperation {
292292
if operation is AnyGraphQLOperation { return }
293293
operationStoreController.networkTransport(networkTransport, willSendOperation: operation)
294294
let chunk = chunkForCurrentState()
295295
eventStreams.broadcast(data: chunk.data)
296296
}
297297

298-
func networkTransport<Operation>(_ networkTransport: DebuggableNetworkTransport, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation: GraphQLOperation {
298+
public func networkTransport<Operation>(_ networkTransport: DebuggableNetworkTransport, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation: GraphQLOperation {
299299
if operation is AnyGraphQLOperation { return }
300300
operationStoreController.networkTransport(networkTransport, didSendOperation: operation, result: result)
301301
let chunk = chunkForCurrentState()

Sources/Classes/DebuggableNetworkTransport.swift

+3-33
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Apollo
1010

11-
protocol DebuggableNetworkTransportDelegate: class {
11+
public protocol DebuggableNetworkTransportDelegate: class {
1212
func networkTransport<Operation: GraphQLOperation>(_ networkTransport: DebuggableNetworkTransport, willSendOperation operation: Operation)
1313
func networkTransport<Operation: GraphQLOperation>(_ networkTransport: DebuggableNetworkTransport, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>)
1414
}
@@ -18,36 +18,6 @@ protocol DebuggableNetworkTransportDelegate: class {
1818
*
1919
* You should instantiate both `ApolloDebugServer` and `ApolloClient` with the same instance of this class.
2020
*/
21-
public class DebuggableNetworkTransport {
22-
public var clientName: String
23-
public var clientVersion: String
24-
weak var delegate: DebuggableNetworkTransportDelegate?
25-
private let networkTransport: NetworkTransport
26-
27-
/**
28-
* Initializes the receiver with the underlying network transport object.
29-
*
30-
* - Parameter networkTransport: The underlying network transport.
31-
*/
32-
public init(networkTransport: NetworkTransport) {
33-
self.networkTransport = networkTransport
34-
// Copies `clientName` and `clientVersion` in case someone wants to set
35-
// a different name or version from the original network transport.
36-
self.clientName = networkTransport.clientName
37-
self.clientVersion = networkTransport.clientVersion
38-
}
39-
}
40-
41-
// MARK: NetworkTransport
42-
43-
extension DebuggableNetworkTransport: NetworkTransport {
44-
public func send<Operation>(operation: Operation, cachePolicy: CachePolicy, contextIdentifier: UUID?, callbackQueue: DispatchQueue, completionHandler: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) -> Cancellable where Operation: GraphQLOperation {
45-
delegate?.networkTransport(self, willSendOperation: operation)
46-
return networkTransport.send(operation: operation, cachePolicy: cachePolicy, contextIdentifier: contextIdentifier, callbackQueue: callbackQueue) { [weak self] result in
47-
if let self = self {
48-
self.delegate?.networkTransport(self, didSendOperation: operation, result: result)
49-
}
50-
completionHandler(result)
51-
}
52-
}
21+
public protocol DebuggableNetworkTransport: NetworkTransport {
22+
var delegate: DebuggableNetworkTransportDelegate? { get set }
5323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// DebuggableRequestChainNetworkTransport.swift
3+
// ApolloDeveloperKit
4+
//
5+
// Created by Ryosuke Ito on 5/30/21.
6+
// Copyright © 2021 Ryosuke Ito. All rights reserved.
7+
//
8+
9+
import Apollo
10+
import Foundation
11+
12+
open class DebuggableRequestChainNetworkTransport: RequestChainNetworkTransport, DebuggableNetworkTransport {
13+
public weak var delegate: DebuggableNetworkTransportDelegate?
14+
private let debuggableInterceptorProvider: DebuggableInterceptorProvider
15+
16+
public override init(interceptorProvider: InterceptorProvider,
17+
endpointURL: URL,
18+
additionalHeaders: [String : String] = [:],
19+
autoPersistQueries: Bool = false,
20+
requestBodyCreator: RequestBodyCreator = ApolloRequestBodyCreator(),
21+
useGETForQueries: Bool = false,
22+
useGETForPersistedQueryRetry: Bool = false) {
23+
debuggableInterceptorProvider = DebuggableInterceptorProvider(interceptorProvider)
24+
super.init(interceptorProvider: debuggableInterceptorProvider,
25+
endpointURL: endpointURL,
26+
additionalHeaders: additionalHeaders,
27+
autoPersistQueries: autoPersistQueries,
28+
requestBodyCreator: requestBodyCreator,
29+
useGETForQueries: useGETForQueries,
30+
useGETForPersistedQueryRetry: useGETForPersistedQueryRetry)
31+
debuggableInterceptorProvider.delegate = self
32+
}
33+
}
34+
35+
extension DebuggableRequestChainNetworkTransport: DebuggableInterceptorProviderDelegate {
36+
func interceptorProvider<Operation>(_ interceptorProvider: DebuggableInterceptorProvider, willSendOperation operation: Operation) where Operation : GraphQLOperation {
37+
delegate?.networkTransport(self, willSendOperation: operation)
38+
}
39+
40+
func interceptorProvider<Operation>(_ interceptorProvider: DebuggableInterceptorProvider, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation : GraphQLOperation {
41+
delegate?.networkTransport(self, didSendOperation: operation, result: result)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// NotifyStartInterceptor.swift
3+
// ApolloDeveloperKit
4+
//
5+
// Created by Ryosuke Ito on 5/30/21.
6+
// Copyright © 2021 Ryosuke Ito. All rights reserved.
7+
//
8+
9+
import Apollo
10+
import Foundation
11+
12+
protocol DebugInitializeInterceptorDelegate: class {
13+
func interceptor<Operation>(_ interceptor: DebugInitializeInterceptor, willSendOperation operation: Operation) where Operation: GraphQLOperation
14+
func interceptor<Operation>(_ interceptor: DebugInitializeInterceptor, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation: GraphQLOperation
15+
}
16+
17+
public class DebugInitializeInterceptor: ApolloInterceptor {
18+
weak var delegate: DebugInitializeInterceptorDelegate?
19+
20+
public func interceptAsync<Operation>(chain: RequestChain, request: HTTPRequest<Operation>, response: HTTPResponse<Operation>?, completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) where Operation : GraphQLOperation {
21+
delegate?.interceptor(self, willSendOperation: request.operation)
22+
chain.proceedAsync(request: request,
23+
response: response) { [weak self] result in
24+
if let self = self {
25+
self.delegate?.interceptor(self, didSendOperation: request.operation, result: result)
26+
}
27+
completion(result)
28+
}
29+
}
30+
}

Sources/Classes/Network/Interceptor/DebuggableInterceptorProvider.swift

+23-3
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,43 @@
99
import Apollo
1010
import Foundation
1111

12+
protocol DebuggableInterceptorProviderDelegate: class {
13+
func interceptorProvider<Operation>(_ interceptorProvider: DebuggableInterceptorProvider, willSendOperation operation: Operation) where Operation: GraphQLOperation
14+
func interceptorProvider<Operation>(_ interceptorProvider: DebuggableInterceptorProvider, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation: GraphQLOperation
15+
}
16+
1217
public class DebuggableInterceptorProvider: InterceptorProvider {
18+
weak var delegate: DebuggableInterceptorProviderDelegate?
1319
private let interceptorProvider: InterceptorProvider
20+
private let debugInitializeInterceptor: DebugInitializeInterceptor
1421
private let debuggableResultTranslateInterceptor = DebuggableResultTranslateInterceptor()
1522

1623
public init(_ interceptorProvider: InterceptorProvider) {
1724
self.interceptorProvider = interceptorProvider
25+
debugInitializeInterceptor = DebugInitializeInterceptor()
26+
debugInitializeInterceptor.delegate = self
1827
}
1928

2029
public func interceptors<Operation>(for operation: Operation) -> [ApolloInterceptor] where Operation : GraphQLOperation {
30+
var interceptors = interceptorProvider.interceptors(for: operation)
31+
interceptors.insert(debugInitializeInterceptor, at: 0)
2132
if operation is AnyGraphQLOperation {
22-
return interceptorProvider.interceptors(for: operation) + [debuggableResultTranslateInterceptor]
23-
} else {
24-
return interceptorProvider.interceptors(for: operation)
33+
interceptors.append(debuggableResultTranslateInterceptor)
2534
}
35+
return interceptors
2636
}
2737

2838
public func additionalErrorInterceptor<Operation: GraphQLOperation>(for operation: Operation) -> ApolloErrorInterceptor? {
2939
return interceptorProvider.additionalErrorInterceptor(for: operation)
3040
}
3141
}
42+
43+
extension DebuggableInterceptorProvider: DebugInitializeInterceptorDelegate {
44+
func interceptor<Operation>(_ interceptor: DebugInitializeInterceptor, willSendOperation operation: Operation) where Operation : GraphQLOperation {
45+
delegate?.interceptorProvider(self, willSendOperation: operation)
46+
}
47+
48+
func interceptor<Operation>(_ interceptor: DebugInitializeInterceptor, didSendOperation operation: Operation, result: Result<GraphQLResult<Operation.Data>, Error>) where Operation : GraphQLOperation {
49+
delegate?.interceptorProvider(self, didSendOperation: operation, result: result)
50+
}
51+
}

0 commit comments

Comments
 (0)