Skip to content
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

Merged
merged 2 commits into from
Jul 24, 2024

Conversation

calvincestari
Copy link
Member

@calvincestari calvincestari commented Jul 24, 2024

The defer execution changes merged from PR #413 introduced a bug where the cache write interceptor would throw if no cache records were returned. This is incorrect as it is a valid response for the data key to be missing and only an errors key returned, such as in the case of a request error.

This changeset is to allow the cache write interceptor to gracefully handle missing cache records and only publish records to the cache if they were returned from parsing.

  • Adds a test that verifies the default interceptor chain successfully parses a request error
  • Removes deprecation attribute for noResponseToParse error
  • Removes error missingCacheRecords since it's no longer valid (breaking change)
  • Moves the chain cancellation check earlier in the interceptAsync of CacheWriteInterceptor method to return at the earliest point

This was first reported as a result of #428 not getting a clean CI build.

* Move chain cancellation check earlier
* Do not expect cache records to be returned
* Only write to cache if cache records were returned
Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for eclectic-pie-88a2ba canceled.

Name Link
🔨 Latest commit 9f9e586
🔍 Latest deploy log https://app.netlify.com/sites/eclectic-pie-88a2ba/deploys/66a134919ab62500084de43a

Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for apollo-ios-docc canceled.

Name Link
🔨 Latest commit 9f9e586
🔍 Latest deploy log https://app.netlify.com/sites/apollo-ios-docc/deploys/66a13491301e580008f886fd

@@ -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.")
Copy link
Member Author

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.

case noResponseToParse

case missingCacheRecords
Copy link
Member Author

Choose a reason for hiding this comment

The 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 1.14.0 I don't see this as being too impactful. Like the deprecation of noResponseToParse it would not have been introduced if we'd caught the bug earlier.

@@ -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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this earlier in the interceptAsync logic because it makes sense that if the chain has already been cancelled you would not want to receive any response, nor should the interceptor chain proceed forward.

Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT. Thanks for the fix!

@calvincestari calvincestari merged commit 9bde5f1 into main Jul 24, 2024
34 checks passed
@calvincestari calvincestari deleted the fix/cache-write-interceptor-missing-data branch July 24, 2024 19:04
BobaFetters pushed a commit to apollographql/apollo-ios that referenced this pull request Jul 24, 2024
BobaFetters pushed a commit that referenced this pull request Jul 24, 2024
39098c59 fix: Cache write interceptor should gracefully handle missing cache records (#439)

git-subtree-dir: apollo-ios
git-subtree-split: 39098c59162100bcc5a1483ca1a86b6f12e68c01
BobaFetters pushed a commit that referenced this pull request Jul 24, 2024
…cefully handle missing cache records

git-subtree-dir: apollo-ios
git-subtree-mainline: 0f86e27
git-subtree-split: 39098c59162100bcc5a1483ca1a86b6f12e68c01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants