Skip to content

Commit 0bde055

Browse files
Update HTTP response to be optional until created.
1 parent 6ad66ee commit 0bde055

13 files changed

Lines changed: 68 additions & 50 deletions

Sources/Apollo/ApolloErrorInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public protocol ApolloErrorInterceptor {
99
/// - error: The received error
1010
/// - chain: The chain the error was received on
1111
/// - request: The request, as far as it was constructed
12-
/// - response: The response, as far as it was constructed
12+
/// - response: [optional] The response, if one was received
1313
/// - completion: The completion closure to fire when the operation has completed. Note that if you call `retry` on the chain, you will not want to call the completion block in this method.
1414
func handleErrorAsync<Operation: GraphQLOperation>(
1515
error: Error,
1616
chain: RequestChain,
1717
request: HTTPRequest<Operation>,
18-
response: HTTPResponse<Operation>,
18+
response: HTTPResponse<Operation>?,
1919
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void)
2020
}

Sources/Apollo/ApolloInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public protocol ApolloInterceptor: class {
66
/// - Parameters:
77
/// - chain: The chain the interceptor is a part of.
88
/// - request: The request, as far as it has been constructed
9-
/// - response: The response, as far as it has been constructed
9+
/// - response: [optional] The response, if received
1010
/// - completion: The completion block to fire when data needs to be returned to the UI.
1111
func interceptAsync<Operation: GraphQLOperation>(
1212
chain: RequestChain,
1313
request: HTTPRequest<Operation>,
14-
response: HTTPResponse<Operation>,
14+
response: HTTPResponse<Operation>?,
1515
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void)
1616
}

Sources/Apollo/AutomaticPersistedQueryInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AutomaticPersistedQueryInterceptor: ApolloInterceptor {
99
public func interceptAsync<Operation: GraphQLOperation>(
1010
chain: RequestChain,
1111
request: HTTPRequest<Operation>,
12-
response: HTTPResponse<Operation>,
12+
response: HTTPResponse<Operation>?,
1313
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
1414

1515
guard
@@ -22,7 +22,7 @@ public class AutomaticPersistedQueryInterceptor: ApolloInterceptor {
2222
return
2323
}
2424

25-
guard let result = response.parsedResponse else {
25+
guard let result = response?.parsedResponse else {
2626
// This is in the wrong order - this needs to be parsed before we can check it.
2727
chain.handleErrorAsync(APQError.noParsedResponse,
2828
request: request,

Sources/Apollo/CodableParsingInterceptor.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Foundation
22

33
public enum ParserError: Error {
44
case nilData
5+
case noResponseToParse
56
case couldNotParseToLegacyJSON
67
}
78

@@ -18,25 +19,39 @@ public class CodableParsingInterceptor<FlexDecoder: FlexibleDecoder>: ApolloInte
1819
public func interceptAsync<Operation: GraphQLOperation>(
1920
chain: RequestChain,
2021
request: HTTPRequest<Operation>,
21-
response: HTTPResponse<Operation>,
22+
response: HTTPResponse<Operation>?,
2223
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
2324
guard !self.isCancelled else {
2425
return
2526
}
2627

27-
guard let data = response.rawData else {
28-
completion(.failure(ParserError.nilData))
28+
guard let createdResponse = response else {
29+
chain.handleErrorAsync(ParserError.noResponseToParse,
30+
request: request,
31+
response: response,
32+
completion: completion)
33+
return
34+
}
35+
36+
guard let data = response?.rawData else {
37+
chain.handleErrorAsync(ParserError.nilData,
38+
request: request,
39+
response: createdResponse,
40+
completion: completion)
2941
return
3042
}
3143

3244
do {
3345
let parsedData = try GraphQLResult<Operation.Data>(from: data, decoder: self.decoder)
34-
response.parsedResponse = parsedData
46+
createdResponse.parsedResponse = parsedData
3547
chain.proceedAsync(request: request,
3648
response: response,
3749
completion: completion)
3850
} catch {
39-
completion(.failure(error))
51+
chain.handleErrorAsync(error,
52+
request: request,
53+
response: createdResponse,
54+
completion: completion)
4055
}
4156
}
4257
}

Sources/Apollo/FinalizingInterceptor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class FinalizingInterceptor: ApolloInterceptor {
1010
public func interceptAsync<Operation: GraphQLOperation>(
1111
chain: RequestChain,
1212
request: HTTPRequest<Operation>,
13-
response: HTTPResponse<Operation>,
13+
response: HTTPResponse<Operation>?,
1414
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
1515

16-
guard let parsed = response.parsedResponse else {
17-
chain.handleErrorAsync(FinalizationError.nilParsedValue(httpResponse: response.httpResponse,
18-
rawData: response.rawData),
16+
guard let parsed = response?.parsedResponse else {
17+
chain.handleErrorAsync(FinalizationError.nilParsedValue(httpResponse: response?.httpResponse,
18+
rawData: response?.rawData),
1919
request: request,
2020
response: response,
2121
completion: completion)

Sources/Apollo/HTTPResponse.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import Foundation
22

33
/// Data about a response received by an HTTP request.
44
public class HTTPResponse<Operation: GraphQLOperation> {
5-
public var httpResponse: HTTPURLResponse?
5+
public var httpResponse: HTTPURLResponse
66
public var rawData: Data?
77
public var parsedResponse: GraphQLResult<Operation.Data>?
88

99
/// Designated initializer
1010
///
1111
/// - Parameters:
12-
/// - response: [optional] The `HTTPURLResponse` received from the server. Will be nil if not yet received or if the response received was not an `HTTPURLResponse`.
13-
/// - rawData: [optional] The raw, unparsed data received from the server. Will be nil if not yet received or if data received from the server was nil.
14-
/// - parsedResponse: [optional] The response parsed into the `ParsedValue` type. Will be nil if not yet received, not yet parsed, or if parsing failed.
15-
public init(response: HTTPURLResponse?,
12+
/// - response: The `HTTPURLResponse` received from the server.
13+
/// - rawData: [optional] The raw, unparsed data received from the server. Will be nil if data received from the server was nil.
14+
/// - parsedResponse: [optional] The response parsed into the `ParsedValue` type. Will be nil if not yet parsed, or if parsing failed.
15+
public init(response: HTTPURLResponse,
1616
rawData: Data?,
1717
parsedResponse: GraphQLResult<Operation.Data>?) {
1818
self.httpResponse = response

Sources/Apollo/LegacyCacheReadInterceptor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class LegacyCacheReadInterceptor: ApolloInterceptor {
1919
public func interceptAsync<Operation: GraphQLOperation>(
2020
chain: RequestChain,
2121
request: HTTPRequest<Operation>,
22-
response: HTTPResponse<Operation>,
22+
response: HTTPResponse<Operation>?,
2323
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
2424

2525
switch request.operation.operationType {

Sources/Apollo/LegacyCacheWriteInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class LegacyCacheWriteInterceptor: ApolloInterceptor {
1515
public func interceptAsync<Operation: GraphQLOperation>(
1616
chain: RequestChain,
1717
request: HTTPRequest<Operation>,
18-
response: HTTPResponse<Operation>,
18+
response: HTTPResponse<Operation>?,
1919
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
2020

2121
guard request.cachePolicy != .fetchIgnoringCacheCompletely else {
@@ -26,7 +26,7 @@ public class LegacyCacheWriteInterceptor: ApolloInterceptor {
2626
return
2727
}
2828

29-
guard let data = response.rawData else {
29+
guard let data = response?.rawData else {
3030
chain.handleErrorAsync(ParserError.nilData,
3131
request: request,
3232
response: response,

Sources/Apollo/LegacyParsingInterceptor.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ public class LegacyParsingInterceptor: ApolloInterceptor {
66
public func interceptAsync<Operation: GraphQLOperation>(
77
chain: RequestChain,
88
request: HTTPRequest<Operation>,
9-
response: HTTPResponse<Operation>,
9+
response: HTTPResponse<Operation>?,
1010
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
1111

12-
guard let data = response.rawData else {
13-
chain.handleErrorAsync(ParserError.nilData,
14-
request: request,
15-
response: response,
16-
completion: completion)
12+
guard
13+
let createdResponse = response,
14+
let data = createdResponse.rawData else {
15+
chain.handleErrorAsync(ParserError.nilData,
16+
request: request,
17+
response: response,
18+
completion: completion)
1719
return
1820
}
1921

@@ -25,11 +27,12 @@ public class LegacyParsingInterceptor: ApolloInterceptor {
2527

2628
let graphQLResponse = GraphQLResponse(operation: request.operation, body: body)
2729
let parsedResult = try graphQLResponse.parseResultFast()
28-
let typedResult = parsedResult
29-
response.parsedResponse = typedResult
30+
let typedResult = parsedResult
31+
32+
createdResponse.parsedResponse = typedResult
3033

3134
chain.proceedAsync(request: request,
32-
response: response,
35+
response: createdResponse,
3336
completion: completion)
3437

3538
} catch {

Sources/Apollo/MaxRetryInterceptor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MaxRetryInterceptor: ApolloInterceptor {
1919
public func interceptAsync<Operation: GraphQLOperation>(
2020
chain: RequestChain,
2121
request: HTTPRequest<Operation>,
22-
response: HTTPResponse<Operation>,
22+
response: HTTPResponse<Operation>?,
2323
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {
2424
guard request.retryCount <= self.maxRetries else {
2525
let error = RetryError.hitMaxRetryCount(count: self.maxRetries,

0 commit comments

Comments
 (0)