From a48495d3fa63cccc7bc2e3f7deede5eae9a74dfc Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Tue, 16 Jan 2024 00:54:30 +0200 Subject: [PATCH] Improve some formatting, fix the bug that directives didn't get print --- .../Utilities/SyntaxSerializer.QuerySyntax.cs | 18 +++--- .../Utf8GraphQLParser.Operations.cs | 46 +++++--------- .../Utilities/QuerySyntaxPrinterTests.cs | 60 +++++++++++++------ 3 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxSerializer.QuerySyntax.cs b/src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxSerializer.QuerySyntax.cs index 2152a7cb5b3..1b950258ce9 100644 --- a/src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxSerializer.QuerySyntax.cs +++ b/src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxSerializer.QuerySyntax.cs @@ -30,7 +30,7 @@ private void VisitOperationDefinition( writer.WriteMany(node.VariableDefinitions, VisitVariableDefinition); writer.Write(')'); } - + WriteDirectives(node.Directives, writer); if (writeOperation) @@ -53,6 +53,8 @@ private void VisitVariableDefinition(VariableDefinitionNode node, ISyntaxWriter writer.Write(" = "); writer.WriteValue(node.DefaultValue); } + + WriteDirectives(node.Directives, writer); } private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter writer) @@ -82,11 +84,8 @@ private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter WriteDirectives(node.Directives, writer); - if (node.SelectionSet is not null) - { - writer.WriteSpace(); - VisitSelectionSet(node.SelectionSet, writer); - } + writer.WriteSpace(); + VisitSelectionSet(node.SelectionSet, writer); } private void VisitSelectionSet(SelectionSetNode node, ISyntaxWriter writer) @@ -230,10 +229,7 @@ private void VisitInlineFragment(InlineFragmentNode node, ISyntaxWriter writer) WriteDirectives(node.Directives, writer); - if (node.SelectionSet is { }) - { - writer.WriteSpace(); - VisitSelectionSet(node.SelectionSet, writer); - } + writer.WriteSpace(); + VisitSelectionSet(node.SelectionSet, writer); } } diff --git a/src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLParser.Operations.cs b/src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLParser.Operations.cs index cce5a6df9cc..9b39086ef45 100644 --- a/src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLParser.Operations.cs +++ b/src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLParser.Operations.cs @@ -27,15 +27,13 @@ private OperationDefinitionNode ParseOperationDefinition() var selectionSet = ParseSelectionSet(); var location = CreateLocation(in start); - return new OperationDefinitionNode - ( + return new OperationDefinitionNode( location, name, operation, variableDefinitions, directives, - selectionSet - ); + selectionSet); } /// @@ -49,15 +47,13 @@ private OperationDefinitionNode ParseShortOperationDefinition() var selectionSet = ParseSelectionSet(); var location = CreateLocation(in start); - return new OperationDefinitionNode - ( + return new OperationDefinitionNode( location, - null, + name: null, OperationType.Query, Array.Empty(), Array.Empty(), - selectionSet - ); + selectionSet); } /// @@ -133,18 +129,16 @@ private VariableDefinitionNode ParseVariableDefinition() ? ParseValueLiteral(true) : null; var directives = - ParseDirectives(true); + ParseDirectives(isConstant: true); var location = CreateLocation(in start); - return new VariableDefinitionNode - ( + return new VariableDefinitionNode( location, variable, type, defaultValue, - directives - ); + directives); } /// @@ -159,11 +153,9 @@ private VariableNode ParseVariable() var name = ParseName(); var location = CreateLocation(in start); - return new VariableNode - ( + return new VariableNode( location, - name - ); + name); } /// @@ -200,11 +192,9 @@ private SelectionSetNode ParseSelectionSet() var location = CreateLocation(in start); - return new SelectionSetNode - ( + return new SelectionSetNode( location, - selections - ); + selections); } /// @@ -259,16 +249,14 @@ private FieldNode ParseField() var location = CreateLocation(in start); - return new FieldNode - ( + return new FieldNode( location, name, alias, required, directives, arguments, - selectionSet - ); + selectionSet); } private INullabilityNode? ParseRequiredStatus() @@ -357,11 +345,9 @@ private ArgumentNode ParseArgument(bool isConstant) var location = CreateLocation(in start); - return new ArgumentNode - ( + return new ArgumentNode( location, name, - value - ); + value); } } diff --git a/src/HotChocolate/Language/test/Language.SyntaxTree.Tests/Utilities/QuerySyntaxPrinterTests.cs b/src/HotChocolate/Language/test/Language.SyntaxTree.Tests/Utilities/QuerySyntaxPrinterTests.cs index c3948c146c9..b52408b2bf7 100644 --- a/src/HotChocolate/Language/test/Language.SyntaxTree.Tests/Utilities/QuerySyntaxPrinterTests.cs +++ b/src/HotChocolate/Language/test/Language.SyntaxTree.Tests/Utilities/QuerySyntaxPrinterTests.cs @@ -10,9 +10,11 @@ public class QuerySyntaxPrinterTests public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame() { // arrange - var query = "{ foo(s: \"String\") { bar @foo " + - "{ baz @foo @bar } } }"; - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + const string query = + """ + { foo(s: "String") { bar @foo { baz @foo @bar } } } + """; + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(false); @@ -25,9 +27,11 @@ public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame() public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted() { // arrange - var query = "{ foo(s: \"String\") { bar @foo " + - "{ baz @foo @bar } } }"; - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + const string query = + """ + { foo(s: "String") { bar @foo { baz @foo @bar } } } + """; + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(false); @@ -40,8 +44,8 @@ public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted() public void Serialize_ShortHandQueryWithIndentation_LineBetweenFields() { // arrange - var query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }"; - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + const string query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }"; + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(); @@ -55,7 +59,7 @@ public void Serialize_KitchenSinkWithIndentation_OutputIsFormatted() { // arrange var query = FileResource.Open("kitchen-sink.graphql"); - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(); @@ -69,7 +73,7 @@ public void Serialize_KitchenSinkWithoutIndentation_OutputIsOneLine() { // arrange var query = FileResource.Open("kitchen-sink.graphql"); - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(); @@ -83,14 +87,13 @@ public void Serialize_KitchenSinkWithIndentation_CanBeParsed() { // arrange var query = FileResource.Open("kitchen-sink.graphql"); - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + var queryDocument = Utf8GraphQLParser.Parse(query); // act var result = queryDocument.ToString(); // assert result.MatchSnapshot(); - } [Fact] @@ -98,13 +101,13 @@ public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed() { // arrange var query = FileResource.Open("kitchen-sink.graphql"); - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + var queryDocument = Utf8GraphQLParser.Parse(query); // act var serializedQuery = queryDocument.ToString(); // assert - DocumentNode parsedQuery = Utf8GraphQLParser.Parse(serializedQuery); + var parsedQuery = Utf8GraphQLParser.Parse(serializedQuery); Assert.Equal(serializedQuery, parsedQuery.ToString()); } @@ -112,10 +115,11 @@ public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed() public void Serialize_QueryWithVarDeclaration_InOutShouldBeTheSame() { // arrange - var query = - "query Foo($bar: [String!]!) { foo(s: \"String\") " + - "{ bar @foo { baz @foo @bar } } }"; - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); + const string query = + """ + query Foo($bar: [String!]!) { foo(s: "String") { bar @foo { baz @foo @bar } } } + """; + var queryDocument = Utf8GraphQLParser.Parse(query); // act var serializedQuery = queryDocument.ToString(false); @@ -129,7 +133,7 @@ public void Serialize_FragmentWithVariableDefs_InOutShouldBeTheSame() { // arrange var query = "fragment Foo ($bar: [String!]!) on Bar { baz }"; - DocumentNode queryDocument = Utf8GraphQLParser.Parse(query, + var queryDocument = Utf8GraphQLParser.Parse(query, new ParserOptions(allowFragmentVariables: true)); // act @@ -138,4 +142,22 @@ public void Serialize_FragmentWithVariableDefs_InOutShouldBeTheSame() // assert Assert.Equal(query, result); } + + // https://github.com/ChilliCream/graphql-platform/issues/1997 + [Fact] + public void Serialize_QueryWithDirectivesOnVariables_InOutShouldBeTheSame() + { + // arrange + const string query = + """ + query Foo($variable: String @foo) { foo(a: $variable) } + """; + var queryDocument = Utf8GraphQLParser.Parse(query); + + // act + var result = queryDocument.ToString(false); + + // assert + Assert.Equal(query, result); + } }