forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONRequest.swift
More file actions
195 lines (174 loc) · 6.93 KB
/
JSONRequest.swift
File metadata and controls
195 lines (174 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import Foundation
#if !COCOAPODS
import ApolloAPI
#endif
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/// A request which sends JSON related to a GraphQL operation.
open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {
public let requestBodyCreator: any RequestBodyCreator
public let autoPersistQueries: Bool
public let useGETForQueries: Bool
public let useGETForPersistedQueryRetry: Bool
public var isPersistedQueryRetry = false {
didSet {
_body = nil
}
}
private var _body: JSONEncodableDictionary?
public var body: JSONEncodableDictionary {
if _body == nil {
_body = createBody()
}
return _body!
}
public let serializationFormat = JSONSerializationFormat.self
/// Designated initializer
///
/// - Parameters:
/// - operation: The GraphQL Operation to execute
/// - graphQLEndpoint: The endpoint to make a GraphQL request to
/// - contextIdentifier: [optional] A unique identifier for this request, to help with deduping cache hits for watchers. Defaults to `nil`.
/// - clientName: The name of the client to send with the `"apollographql-client-name"` header
/// - clientVersion: The version of the client to send with the `"apollographql-client-version"` header
/// - additionalHeaders: Any additional headers you wish to add by default to this request
/// - cachePolicy: The `CachePolicy` to use for this request.
/// - context: [optional] A context that is being passed through the request chain. Defaults to `nil`.
/// - autoPersistQueries: `true` if Auto-Persisted Queries should be used. Defaults to `false`.
/// - useGETForQueries: `true` if Queries should use `GET` instead of `POST` for HTTP requests. Defaults to `false`.
/// - useGETForPersistedQueryRetry: `true` if when an Auto-Persisted query is retried, it should use `GET` instead of `POST` to send the query. Defaults to `false`.
/// - requestBodyCreator: An object conforming to the `RequestBodyCreator` protocol to assist with creating the request body. Defaults to the provided `ApolloRequestBodyCreator` implementation.
public init(
operation: Operation,
graphQLEndpoint: URL,
contextIdentifier: UUID? = nil,
clientName: String,
clientVersion: String,
additionalHeaders: [String: String] = [:],
cachePolicy: CachePolicy = .default,
context: (any RequestContext)? = nil,
autoPersistQueries: Bool = false,
useGETForQueries: Bool = false,
useGETForPersistedQueryRetry: Bool = false,
requestBodyCreator: any RequestBodyCreator = ApolloRequestBodyCreator()
) {
self.autoPersistQueries = autoPersistQueries
self.useGETForQueries = useGETForQueries
self.useGETForPersistedQueryRetry = useGETForPersistedQueryRetry
self.requestBodyCreator = requestBodyCreator
super.init(
graphQLEndpoint: graphQLEndpoint,
operation: operation,
contextIdentifier: contextIdentifier,
contentType: "application/json",
clientName: clientName,
clientVersion: clientVersion,
additionalHeaders: additionalHeaders,
cachePolicy: cachePolicy,
context: context
)
}
open override func toURLRequest() throws -> URLRequest {
var request = try super.toURLRequest()
let useGetMethod: Bool
let body = self.body
switch Operation.operationType {
case .query:
if isPersistedQueryRetry {
useGetMethod = self.useGETForPersistedQueryRetry
} else {
useGetMethod = self.useGETForQueries || (self.autoPersistQueries && self.useGETForPersistedQueryRetry)
}
default:
useGetMethod = false
}
let httpMethod: GraphQLHTTPMethod = useGetMethod ? .GET : .POST
switch httpMethod {
case .GET:
let transformer = GraphQLGETTransformer(body: body, url: self.graphQLEndpoint)
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 {
throw GraphQLHTTPRequestError.serializedQueryParamsMessageError
}
case .POST:
do {
request.httpBody = try serializationFormat.serialize(value: body)
request.httpMethod = GraphQLHTTPMethod.POST.rawValue
} catch {
throw GraphQLHTTPRequestError.serializedBodyMessageError
}
}
return request
}
private func createBody() -> JSONEncodableDictionary {
let sendQueryDocument: Bool
let autoPersistQueries: Bool
switch Operation.operationType {
case .query:
if isPersistedQueryRetry {
sendQueryDocument = true
autoPersistQueries = true
} else {
sendQueryDocument = !self.autoPersistQueries
autoPersistQueries = self.autoPersistQueries
}
case .mutation:
if isPersistedQueryRetry {
sendQueryDocument = true
autoPersistQueries = true
} else {
sendQueryDocument = !self.autoPersistQueries
autoPersistQueries = self.autoPersistQueries
}
default:
sendQueryDocument = true
autoPersistQueries = false
}
let body = self.requestBodyCreator.requestBody(
for: operation,
sendQueryDocument: sendQueryDocument,
autoPersistQuery: autoPersistQueries
)
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 .useProtocolCachePolicy
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 {
lhs as HTTPRequest<Operation> == rhs as HTTPRequest<Operation> &&
type(of: lhs.requestBodyCreator) == type(of: rhs.requestBodyCreator) &&
lhs.autoPersistQueries == rhs.autoPersistQueries &&
lhs.useGETForQueries == rhs.useGETForQueries &&
lhs.useGETForPersistedQueryRetry == rhs.useGETForPersistedQueryRetry &&
lhs.isPersistedQueryRetry == rhs.isPersistedQueryRetry &&
lhs.body._jsonObject == rhs.body._jsonObject
}
public override func hash(into hasher: inout Hasher) {
super.hash(into: &hasher)
hasher.combine("\(type(of: requestBodyCreator))")
hasher.combine(autoPersistQueries)
hasher.combine(useGETForQueries)
hasher.combine(useGETForPersistedQueryRetry)
hasher.combine(isPersistedQueryRetry)
hasher.combine(body._jsonObject)
}
}