-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Cache write interceptor should gracefully handle missing cache records #439
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,12 @@ import ApolloAPI | |
public struct CacheWriteInterceptor: ApolloInterceptor { | ||
|
||
public enum CacheWriteError: Error, LocalizedError { | ||
@available(*, deprecated, message: "Will be removed in a future version.") | ||
case noResponseToParse | ||
|
||
case missingCacheRecords | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguably this is a breaking change but given the short availability of release |
||
|
||
public var errorDescription: String? { | ||
switch self { | ||
case .noResponseToParse: | ||
return "The Cache Write Interceptor was called before a response was received to be parsed. Double-check the order of your interceptors." | ||
case .missingCacheRecords: | ||
return "The Cache Write Interceptor cannot find any cache records. Double-check the order of your interceptors." | ||
} | ||
} | ||
} | ||
|
@@ -37,7 +32,11 @@ public struct CacheWriteInterceptor: ApolloInterceptor { | |
request: HTTPRequest<Operation>, | ||
response: HTTPResponse<Operation>?, | ||
completion: @escaping (Result<GraphQLResult<Operation.Data>, any Error>) -> Void) { | ||
|
||
|
||
guard !chain.isCancelled else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've moved this earlier in the |
||
return | ||
} | ||
|
||
guard request.cachePolicy != .fetchIgnoringCacheCompletely else { | ||
// If we're ignoring the cache completely, we're not writing to it. | ||
chain.proceedAsync( | ||
|
@@ -49,25 +48,20 @@ public struct CacheWriteInterceptor: ApolloInterceptor { | |
return | ||
} | ||
|
||
guard | ||
let createdResponse = response, | ||
let cacheRecords = createdResponse.cacheRecords | ||
else { | ||
guard let createdResponse = response else { | ||
chain.handleErrorAsync( | ||
CacheWriteError.missingCacheRecords, | ||
CacheWriteError.noResponseToParse, | ||
request: request, | ||
response: response, | ||
completion: completion | ||
) | ||
return | ||
} | ||
|
||
guard !chain.isCancelled else { | ||
return | ||
if let cacheRecords = createdResponse.cacheRecords { | ||
self.store.publish(records: cacheRecords, identifier: request.contextIdentifier) | ||
} | ||
|
||
self.store.publish(records: cacheRecords, identifier: request.contextIdentifier) | ||
|
||
chain.proceedAsync( | ||
request: request, | ||
response: createdResponse, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was deprecated because in the defer execution PR it was no longer being used. Had we caught this bug then it would never have been deprecated.