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

enhancement: Set URLRequest cache policy on GET requests #476

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
19 changes: 18 additions & 1 deletion apollo-ios/Sources/Apollo/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {
if let urlForGet = transformer.createGetURL() {
request.url = urlForGet
request.httpMethod = GraphQLHTTPMethod.GET.rawValue

request.cachePolicy = requestCachePolicy

// GET requests shouldn't have a content-type since they do not provide actual content.
request.allHTTPHeaderFields?.removeValue(forKey: "Content-Type")
} else {
Expand Down Expand Up @@ -150,6 +151,22 @@ open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {
return body
}

/// Convert the Apollo iOS cache policy into a matching cache policy for URLRequest.
private var requestCachePolicy: URLRequest.CachePolicy {
switch cachePolicy {
case .returnCacheDataElseFetch:
return .returnCacheDataElseLoad
case .fetchIgnoringCacheData:
return .reloadIgnoringLocalCacheData
case .fetchIgnoringCacheCompletely:
return .reloadIgnoringLocalAndRemoteCacheData
case .returnCacheDataDontFetch:
return .returnCacheDataDontLoad
case .returnCacheDataAndFetch:
return .reloadRevalidatingCacheData
}
}

// MARK: - Equtable/Hashable Conformance

public static func == (lhs: JSONRequest<Operation>, rhs: JSONRequest<Operation>) -> Bool {
Expand Down
Loading