Skip to content

Commit

Permalink
Ported Do not emit "query": "" for Strawberry Shake persisted queries
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Sep 4, 2024
1 parent 28f39cf commit a190f03
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
6 changes: 0 additions & 6 deletions src/StrawberryShake/Client/src/Core/RequestStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@ public enum RequestStrategy
/// An id is send representing the query that is stored on the server.
/// </summary>
PersistedQuery,

/// <summary>
/// The full GraphQL query is only send if the server has not yet stored the
/// persisted query.
/// </summary>
AutomaticPersistedQuery,
}
28 changes: 17 additions & 11 deletions src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Text;
using System.Text.Json;
using HotChocolate.Transport.Http;
using HotChocolate.Utilities;
using StrawberryShake.Json;
using static StrawberryShake.Properties.Resources;
using static StrawberryShake.Transport.Http.ResponseEnumerable;

Expand All @@ -25,13 +23,7 @@ public IAsyncEnumerable<Response<JsonDocument>> ExecuteAsync(OperationRequest re

private static GraphQLHttpRequest MapRequest(OperationRequest request)
{
var (id, name, document, variables, extensions, _, files, _) = request;

#if NETSTANDARD2_0
var body = Encoding.UTF8.GetString(document.Body.ToArray());
#else
var body = Encoding.UTF8.GetString(document.Body);
#endif
var (id, name, document, variables, extensions, _, files, strategy) = request;

var hasFiles = files is { Count: > 0, };

Expand All @@ -41,8 +33,22 @@ private static GraphQLHttpRequest MapRequest(OperationRequest request)
variables = MapFilesToVariables(variables, files!);
}

var operation =
new HotChocolate.Transport.OperationRequest(body, id, name, variables, extensions);
HotChocolate.Transport.OperationRequest operation;

if (strategy == RequestStrategy.PersistedQuery)
{
operation = new HotChocolate.Transport.OperationRequest(null, id, name, variables, extensions);
}
else
{
#if NETSTANDARD2_0
var body = Encoding.UTF8.GetString(document.Body.ToArray());
#else
var body = Encoding.UTF8.GetString(document.Body);
#endif

operation = new HotChocolate.Transport.OperationRequest(body, null, name, variables, extensions);
}

return new GraphQLHttpRequest(operation) { EnableFileUploads = hasFiles, };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ public void Serialize_Request_With_Json()
Encoding.UTF8.GetString(stream.ToArray()).MatchSnapshot();
}

[Fact]
public void Serialize_Request_With_Id_And_Empty_Query()
{
// arrange
var json = JsonDocument.Parse(@"{ ""abc"": { ""def"": ""def"" } }");

// act
using var stream = new MemoryStream();
using var jsonWriter = new Utf8JsonWriter(stream, new() { Indented = true, });
var serializer = new JsonOperationRequestSerializer();
serializer.Serialize(
new OperationRequest(
"123",
"abc",
new EmptyDocument(),
new Dictionary<string, object?> { { "abc", json.RootElement }, },
strategy: RequestStrategy.PersistedQuery),
jsonWriter);
jsonWriter.Flush();

// assert
Encoding.UTF8.GetString(stream.ToArray()).MatchSnapshot();
}

[Fact]
public void Serialize_Request_With_Extensions()
{
Expand Down Expand Up @@ -123,4 +147,13 @@ private sealed class Document : IDocument

public DocumentHash Hash { get; } = new("MD5", "ABCDEF");
}

private sealed class EmptyDocument : IDocument
{
public OperationKind Kind => OperationKind.Query;

public ReadOnlySpan<byte> Body => Array.Empty<byte>();

public DocumentHash Hash { get; } = new("MD5", "ABCDEF");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "123",
"operationName": "abc",
"variables": {
"abc": {
"abc": {
"def": "def"
}
}
}
}

0 comments on commit a190f03

Please sign in to comment.