Skip to content

Commit c3532bf

Browse files
authored
Improved some formatting, fix the bug that directives didn't get print (#6825)
1 parent 4a5343b commit c3532bf

File tree

3 files changed

+64
-60
lines changed

3 files changed

+64
-60
lines changed

src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxSerializer.QuerySyntax.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void VisitOperationDefinition(
3030
writer.WriteMany(node.VariableDefinitions, VisitVariableDefinition);
3131
writer.Write(')');
3232
}
33-
33+
3434
WriteDirectives(node.Directives, writer);
3535

3636
if (writeOperation)
@@ -53,6 +53,8 @@ private void VisitVariableDefinition(VariableDefinitionNode node, ISyntaxWriter
5353
writer.Write(" = ");
5454
writer.WriteValue(node.DefaultValue);
5555
}
56+
57+
WriteDirectives(node.Directives, writer);
5658
}
5759

5860
private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter writer)
@@ -82,11 +84,8 @@ private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter
8284

8385
WriteDirectives(node.Directives, writer);
8486

85-
if (node.SelectionSet is not null)
86-
{
87-
writer.WriteSpace();
88-
VisitSelectionSet(node.SelectionSet, writer);
89-
}
87+
writer.WriteSpace();
88+
VisitSelectionSet(node.SelectionSet, writer);
9089
}
9190

9291
private void VisitSelectionSet(SelectionSetNode node, ISyntaxWriter writer)
@@ -230,10 +229,7 @@ private void VisitInlineFragment(InlineFragmentNode node, ISyntaxWriter writer)
230229

231230
WriteDirectives(node.Directives, writer);
232231

233-
if (node.SelectionSet is { })
234-
{
235-
writer.WriteSpace();
236-
VisitSelectionSet(node.SelectionSet, writer);
237-
}
232+
writer.WriteSpace();
233+
VisitSelectionSet(node.SelectionSet, writer);
238234
}
239235
}

src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLParser.Operations.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ private OperationDefinitionNode ParseOperationDefinition()
2727
var selectionSet = ParseSelectionSet();
2828
var location = CreateLocation(in start);
2929

30-
return new OperationDefinitionNode
31-
(
30+
return new OperationDefinitionNode(
3231
location,
3332
name,
3433
operation,
3534
variableDefinitions,
3635
directives,
37-
selectionSet
38-
);
36+
selectionSet);
3937
}
4038

4139
/// <summary>
@@ -49,15 +47,13 @@ private OperationDefinitionNode ParseShortOperationDefinition()
4947
var selectionSet = ParseSelectionSet();
5048
var location = CreateLocation(in start);
5149

52-
return new OperationDefinitionNode
53-
(
50+
return new OperationDefinitionNode(
5451
location,
55-
null,
52+
name: null,
5653
OperationType.Query,
5754
Array.Empty<VariableDefinitionNode>(),
5855
Array.Empty<DirectiveNode>(),
59-
selectionSet
60-
);
56+
selectionSet);
6157
}
6258

6359
/// <summary>
@@ -133,18 +129,16 @@ private VariableDefinitionNode ParseVariableDefinition()
133129
? ParseValueLiteral(true)
134130
: null;
135131
var directives =
136-
ParseDirectives(true);
132+
ParseDirectives(isConstant: true);
137133

138134
var location = CreateLocation(in start);
139135

140-
return new VariableDefinitionNode
141-
(
136+
return new VariableDefinitionNode(
142137
location,
143138
variable,
144139
type,
145140
defaultValue,
146-
directives
147-
);
141+
directives);
148142
}
149143

150144
/// <summary>
@@ -159,11 +153,9 @@ private VariableNode ParseVariable()
159153
var name = ParseName();
160154
var location = CreateLocation(in start);
161155

162-
return new VariableNode
163-
(
156+
return new VariableNode(
164157
location,
165-
name
166-
);
158+
name);
167159
}
168160

169161
/// <summary>
@@ -200,11 +192,9 @@ private SelectionSetNode ParseSelectionSet()
200192

201193
var location = CreateLocation(in start);
202194

203-
return new SelectionSetNode
204-
(
195+
return new SelectionSetNode(
205196
location,
206-
selections
207-
);
197+
selections);
208198
}
209199

210200
/// <summary>
@@ -259,16 +249,14 @@ private FieldNode ParseField()
259249

260250
var location = CreateLocation(in start);
261251

262-
return new FieldNode
263-
(
252+
return new FieldNode(
264253
location,
265254
name,
266255
alias,
267256
required,
268257
directives,
269258
arguments,
270-
selectionSet
271-
);
259+
selectionSet);
272260
}
273261

274262
private INullabilityNode? ParseRequiredStatus()
@@ -357,11 +345,9 @@ private ArgumentNode ParseArgument(bool isConstant)
357345

358346
var location = CreateLocation(in start);
359347

360-
return new ArgumentNode
361-
(
348+
return new ArgumentNode(
362349
location,
363350
name,
364-
value
365-
);
351+
value);
366352
}
367353
}

src/HotChocolate/Language/test/Language.SyntaxTree.Tests/Utilities/QuerySyntaxPrinterTests.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ public class QuerySyntaxPrinterTests
1010
public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame()
1111
{
1212
// arrange
13-
var query = "{ foo(s: \"String\") { bar @foo " +
14-
"{ baz @foo @bar } } }";
15-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
13+
const string query =
14+
"""
15+
{ foo(s: "String") { bar @foo { baz @foo @bar } } }
16+
""";
17+
var queryDocument = Utf8GraphQLParser.Parse(query);
1618

1719
// act
1820
var result = queryDocument.ToString(false);
@@ -25,9 +27,11 @@ public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame()
2527
public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted()
2628
{
2729
// arrange
28-
var query = "{ foo(s: \"String\") { bar @foo " +
29-
"{ baz @foo @bar } } }";
30-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
30+
const string query =
31+
"""
32+
{ foo(s: "String") { bar @foo { baz @foo @bar } } }
33+
""";
34+
var queryDocument = Utf8GraphQLParser.Parse(query);
3135

3236
// act
3337
var result = queryDocument.ToString(false);
@@ -40,8 +44,8 @@ public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted()
4044
public void Serialize_ShortHandQueryWithIndentation_LineBetweenFields()
4145
{
4246
// arrange
43-
var query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }";
44-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
47+
const string query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }";
48+
var queryDocument = Utf8GraphQLParser.Parse(query);
4549

4650
// act
4751
var result = queryDocument.ToString();
@@ -55,7 +59,7 @@ public void Serialize_KitchenSinkWithIndentation_OutputIsFormatted()
5559
{
5660
// arrange
5761
var query = FileResource.Open("kitchen-sink.graphql");
58-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
62+
var queryDocument = Utf8GraphQLParser.Parse(query);
5963

6064
// act
6165
var result = queryDocument.ToString();
@@ -69,7 +73,7 @@ public void Serialize_KitchenSinkWithoutIndentation_OutputIsOneLine()
6973
{
7074
// arrange
7175
var query = FileResource.Open("kitchen-sink.graphql");
72-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
76+
var queryDocument = Utf8GraphQLParser.Parse(query);
7377

7478
// act
7579
var result = queryDocument.ToString();
@@ -83,39 +87,39 @@ public void Serialize_KitchenSinkWithIndentation_CanBeParsed()
8387
{
8488
// arrange
8589
var query = FileResource.Open("kitchen-sink.graphql");
86-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
90+
var queryDocument = Utf8GraphQLParser.Parse(query);
8791

8892
// act
8993
var result = queryDocument.ToString();
9094

9195
// assert
9296
result.MatchSnapshot();
93-
9497
}
9598

9699
[Fact]
97100
public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed()
98101
{
99102
// arrange
100103
var query = FileResource.Open("kitchen-sink.graphql");
101-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
104+
var queryDocument = Utf8GraphQLParser.Parse(query);
102105

103106
// act
104107
var serializedQuery = queryDocument.ToString();
105108

106109
// assert
107-
DocumentNode parsedQuery = Utf8GraphQLParser.Parse(serializedQuery);
110+
var parsedQuery = Utf8GraphQLParser.Parse(serializedQuery);
108111
Assert.Equal(serializedQuery, parsedQuery.ToString());
109112
}
110113

111114
[Fact]
112115
public void Serialize_QueryWithVarDeclaration_InOutShouldBeTheSame()
113116
{
114117
// arrange
115-
var query =
116-
"query Foo($bar: [String!]!) { foo(s: \"String\") " +
117-
"{ bar @foo { baz @foo @bar } } }";
118-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
118+
const string query =
119+
"""
120+
query Foo($bar: [String!]!) { foo(s: "String") { bar @foo { baz @foo @bar } } }
121+
""";
122+
var queryDocument = Utf8GraphQLParser.Parse(query);
119123

120124
// act
121125
var serializedQuery = queryDocument.ToString(false);
@@ -129,7 +133,7 @@ public void Serialize_FragmentWithVariableDefs_InOutShouldBeTheSame()
129133
{
130134
// arrange
131135
var query = "fragment Foo ($bar: [String!]!) on Bar { baz }";
132-
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query,
136+
var queryDocument = Utf8GraphQLParser.Parse(query,
133137
new ParserOptions(allowFragmentVariables: true));
134138

135139
// act
@@ -138,4 +142,22 @@ public void Serialize_FragmentWithVariableDefs_InOutShouldBeTheSame()
138142
// assert
139143
Assert.Equal(query, result);
140144
}
145+
146+
// https://github.com/ChilliCream/graphql-platform/issues/1997
147+
[Fact]
148+
public void Serialize_QueryWithDirectivesOnVariables_InOutShouldBeTheSame()
149+
{
150+
// arrange
151+
const string query =
152+
"""
153+
query Foo($variable: String @foo) { foo(a: $variable) }
154+
""";
155+
var queryDocument = Utf8GraphQLParser.Parse(query);
156+
157+
// act
158+
var result = queryDocument.ToString(false);
159+
160+
// assert
161+
Assert.Equal(query, result);
162+
}
141163
}

0 commit comments

Comments
 (0)