-
-
Save thexande/875296283fcc0d7977bfe42dd9b95e1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Apollo | |
final class MockNetworkTransport: NetworkTransport { | |
enum NetworkError: Error { | |
case networkFailure | |
} | |
private final class MockTask: Cancellable { | |
func cancel() { } | |
} | |
let simulateNetworkFailure: Bool | |
let body: JSONObject | |
init(body: JSONObject, simulateNetworkFailure: Bool = false) { | |
self.body = body | |
self.simulateNetworkFailure = simulateNetworkFailure | |
} | |
func send(operation: Operation, | |
completionHandler: @escaping (_ response: GraphQLResponse?, _ error: Error?) -> Void) -> Cancellable { | |
DispatchQueue.global(qos: .default).async { | |
if self.simulateNetworkFailure { | |
completionHandler(nil, NetworkError.networkFailure) | |
return | |
} | |
completionHandler(GraphQLResponse(operation: operation, | |
body: self.body), nil) | |
} | |
return MockTask() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment