Skip to content

Commit

Permalink
Enable lazy access to the request body creation for leverage in custo…
Browse files Browse the repository at this point in the history
…m built interceptors (apollographql#2184)
  • Loading branch information
rickfast authored and Maxim Globak committed Jun 1, 2022
1 parent 3320aba commit a3b5f5a
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions Sources/Apollo/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {
public let serializationFormat = JSONSerializationFormat.self

/// Designated initializer
///
///
/// - Parameters:
/// - operation: The GraphQL Operation to execute
/// - graphQLEndpoint: The endpoint to make a GraphQL request to
Expand Down Expand Up @@ -58,50 +58,30 @@ open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {

open override func toURLRequest() throws -> URLRequest {
var request = try super.toURLRequest()

let useGetMethod: Bool
let sendQueryDocument: Bool
let autoPersistQueries: Bool
let body = self.body

switch operation.operationType {
case .query:
if isPersistedQueryRetry {
useGetMethod = self.useGETForPersistedQueryRetry
sendQueryDocument = true
autoPersistQueries = true
} else {
useGetMethod = self.useGETForQueries || (self.autoPersistQueries && self.useGETForPersistedQueryRetry)
sendQueryDocument = !self.autoPersistQueries
autoPersistQueries = self.autoPersistQueries
}
case .mutation:
useGetMethod = false
if isPersistedQueryRetry {
sendQueryDocument = true
autoPersistQueries = true
} else {
sendQueryDocument = !self.autoPersistQueries
autoPersistQueries = self.autoPersistQueries
}
default:
useGetMethod = false
sendQueryDocument = true
autoPersistQueries = false
}

let body = self.requestBodyCreator.requestBody(for: operation,
sendOperationIdentifiers: self.sendOperationIdentifier,
sendQueryDocument: sendQueryDocument,
autoPersistQuery: autoPersistQueries)

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

// GET requests shouldn't have a content-type since they do not provide actual content.
// 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
Expand All @@ -117,4 +97,37 @@ open class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {

return request
}

public private(set) lazy var body: GraphQLMap = {
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,
sendOperationIdentifiers: self.sendOperationIdentifier,
sendQueryDocument: sendQueryDocument,
autoPersistQuery: autoPersistQueries)

return body
}()
}

0 comments on commit a3b5f5a

Please sign in to comment.