Skip to content
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

Do not emit "query": "" for Strawberry Shake persisted queries #7062

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
26 changes: 17 additions & 9 deletions src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@

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 @@ -39,8 +33,22 @@
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);

Check warning on line 40 in src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs

View check run for this annotation

Codecov / codecov/patch

src/StrawberryShake/Client/src/Transport.Http/HttpConnection.cs#L38-L40

Added lines #L38 - L40 were not covered by tests
}
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"
}
}
}
}
Loading