-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathRequestBodyCreator.swift
More file actions
64 lines (53 loc) · 2.33 KB
/
RequestBodyCreator.swift
File metadata and controls
64 lines (53 loc) · 2.33 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
import Foundation
#if !COCOAPODS
import ApolloCore
#endif
public protocol RequestBodyCreator {
/// Creates a `GraphQLMap` out of the passed-in operation
///
/// - Parameters:
/// - operation: The operation to use
/// - sendOperationIdentifiers: Whether or not to send operation identifiers. Should default to `false`.
/// - sendQueryDocument: Whether or not to send the full query document. Should default to `true`.
/// - autoPersistQuery: Whether to use auto-persisted query information. Should default to `false`.
/// - Returns: The created `GraphQLMap`
func requestBody<Operation: GraphQLOperation>(for operation: Operation,
sendOperationIdentifiers: Bool,
sendQueryDocument: Bool,
autoPersistQuery: Bool) -> GraphQLMap
}
// MARK: - Default Implementation
extension RequestBodyCreator {
public func requestBody<Operation: GraphQLOperation>(for operation: Operation,
sendOperationIdentifiers: Bool,
sendQueryDocument: Bool,
autoPersistQuery: Bool) -> GraphQLMap {
var body: GraphQLMap = [
"variables": operation.variables,
"operationName": operation.operationName,
]
if sendOperationIdentifiers {
guard let operationIdentifier = operation.operationIdentifier else {
preconditionFailure("To send operation identifiers, Apollo types must be generated with operationIdentifiers")
}
body["id"] = operationIdentifier
}
if sendQueryDocument {
body["query"] = operation.queryDocument
}
if autoPersistQuery {
guard let operationIdentifier = operation.operationIdentifier else {
preconditionFailure("To enable `autoPersistQueries`, Apollo types must be generated with operationIdentifiers")
}
body["extensions"] = [
"persistedQuery" : ["sha256Hash": operationIdentifier, "version": 1]
]
}
return body
}
}
// Helper struct to create requests independently of HTTP operations.
public struct ApolloRequestBodyCreator: RequestBodyCreator {
// Internal init methods cannot be used in public methods
public init() { }
}