-
-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds GraphQL SSE support to StrawberryShake (#6401)
- Loading branch information
1 parent
aba1904
commit 1977700
Showing
14 changed files
with
1,056 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/StrawberryShake/Client/src/Resources/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 179 additions & 33 deletions
212
src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,220 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Text; | ||
using System.Text.Json; | ||
using StrawberryShake.Internal; | ||
using HotChocolate.Transport.Http; | ||
using HotChocolate.Utilities; | ||
using StrawberryShake.Json; | ||
using static StrawberryShake.Properties.Resources; | ||
using static StrawberryShake.Transport.Http.ResponseEnumerable; | ||
|
||
namespace StrawberryShake.Transport.Http; | ||
|
||
public sealed class HttpConnection : IHttpConnection | ||
{ | ||
private readonly Func<HttpClient> _createClient; | ||
private readonly JsonOperationRequestSerializer _serializer = new(); | ||
|
||
public HttpConnection(Func<HttpClient> createClient) | ||
{ | ||
_createClient = createClient ?? throw new ArgumentNullException(nameof(createClient)); | ||
} | ||
|
||
public IAsyncEnumerable<Response<JsonDocument>> ExecuteAsync(OperationRequest request) | ||
=> Create(_createClient, () => CreateRequestMessage(request)); | ||
=> Create(_createClient, () => MapRequest(request)); | ||
|
||
private HttpRequestMessage CreateRequestMessage(OperationRequest request) | ||
private static GraphQLHttpRequest MapRequest(OperationRequest request) | ||
{ | ||
var operation = CreateRequestMessageBody(request); | ||
var (id, name, document, variables, extensions, _, files, _) = request; | ||
|
||
var content = request.Files.Count == 0 | ||
? CreateRequestContent(operation) | ||
: CreateMultipartContent(request, operation); | ||
#if NETSTANDARD2_0 | ||
var body = Encoding.UTF8.GetString(document.Body.ToArray()); | ||
#else | ||
var body = Encoding.UTF8.GetString(document.Body); | ||
#endif | ||
|
||
return new HttpRequestMessage { Method = HttpMethod.Post, Content = content }; | ||
var hasFiles = files is { Count: > 0 }; | ||
|
||
variables = MapVariables(variables); | ||
if (hasFiles && variables is not null) | ||
{ | ||
variables = MapFilesToVariables(variables, files!); | ||
} | ||
|
||
var operation = | ||
new HotChocolate.Transport.OperationRequest(body, id, name, variables, extensions); | ||
|
||
return new GraphQLHttpRequest(operation) { EnableFileUploads = hasFiles }; | ||
} | ||
|
||
private byte[] CreateRequestMessageBody(OperationRequest request) | ||
/// <summary> | ||
/// Converts the variables into a dictionary that can be serialized. This is necessary | ||
/// because the variables can contain lists of key value pairs which are not supported | ||
/// by HotChocolate.Transport.Http | ||
/// </summary> | ||
/// <remarks> | ||
/// We only convert the variables if necessary to avoid unnecessary allocations. | ||
/// </remarks> | ||
private static IReadOnlyDictionary<string, object?>? MapVariables( | ||
IReadOnlyDictionary<string, object?> variables) | ||
{ | ||
using var arrayWriter = new ArrayWriter(); | ||
_serializer.Serialize(request, arrayWriter); | ||
var buffer = new byte[arrayWriter.Length]; | ||
arrayWriter.Body.Span.CopyTo(buffer); | ||
return buffer; | ||
if (variables.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
Dictionary<string, object?>? copy = null; | ||
foreach (var variable in variables) | ||
{ | ||
var value = variable.Value; | ||
// the value can be a List<T> of key value pairs and not only a dictionary. We do expect | ||
// to just have lists here, but in case we have a dictionary this should also just work. | ||
if (value is IEnumerable<KeyValuePair<string, object?>> items) | ||
{ | ||
copy ??= CreateDictionary(variables); | ||
|
||
value = MapVariables(CreateDictionary(items)); | ||
} | ||
else if (value is List<object?> list) | ||
{ | ||
// the lists are mutable so we can just update the value in the list | ||
MapVariables(list); | ||
} | ||
|
||
if (copy is not null) | ||
{ | ||
copy[variable.Key] = value; | ||
} | ||
} | ||
|
||
return copy ?? variables; | ||
} | ||
|
||
private static HttpContent CreateRequestContent(byte[] operation) | ||
private static void MapVariables(List<object?> variables) | ||
{ | ||
var content = new ByteArrayContent(operation); | ||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
return content; | ||
if (variables.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
for (var index = 0; index < variables.Count; index++) | ||
{ | ||
switch (variables[index]) | ||
{ | ||
case IEnumerable<KeyValuePair<string, object?>> items: | ||
variables[index] = MapVariables(CreateDictionary(items)); | ||
break; | ||
|
||
case List<object?> list: | ||
MapVariables(list); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private static HttpContent CreateMultipartContent(OperationRequest request, byte[] operation) | ||
private static Dictionary<string, object?> CreateDictionary( | ||
IEnumerable<KeyValuePair<string, object?>> values) | ||
{ | ||
var fileMap = new Dictionary<string, string[]>(); | ||
var form = new MultipartFormDataContent | ||
#if NETSTANDARD2_0 | ||
var dictionary = new Dictionary<string, object?>(); | ||
|
||
foreach (var value in values) | ||
{ | ||
{ new ByteArrayContent(operation), "operations" }, | ||
{ JsonContent.Create(fileMap), "map" } | ||
}; | ||
dictionary[value.Key] = value.Value; | ||
} | ||
|
||
foreach (var file in request.Files) | ||
return dictionary; | ||
#else | ||
return new Dictionary<string, object?>(values); | ||
#endif | ||
} | ||
|
||
private static IReadOnlyDictionary<string, object?> MapFilesToVariables( | ||
IReadOnlyDictionary<string, object?> variables, | ||
IReadOnlyDictionary<string, Upload?> files) | ||
{ | ||
foreach (var file in files) | ||
{ | ||
if (file.Value is { } fileContent) | ||
var path = file.Key; | ||
var upload = file.Value; | ||
|
||
if (!upload.HasValue) | ||
{ | ||
continue; | ||
} | ||
|
||
var currentPath = path.Substring("variables.".Length); | ||
object? currentObject = variables; | ||
int index; | ||
while ((index = currentPath.IndexOf('.')) >= 0) | ||
{ | ||
var identifier = (fileMap.Count + 1).ToString(); | ||
fileMap.Add(identifier, new[] { file.Key }); | ||
form.Add(new StreamContent(fileContent.Content), identifier, fileContent.FileName); | ||
var segment = currentPath.Substring(0, index); | ||
switch (currentObject) | ||
{ | ||
case Dictionary<string, object> dictionary: | ||
if (!dictionary.TryGetValue(segment, out currentObject)) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
break; | ||
|
||
case List<object> array: | ||
if (!int.TryParse(segment, out var arrayIndex)) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
if (arrayIndex >= array.Count) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
currentObject = array[arrayIndex]; | ||
break; | ||
|
||
default: | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
currentPath = currentPath.Substring(index + 1); | ||
} | ||
|
||
switch (currentObject) | ||
{ | ||
case Dictionary<string, object> result: | ||
result[currentPath] = | ||
new FileReference(upload.Value.Content, upload.Value.FileName); | ||
break; | ||
|
||
case List<object> array: | ||
if (!int.TryParse(currentPath, out var arrayIndex)) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
if (arrayIndex >= array.Count) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
|
||
array[arrayIndex] = | ||
new FileReference(upload.Value.Content, upload.Value.FileName); | ||
|
||
break; | ||
|
||
default: | ||
throw new InvalidOperationException( | ||
string.Format(HttpConnection_FileMapDoesNotMatch, path)); | ||
} | ||
} | ||
|
||
return form; | ||
return variables; | ||
} | ||
} |
Oops, something went wrong.