Skip to content

Commit 5515682

Browse files
PascalSennmichaelstaib
authored andcommitted
Adds GraphQL-Preflight in StrawberryShake automatically (#6807)
1 parent f2868f9 commit 5515682

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

src/HotChocolate/AspNetCore/src/Transport.Http/DefaultGraphQLHttpClient.cs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ private async Task<GraphQLHttpResponse> ExecuteInternalAsync(
107107
requestMessage.Version = _http.DefaultRequestVersion;
108108
requestMessage.VersionPolicy = _http.DefaultVersionPolicy;
109109
#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);
111113
return new GraphQLHttpResponse(responseMessage);
112114
}
113115

@@ -134,9 +136,16 @@ private static HttpRequestMessage CreateRequestMessage(
134136

135137
if (method == GraphQLHttpMethod.Post)
136138
{
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+
140149
message.RequestUri = requestUri;
141150
}
142151
else if (method == GraphQLHttpMethod.Get)
@@ -153,7 +162,9 @@ private static HttpRequestMessage CreateRequestMessage(
153162
return message;
154163
}
155164

156-
private static HttpContent CreatePostContent(ArrayWriter arrayWriter, GraphQLHttpRequest request)
165+
private static HttpContent CreatePostContent(
166+
ArrayWriter arrayWriter,
167+
GraphQLHttpRequest request)
157168
{
158169
using var jsonWriter = new Utf8JsonWriter(arrayWriter, JsonOptionDefaults.WriterOptions);
159170
request.Operation.WriteTo(jsonWriter);
@@ -163,49 +174,54 @@ private static HttpContent CreatePostContent(ArrayWriter arrayWriter, GraphQLHtt
163174
#if NET7_0_OR_GREATER
164175
content.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json, "utf-8");
165176
#else
166-
content.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
177+
content.Headers.ContentType =
178+
new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
167179
#endif
168180
return content;
169181
}
170182

171-
private static HttpContent CreateMultipartContent(ArrayWriter arrayWriter, GraphQLHttpRequest request)
183+
private static HttpContent CreateMultipartContent(
184+
ArrayWriter arrayWriter,
185+
GraphQLHttpRequest request)
172186
{
173187
var fileInfos = WriteFileMapJson(arrayWriter, request);
174188

175189
if (fileInfos.Count == 0)
176190
{
177191
arrayWriter.Reset();
178-
return CreatePostContent(arrayWriter, request);
192+
return CreatePostContent(arrayWriter, request);
179193
}
180-
194+
181195
var start = arrayWriter.Length;
182196
WriteOperationJson(arrayWriter, request);
183197
var buffer = arrayWriter.GetInternalBuffer();
184198

185199
var form = new MultipartFormDataContent();
186-
200+
187201
var operation = new ByteArrayContent(buffer, start, arrayWriter.Length - start);
188202
#if NET7_0_OR_GREATER
189203
operation.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json, "utf-8");
190204
#else
191-
operation.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
205+
operation.Headers.ContentType =
206+
new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
192207
#endif
193208
form.Add(operation, "operations");
194-
209+
195210
var fileMap = new ByteArrayContent(buffer, 0, start);
196211
#if NET7_0_OR_GREATER
197212
fileMap.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json, "utf-8");
198213
#else
199-
fileMap.Headers.ContentType = new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
214+
fileMap.Headers.ContentType =
215+
new MediaTypeHeaderValue(ContentType.Json) { CharSet = "utf-8" };
200216
#endif
201217
form.Add(fileMap, "map");
202-
218+
203219
foreach (var fileInfo in fileInfos)
204220
{
205221
var file = new StreamContent(fileInfo.File.OpenRead());
206222
form.Add(file, fileInfo.Name, fileInfo.File.FileName);
207223
}
208-
224+
209225
return form;
210226
}
211227

@@ -215,14 +231,18 @@ private static void WriteOperationJson(ArrayWriter arrayWriter, GraphQLHttpReque
215231
request.Operation.WriteTo(jsonWriter);
216232
}
217233

218-
private static IReadOnlyList<FileReferenceInfo> WriteFileMapJson(ArrayWriter arrayWriter, GraphQLHttpRequest request)
234+
private static IReadOnlyList<FileReferenceInfo> WriteFileMapJson(
235+
ArrayWriter arrayWriter,
236+
GraphQLHttpRequest request)
219237
{
220238
using var jsonWriter = new Utf8JsonWriter(arrayWriter, JsonOptionDefaults.WriterOptions);
221239
return Utf8JsonWriterHelper.WriteFilesMap(jsonWriter, request.Operation);
222240
}
223241

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)
226246
{
227247
var sb = new StringBuilder();
228248
var appendAmpersand = false;
@@ -255,7 +275,8 @@ private static Uri CreateGetRequestUri(ArrayWriter arrayWriter, Uri baseAddress,
255275
{
256276
AppendAmpersand(sb, ref appendAmpersand);
257277
sb.Append("variables=");
258-
sb.Append(Uri.EscapeDataString(FormatDocumentAsJson(arrayWriter, request.VariablesNode)));
278+
sb.Append(
279+
Uri.EscapeDataString(FormatDocumentAsJson(arrayWriter, request.VariablesNode)));
259280
}
260281
else if (request.Variables is not null)
261282
{
@@ -268,7 +289,8 @@ private static Uri CreateGetRequestUri(ArrayWriter arrayWriter, Uri baseAddress,
268289
{
269290
AppendAmpersand(sb, ref appendAmpersand);
270291
sb.Append("extensions=");
271-
sb.Append(Uri.EscapeDataString(FormatDocumentAsJson(arrayWriter, request.ExtensionsNode)));
292+
sb.Append(
293+
Uri.EscapeDataString(FormatDocumentAsJson(arrayWriter, request.ExtensionsNode)));
272294
}
273295
else if (request.Extensions is not null)
274296
{
@@ -310,4 +332,4 @@ protected override void Dispose(bool disposing)
310332
_http.Dispose();
311333
}
312334
}
313-
}
335+
}

src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,7 @@ public async Task Post_GraphQL_FileUpload()
685685
var request = new GraphQLHttpRequest(operation, requestUri)
686686
{
687687
Method = GraphQLHttpMethod.Post,
688-
EnableFileUploads = true,
689-
OnMessageCreated = (_, m) => m.Headers.AddGraphQLPreflight()
688+
EnableFileUploads = true
690689
};
691690

692691
// act
@@ -731,8 +730,7 @@ public async Task Post_GraphQL_FileUpload_With_ObjectValueNode()
731730
var request = new GraphQLHttpRequest(operation, requestUri)
732731
{
733732
Method = GraphQLHttpMethod.Post,
734-
EnableFileUploads = true,
735-
OnMessageCreated = (_, m) => m.Headers.AddGraphQLPreflight()
733+
EnableFileUploads = true
736734
};
737735

738736
// act

0 commit comments

Comments
 (0)