@@ -107,7 +107,9 @@ private async Task<GraphQLHttpResponse> ExecuteInternalAsync(
107
107
requestMessage . Version = _http . DefaultRequestVersion ;
108
108
requestMessage . VersionPolicy = _http . DefaultVersionPolicy ;
109
109
#endif
110
- var responseMessage = await _http . SendAsync ( requestMessage , ResponseHeadersRead , ct ) . ConfigureAwait ( false ) ;
110
+ var responseMessage = await _http
111
+ . SendAsync ( requestMessage , ResponseHeadersRead , ct )
112
+ . ConfigureAwait ( false ) ;
111
113
return new GraphQLHttpResponse ( responseMessage ) ;
112
114
}
113
115
@@ -134,9 +136,16 @@ private static HttpRequestMessage CreateRequestMessage(
134
136
135
137
if ( method == GraphQLHttpMethod . Post )
136
138
{
137
- message . Content = request . EnableFileUploads
138
- ? CreateMultipartContent ( arrayWriter , request )
139
- : CreatePostContent ( arrayWriter , request ) ;
139
+ if ( request . EnableFileUploads )
140
+ {
141
+ message . Content = CreateMultipartContent ( arrayWriter , request ) ;
142
+ message . Headers . AddGraphQLPreflight ( ) ;
143
+ }
144
+ else
145
+ {
146
+ message . Content = CreatePostContent ( arrayWriter , request ) ;
147
+ }
148
+
140
149
message . RequestUri = requestUri ;
141
150
}
142
151
else if ( method == GraphQLHttpMethod . Get )
@@ -153,7 +162,9 @@ private static HttpRequestMessage CreateRequestMessage(
153
162
return message ;
154
163
}
155
164
156
- private static HttpContent CreatePostContent ( ArrayWriter arrayWriter , GraphQLHttpRequest request )
165
+ private static HttpContent CreatePostContent (
166
+ ArrayWriter arrayWriter ,
167
+ GraphQLHttpRequest request )
157
168
{
158
169
using var jsonWriter = new Utf8JsonWriter ( arrayWriter , JsonOptionDefaults . WriterOptions ) ;
159
170
request . Operation . WriteTo ( jsonWriter ) ;
@@ -163,49 +174,54 @@ private static HttpContent CreatePostContent(ArrayWriter arrayWriter, GraphQLHtt
163
174
#if NET7_0_OR_GREATER
164
175
content . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json , "utf-8" ) ;
165
176
#else
166
- content . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
177
+ content . Headers . ContentType =
178
+ new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
167
179
#endif
168
180
return content ;
169
181
}
170
182
171
- private static HttpContent CreateMultipartContent ( ArrayWriter arrayWriter , GraphQLHttpRequest request )
183
+ private static HttpContent CreateMultipartContent (
184
+ ArrayWriter arrayWriter ,
185
+ GraphQLHttpRequest request )
172
186
{
173
187
var fileInfos = WriteFileMapJson ( arrayWriter , request ) ;
174
188
175
189
if ( fileInfos . Count == 0 )
176
190
{
177
191
arrayWriter . Reset ( ) ;
178
- return CreatePostContent ( arrayWriter , request ) ;
192
+ return CreatePostContent ( arrayWriter , request ) ;
179
193
}
180
-
194
+
181
195
var start = arrayWriter . Length ;
182
196
WriteOperationJson ( arrayWriter , request ) ;
183
197
var buffer = arrayWriter . GetInternalBuffer ( ) ;
184
198
185
199
var form = new MultipartFormDataContent ( ) ;
186
-
200
+
187
201
var operation = new ByteArrayContent ( buffer , start , arrayWriter . Length - start ) ;
188
202
#if NET7_0_OR_GREATER
189
203
operation . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json , "utf-8" ) ;
190
204
#else
191
- operation . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
205
+ operation . Headers . ContentType =
206
+ new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
192
207
#endif
193
208
form . Add ( operation , "operations" ) ;
194
-
209
+
195
210
var fileMap = new ByteArrayContent ( buffer , 0 , start ) ;
196
211
#if NET7_0_OR_GREATER
197
212
fileMap . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json , "utf-8" ) ;
198
213
#else
199
- fileMap . Headers . ContentType = new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
214
+ fileMap . Headers . ContentType =
215
+ new MediaTypeHeaderValue ( ContentType . Json ) { CharSet = "utf-8" } ;
200
216
#endif
201
217
form . Add ( fileMap , "map" ) ;
202
-
218
+
203
219
foreach ( var fileInfo in fileInfos )
204
220
{
205
221
var file = new StreamContent ( fileInfo . File . OpenRead ( ) ) ;
206
222
form . Add ( file , fileInfo . Name , fileInfo . File . FileName ) ;
207
223
}
208
-
224
+
209
225
return form ;
210
226
}
211
227
@@ -215,14 +231,18 @@ private static void WriteOperationJson(ArrayWriter arrayWriter, GraphQLHttpReque
215
231
request . Operation . WriteTo ( jsonWriter ) ;
216
232
}
217
233
218
- private static IReadOnlyList < FileReferenceInfo > WriteFileMapJson ( ArrayWriter arrayWriter , GraphQLHttpRequest request )
234
+ private static IReadOnlyList < FileReferenceInfo > WriteFileMapJson (
235
+ ArrayWriter arrayWriter ,
236
+ GraphQLHttpRequest request )
219
237
{
220
238
using var jsonWriter = new Utf8JsonWriter ( arrayWriter , JsonOptionDefaults . WriterOptions ) ;
221
239
return Utf8JsonWriterHelper . WriteFilesMap ( jsonWriter , request . Operation ) ;
222
240
}
223
241
224
-
225
- private static Uri CreateGetRequestUri ( ArrayWriter arrayWriter , Uri baseAddress , OperationRequest request )
242
+ private static Uri CreateGetRequestUri (
243
+ ArrayWriter arrayWriter ,
244
+ Uri baseAddress ,
245
+ OperationRequest request )
226
246
{
227
247
var sb = new StringBuilder ( ) ;
228
248
var appendAmpersand = false ;
@@ -255,7 +275,8 @@ private static Uri CreateGetRequestUri(ArrayWriter arrayWriter, Uri baseAddress,
255
275
{
256
276
AppendAmpersand ( sb , ref appendAmpersand ) ;
257
277
sb . Append ( "variables=" ) ;
258
- sb . Append ( Uri . EscapeDataString ( FormatDocumentAsJson ( arrayWriter , request . VariablesNode ) ) ) ;
278
+ sb . Append (
279
+ Uri . EscapeDataString ( FormatDocumentAsJson ( arrayWriter , request . VariablesNode ) ) ) ;
259
280
}
260
281
else if ( request . Variables is not null )
261
282
{
@@ -268,7 +289,8 @@ private static Uri CreateGetRequestUri(ArrayWriter arrayWriter, Uri baseAddress,
268
289
{
269
290
AppendAmpersand ( sb , ref appendAmpersand ) ;
270
291
sb . Append ( "extensions=" ) ;
271
- sb . Append ( Uri . EscapeDataString ( FormatDocumentAsJson ( arrayWriter , request . ExtensionsNode ) ) ) ;
292
+ sb . Append (
293
+ Uri . EscapeDataString ( FormatDocumentAsJson ( arrayWriter , request . ExtensionsNode ) ) ) ;
272
294
}
273
295
else if ( request . Extensions is not null )
274
296
{
@@ -310,4 +332,4 @@ protected override void Dispose(bool disposing)
310
332
_http . Dispose ( ) ;
311
333
}
312
334
}
313
- }
335
+ }
0 commit comments