-
-
Notifications
You must be signed in to change notification settings - Fork 753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds GraphQL SSE support to StrawberryShake #6401
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27fdada
Adds GraphQL SSE support to StrawberryShake
PascalSenn 80532e5
Remove unused file
PascalSenn d745fe5
Cleanup
PascalSenn 0bff1bf
Cleanup
PascalSenn 997f328
Merge branch 'main' into pse/add-graphql-sse-to-strawberry-shake
michaelstaib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the null values also in arrays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ... i know why i did that ... if null i wanted to short-circur
writer.WriteNullValue()
in the null check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uhh ... there are a couple of bugs in there .... I will need to do a separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn it michael ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the Utf8JsonWriter?