-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cleanup * Move LegacyInterceptorProvider to its own file * Inject WebSocket into WebSocketTransport * Rename LegacyCacheInterceptors to remove “Legacy” * Kill FlexibleDecoder and Parseable * Cleanup and rename LegacyParsingInterceptor to JSONResponseParsingInterceptor * Make unneeded classes into structs * Rename LegacyInterceptorProvider -> DefaultInterceptorProvider * Fixed a few more instances of “Legacy” * Fixed Integration Tests * Fixed more references to renamed “legacy” interceptors * minor fix
- Loading branch information
1 parent
fceb28b
commit 57c07f1
Showing
38 changed files
with
294 additions
and
405 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
4 changes: 2 additions & 2 deletions
4
...s/Apollo/LegacyCacheReadInterceptor.swift → Sources/Apollo/CacheReadInterceptor.swift
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Foundation | ||
|
||
/// 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) -> [ApolloInterceptor] { | ||
return [ | ||
MaxRetryInterceptor(), | ||
CacheReadInterceptor(store: self.store), | ||
NetworkFetchInterceptor(client: self.client), | ||
ResponseCodeInterceptor(), | ||
JSONResponseParsingInterceptor(cacheKeyForObject: self.store.cacheKeyForObject), | ||
AutomaticPersistedQueryInterceptor(), | ||
CacheWriteInterceptor(store: self.store), | ||
] | ||
} | ||
|
||
open func additionalErrorInterceptor<Operation: GraphQLOperation>(for operation: Operation) -> ApolloErrorInterceptor? { | ||
return nil | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Foundation | ||
|
||
/// An interceptor which parses JSON response data into a `GraphQLResult` and attaches it to the `HTTPResponse`. | ||
public struct JSONResponseParsingInterceptor: ApolloInterceptor { | ||
|
||
public enum JSONResponseParsingError: Error, LocalizedError { | ||
case noResponseToParse | ||
case couldNotParseToJSON(data: Data) | ||
|
||
public var errorDescription: String? { | ||
switch self { | ||
case .noResponseToParse: | ||
return "The Codable Parsing Interceptor was called before a response was received to be parsed. Double-check the order of your interceptors." | ||
case .couldNotParseToJSON(let data): | ||
var errorStrings = [String]() | ||
errorStrings.append("Could not parse data to JSON format.") | ||
if let dataString = String(bytes: data, encoding: .utf8) { | ||
errorStrings.append("Data received as a String was:") | ||
errorStrings.append(dataString) | ||
} else { | ||
errorStrings.append("Data of count \(data.count) also could not be parsed into a String.") | ||
} | ||
|
||
return errorStrings.joined(separator: " ") | ||
} | ||
} | ||
} | ||
|
||
public let cacheKeyForObject: CacheKeyForObject? | ||
|
||
/// Designated Initializer | ||
public init(cacheKeyForObject: CacheKeyForObject? = nil) { | ||
self.cacheKeyForObject = cacheKeyForObject | ||
} | ||
|
||
public func interceptAsync<Operation: GraphQLOperation>( | ||
chain: RequestChain, | ||
request: HTTPRequest<Operation>, | ||
response: HTTPResponse<Operation>?, | ||
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void | ||
) { | ||
guard let createdResponse = response else { | ||
chain.handleErrorAsync(JSONResponseParsingError.noResponseToParse, | ||
request: request, | ||
response: response, | ||
completion: completion) | ||
return | ||
} | ||
|
||
do { | ||
guard let body = try? JSONSerializationFormat | ||
.deserialize(data: createdResponse.rawData) as? JSONObject else { | ||
throw JSONResponseParsingError.couldNotParseToJSON(data: createdResponse.rawData) | ||
} | ||
|
||
let graphQLResponse = GraphQLResponse(operation: request.operation, body: body) | ||
createdResponse.legacyResponse = graphQLResponse | ||
|
||
|
||
let result = try parseResult(from: graphQLResponse, cachePolicy: request.cachePolicy) | ||
createdResponse.parsedResponse = result | ||
chain.proceedAsync(request: request, | ||
response: createdResponse, | ||
completion: completion) | ||
|
||
} catch { | ||
chain.handleErrorAsync(error, | ||
request: request, | ||
response: createdResponse, | ||
completion: completion) | ||
} | ||
} | ||
|
||
private func parseResult<Data>( | ||
from response: GraphQLResponse<Data>, | ||
cachePolicy: CachePolicy | ||
) throws -> GraphQLResult<Data> { | ||
switch cachePolicy { | ||
case .fetchIgnoringCacheCompletely: | ||
// There is no cache, so we don't need to get any info on dependencies. Use fast parsing. | ||
return try response.parseResultFast() | ||
default: | ||
let (parsedResult, _) = try response.parseResult(cacheKeyForObject: self.cacheKeyForObject) | ||
return parsedResult | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.