diff --git a/src/StrawberryShake/Client/src/Core/RequestStrategy.cs b/src/StrawberryShake/Client/src/Core/RequestStrategy.cs index 9902fee7098..14df32c10d2 100644 --- a/src/StrawberryShake/Client/src/Core/RequestStrategy.cs +++ b/src/StrawberryShake/Client/src/Core/RequestStrategy.cs @@ -14,10 +14,4 @@ public enum RequestStrategy /// An id is send representing the query that is stored on the server. /// PersistedQuery, - - /// - /// The full GraphQL query is only send if the server has not yet stored the - /// persisted query. - /// - AutomaticPersistedQuery, } diff --git a/src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs b/src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs index e63a6bac7a7..9b962c0885a 100644 --- a/src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs +++ b/src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs @@ -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; @@ -25,13 +23,7 @@ public IAsyncEnumerable> 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, }; @@ -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, }; } diff --git a/src/StrawberryShake/Client/test/Core.Tests/Json/JsonOperationRequestSerializerTests.cs b/src/StrawberryShake/Client/test/Core.Tests/Json/JsonOperationRequestSerializerTests.cs index 0e37e3439ba..86acb9f05c8 100644 --- a/src/StrawberryShake/Client/test/Core.Tests/Json/JsonOperationRequestSerializerTests.cs +++ b/src/StrawberryShake/Client/test/Core.Tests/Json/JsonOperationRequestSerializerTests.cs @@ -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 { { "abc", json.RootElement }, }, + strategy: RequestStrategy.PersistedQuery), + jsonWriter); + jsonWriter.Flush(); + + // assert + Encoding.UTF8.GetString(stream.ToArray()).MatchSnapshot(); + } + [Fact] public void Serialize_Request_With_Extensions() { @@ -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 Body => Array.Empty(); + + public DocumentHash Hash { get; } = new("MD5", "ABCDEF"); + } } diff --git a/src/StrawberryShake/Client/test/Core.Tests/Json/__snapshots__/JsonOperationRequestSerializerTests.Serialize_Request_With_Id_And_Empty_Query.snap b/src/StrawberryShake/Client/test/Core.Tests/Json/__snapshots__/JsonOperationRequestSerializerTests.Serialize_Request_With_Id_And_Empty_Query.snap new file mode 100644 index 00000000000..31a8646dbd3 --- /dev/null +++ b/src/StrawberryShake/Client/test/Core.Tests/Json/__snapshots__/JsonOperationRequestSerializerTests.Serialize_Request_With_Id_And_Empty_Query.snap @@ -0,0 +1,11 @@ +{ + "id": "123", + "operationName": "abc", + "variables": { + "abc": { + "abc": { + "def": "def" + } + } + } +} \ No newline at end of file