Skip to content

Commit

Permalink
enhancement: Set URLRequest cache policy on GET requests (apollograph…
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Sep 11, 2024
1 parent f0c89da commit 6620447
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion 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

0 comments on commit 6620447

Please sign in to comment.