Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions apollo-ios/Sources/Apollo/RequestChain/GraphQLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ extension GraphQLRequest {
request.timeoutInterval = requestTimeout
}

request.cachePolicy = requestCachePolicy

return request
}

Expand All @@ -79,5 +81,38 @@ extension GraphQLRequest {
public mutating func addHeaders(_ headers: [String: String]) {
self.additionalHeaders.merge(headers) { (_, new) in new }
}

/// Convert the Apollo iOS cache policy into a matching cache policy for URLRequest.
/// Ensures URL cache doesn't interfere with Apollo's cache strategy.
private var requestCachePolicy: URLRequest.CachePolicy {
switch (fetchBehavior.cacheRead, fetchBehavior.networkFetch) {
/// CacheOnly
case (.beforeNetworkFetch, .never):
return .returnCacheDataDontLoad

/// CacheAndNetwork
case (.beforeNetworkFetch, .always):
return .reloadIgnoringLocalCacheData

/// CacheFirst
case (.beforeNetworkFetch, .onCacheMiss):
return .returnCacheDataElseLoad

/// NetworkFirst
case (.onNetworkFailure, .always):
return .reloadIgnoringLocalCacheData

/// NetworkOnly
case (.never, .always):
return .reloadIgnoringLocalCacheData

/// Impossible / nondefined combinations - Safest fallback is fetch from the server to avoid stale URL cache
case (.onNetworkFailure, .never),
(.onNetworkFailure, .onCacheMiss),
(.never, .never),
(.never, .onCacheMiss):
return .reloadIgnoringLocalCacheData
}
}

}
Loading