Skip to content

Commit a190f03

Browse files
committed
Ported Do not emit "query": "" for Strawberry Shake persisted queries
1 parent 28f39cf commit a190f03

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

src/StrawberryShake/Client/src/Core/RequestStrategy.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,4 @@ public enum RequestStrategy
1414
/// An id is send representing the query that is stored on the server.
1515
/// </summary>
1616
PersistedQuery,
17-
18-
/// <summary>
19-
/// The full GraphQL query is only send if the server has not yet stored the
20-
/// persisted query.
21-
/// </summary>
22-
AutomaticPersistedQuery,
2317
}

src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Text;
55
using System.Text.Json;
66
using HotChocolate.Transport.Http;
7-
using HotChocolate.Utilities;
8-
using StrawberryShake.Json;
97
using static StrawberryShake.Properties.Resources;
108
using static StrawberryShake.Transport.Http.ResponseEnumerable;
119

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

2624
private static GraphQLHttpRequest MapRequest(OperationRequest request)
2725
{
28-
var (id, name, document, variables, extensions, _, files, _) = request;
29-
30-
#if NETSTANDARD2_0
31-
var body = Encoding.UTF8.GetString(document.Body.ToArray());
32-
#else
33-
var body = Encoding.UTF8.GetString(document.Body);
34-
#endif
26+
var (id, name, document, variables, extensions, _, files, strategy) = request;
3527

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

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

44-
var operation =
45-
new HotChocolate.Transport.OperationRequest(body, id, name, variables, extensions);
36+
HotChocolate.Transport.OperationRequest operation;
37+
38+
if (strategy == RequestStrategy.PersistedQuery)
39+
{
40+
operation = new HotChocolate.Transport.OperationRequest(null, id, name, variables, extensions);
41+
}
42+
else
43+
{
44+
#if NETSTANDARD2_0
45+
var body = Encoding.UTF8.GetString(document.Body.ToArray());
46+
#else
47+
var body = Encoding.UTF8.GetString(document.Body);
48+
#endif
49+
50+
operation = new HotChocolate.Transport.OperationRequest(body, null, name, variables, extensions);
51+
}
4652

4753
return new GraphQLHttpRequest(operation) { EnableFileUploads = hasFiles, };
4854
}

src/StrawberryShake/Client/test/Core.Tests/Json/JsonOperationRequestSerializerTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ public void Serialize_Request_With_Json()
6868
Encoding.UTF8.GetString(stream.ToArray()).MatchSnapshot();
6969
}
7070

71+
[Fact]
72+
public void Serialize_Request_With_Id_And_Empty_Query()
73+
{
74+
// arrange
75+
var json = JsonDocument.Parse(@"{ ""abc"": { ""def"": ""def"" } }");
76+
77+
// act
78+
using var stream = new MemoryStream();
79+
using var jsonWriter = new Utf8JsonWriter(stream, new() { Indented = true, });
80+
var serializer = new JsonOperationRequestSerializer();
81+
serializer.Serialize(
82+
new OperationRequest(
83+
"123",
84+
"abc",
85+
new EmptyDocument(),
86+
new Dictionary<string, object?> { { "abc", json.RootElement }, },
87+
strategy: RequestStrategy.PersistedQuery),
88+
jsonWriter);
89+
jsonWriter.Flush();
90+
91+
// assert
92+
Encoding.UTF8.GetString(stream.ToArray()).MatchSnapshot();
93+
}
94+
7195
[Fact]
7296
public void Serialize_Request_With_Extensions()
7397
{
@@ -123,4 +147,13 @@ private sealed class Document : IDocument
123147

124148
public DocumentHash Hash { get; } = new("MD5", "ABCDEF");
125149
}
150+
151+
private sealed class EmptyDocument : IDocument
152+
{
153+
public OperationKind Kind => OperationKind.Query;
154+
155+
public ReadOnlySpan<byte> Body => Array.Empty<byte>();
156+
157+
public DocumentHash Hash { get; } = new("MD5", "ABCDEF");
158+
}
126159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": "123",
3+
"operationName": "abc",
4+
"variables": {
5+
"abc": {
6+
"abc": {
7+
"def": "def"
8+
}
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)