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

Bugfix/querysyntaxserializer #2646

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
44 changes: 43 additions & 1 deletion src/Core/Language.Tests/Visitors/QuerySyntaxSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System.IO;
using System.Text;
using ChilliCream.Testing;
Expand Down Expand Up @@ -202,5 +202,47 @@ public void Serialize_FargmentWithVariableDefs_InOutShouldBeTheSame()
query,
content.ToString());
}

[Fact]
public void Serialize_WithNameAndVariable()
{
// arrange
var documentString = "query name($id: String!) { a }";
DocumentNode document = Utf8GraphQLParser.Parse(documentString);

// act
var serialized = QuerySyntaxSerializer.Serialize(document, false);

// assert
Assert.Equal(documentString, serialized);
}

[Fact]
public void Serialize_ShortHand()
{
// arrange
var documentString = "{ a }";
DocumentNode document = Utf8GraphQLParser.Parse(documentString);

// act
var serialized = QuerySyntaxSerializer.Serialize(document, false);

// assert
Assert.Equal(documentString, serialized);
}

[Fact]
public void Serialize_WithoutNameAndWithVariable()
{
// arrange
var documentString = "query ($id: String!) { a }";
DocumentNode document = Utf8GraphQLParser.Parse(documentString);

// act
var serialized = QuerySyntaxSerializer.Serialize(document, false);

// assert
Assert.Equal(documentString, serialized);
}
}
}
10 changes: 7 additions & 3 deletions src/Core/Language/Visitors/QuerySyntaxSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -102,12 +102,16 @@ protected override void VisitOperationDefinition(
OperationDefinitionNode node,
DocumentWriter writer)
{
if (node.Name != null)
if (node.Name != null || node.VariableDefinitions.Count > 0)
{
writer.Write(node.Operation.ToString().ToLowerInvariant());
writer.WriteSpace();

writer.WriteName(node.Name);
if (node.Name != null)
{
writer.WriteName(node.Name);
}

if (node.VariableDefinitions.Any())
{
writer.Write('(');
Expand Down