diff --git a/src/Core/Language.Tests/Visitors/QuerySyntaxSerializerTests.cs b/src/Core/Language.Tests/Visitors/QuerySyntaxSerializerTests.cs index 284ed2dc072..7849e7a5471 100644 --- a/src/Core/Language.Tests/Visitors/QuerySyntaxSerializerTests.cs +++ b/src/Core/Language.Tests/Visitors/QuerySyntaxSerializerTests.cs @@ -1,4 +1,4 @@ - + using System.IO; using System.Text; using ChilliCream.Testing; @@ -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); + } } } diff --git a/src/Core/Language/Visitors/QuerySyntaxSerializer.cs b/src/Core/Language/Visitors/QuerySyntaxSerializer.cs index 23754a35b20..42a4ec70448 100644 --- a/src/Core/Language/Visitors/QuerySyntaxSerializer.cs +++ b/src/Core/Language/Visitors/QuerySyntaxSerializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Text; @@ -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('(');