11import Foundation
22
3- // Helper struct to create requests independently of HTTP operations.
4- public struct RequestCreator {
5-
3+ public protocol RequestCreator {
4+ /// Creates a `GraphQLMap` out of the passed-in operation
5+ ///
6+ /// - Parameters:
7+ /// - operation: The operation to use
8+ /// - sendOperationIdentifiers: Whether or not to send operation identifiers. Defaults to false.
9+ /// - Returns: The created `GraphQLMap`
10+ func requestBody< Operation: GraphQLOperation > ( for operation: Operation , sendOperationIdentifiers: Bool ) -> GraphQLMap
11+
12+ /// Creates multi-part form data to send with a request
13+ ///
14+ /// - Parameters:
15+ /// - operation: The operation to create the data for.
16+ /// - files: An array of files to use.
17+ /// - sendOperationIdentifiers: True if operation identifiers should be sent, false if not.
18+ /// - serializationFormat: The format to use to serialize data.
19+ /// - manualBoundary: [optional] A manual boundary to pass in. A default boundary will be used otherwise.
20+ /// - Returns: The created form data
21+ /// - Throws: Errors creating or loading the form data
22+ func requestMultipartFormData< Operation: GraphQLOperation > ( for operation: Operation ,
23+ files: [ GraphQLFile ] ,
24+ sendOperationIdentifiers: Bool ,
25+ serializationFormat: JSONSerializationFormat . Type ,
26+ manualBoundary: String ? ) throws -> MultipartFormData
27+ }
28+
29+ extension RequestCreator {
630 /// Creates a `GraphQLMap` out of the passed-in operation
731 ///
832 /// - Parameters:
933 /// - operation: The operation to use
1034 /// - sendOperationIdentifiers: Whether or not to send operation identifiers. Defaults to false.
1135 /// - Returns: The created `GraphQLMap`
12- public static func requestBody< Operation: GraphQLOperation > ( for operation: Operation , sendOperationIdentifiers: Bool = false ) -> GraphQLMap {
36+ public func requestBody< Operation: GraphQLOperation > ( for operation: Operation , sendOperationIdentifiers: Bool = false ) -> GraphQLMap {
1337 var body : GraphQLMap = [
1438 " variables " : operation. variables,
1539 " operationName " : operation. operationName,
1640 ]
17-
41+
1842 if sendOperationIdentifiers {
1943 guard let operationIdentifier = operation. operationIdentifier else {
2044 preconditionFailure ( " To send operation identifiers, Apollo types must be generated with operationIdentifiers " )
2145 }
22-
46+
2347 body [ " id " ] = operationIdentifier
2448 } else {
2549 body [ " query " ] = operation. queryDocument
2650 }
27-
51+
2852 return body
2953 }
30-
54+
3155 /// Creates multi-part form data to send with a request
3256 ///
3357 /// - Parameters:
@@ -38,7 +62,7 @@ public struct RequestCreator {
3862 /// - manualBoundary: [optional] A manual boundary to pass in. A default boundary will be used otherwise.
3963 /// - Returns: The created form data
4064 /// - Throws: Errors creating or loading the form data
41- static func requestMultipartFormData< Operation: GraphQLOperation > ( for operation: Operation ,
65+ public func requestMultipartFormData< Operation: GraphQLOperation > ( for operation: Operation ,
4266 files: [ GraphQLFile ] ,
4367 sendOperationIdentifiers: Bool ,
4468 serializationFormat: JSONSerializationFormat . Type ,
@@ -50,7 +74,7 @@ public struct RequestCreator {
5074 } else {
5175 formData = MultipartFormData ( )
5276 }
53-
77+
5478 // Make sure all fields for files are set to null, or the server won't look
5579 // for the files in the rest of the form data
5680 let fieldsForFiles = Set ( files. map { $0. fieldName } )
@@ -67,10 +91,10 @@ public struct RequestCreator {
6791 }
6892 }
6993 fields [ " variables " ] = variables
70-
94+
7195 let operationData = try serializationFormat. serialize ( value: fields)
7296 formData. appendPart ( data: operationData, name: " operations " )
73-
97+
7498 var map = [ String: [ String] ] ( )
7599 if files. count == 1 {
76100 let firstFile = files. first!
@@ -80,10 +104,10 @@ public struct RequestCreator {
80104 map [ " \( index) " ] = [ " variables. \( file. fieldName) . \( index) " ]
81105 }
82106 }
83-
107+
84108 let mapData = try serializationFormat. serialize ( value: map)
85109 formData. appendPart ( data: mapData, name: " map " )
86-
110+
87111 for (index, file) in files. enumerated ( ) {
88112 formData. appendPart ( inputStream: file. inputStream,
89113 contentLength: file. contentLength,
@@ -95,3 +119,9 @@ public struct RequestCreator {
95119 return formData
96120 }
97121}
122+
123+ // Helper struct to create requests independently of HTTP operations.
124+ public struct ApolloRequestCreator : RequestCreator {
125+ // Internal init methods cannot be used in public methods
126+ public init ( ) { }
127+ }
0 commit comments