Skip to content

Commit 03ba732

Browse files
authored
Updated the FieldSelectionMapSyntaxSerializer to include commas (#8424)
1 parent cf0d38a commit 03ba732

9 files changed

+98
-39
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Language/SyntaxSerializers/FieldSelectionMapSyntaxSerializer.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected override ISyntaxVisitorAction Enter(
110110

111111
for (var i = 1; i < node.Fields.Count; i++)
112112
{
113-
WriteLineOrSpace(writer);
113+
WriteLineOrCommaSpace(writer);
114114
Visit(node.Fields[i], writer);
115115
}
116116
}
@@ -177,4 +177,16 @@ private void WriteLineOrSpace(ISyntaxWriter writer)
177177
writer.WriteSpace();
178178
}
179179
}
180+
181+
private void WriteLineOrCommaSpace(ISyntaxWriter writer)
182+
{
183+
if (options.Indented)
184+
{
185+
writer.WriteLine();
186+
}
187+
else
188+
{
189+
writer.Write(", ");
190+
}
191+
}
180192
}

src/HotChocolate/Fusion-vnext/test/Fusion.Composition.Tests/SatisfiabilityValidatorTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ type Product @key(fields: "id") @key(fields: "id pid") {
792792
# Schema B
793793
type Query { # Added
794794
productListByProductsIdAndPid(
795-
key: [ProductIdAndPidInput!]! @is(field: "products[{id pid}]")
795+
key: [ProductIdAndPidInput!]! @is(field: "products[{ id, pid }]")
796796
): ProductList @lookup @inaccessible
797797
productByIdAndPid(id: String!, pid: String): Product @lookup @inaccessible
798798
}
@@ -818,13 +818,13 @@ type Product @key(fields: "id pid") {
818818
type Query { # Added
819819
productListByProductsIdAndPidAndCategoryAndSelected(
820820
products: [ProductIdAndPidAndCategoryInput!]!
821-
@is(field: "products[{ id pid category: category.{ id tag } }]")
821+
@is(field: "products[{ id, pid, category: category.{ id, tag } }]")
822822
selectedId: String! @is(field: "selected.id")
823823
): ProductList @lookup @inaccessible
824824
productByIdAndPidAndCategory(
825825
id: String!
826826
pid: String
827-
category: CategoryIdAndTagInput! @is(field: "category.{ id tag }")
827+
category: CategoryIdAndTagInput! @is(field: "category.{ id, tag }")
828828
): Product @lookup @inaccessible
829829
categoryByIdAndTag(id: String!, tag: String): Category @lookup @inaccessible
830830
}
@@ -1791,7 +1791,7 @@ type Query {
17911791
17921792
type Product {
17931793
id: ID!
1794-
title(description: String @require(field: "{ name price }")): String
1794+
title(description: String @require(field: "{ name, price }")): String
17951795
}
17961796
""",
17971797
"""
@@ -1853,7 +1853,7 @@ type Query {
18531853
type Product {
18541854
id: ID!
18551855
title(
1856-
input: String @require(field: "{ a: category.name b: section.name }")
1856+
input: String @require(field: "{ a: category.name, b: section.name }")
18571857
): String
18581858
}
18591859
""",

src/HotChocolate/Fusion-vnext/test/Fusion.Language.Tests/FieldSelectionMapParserTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ public void Parse_SelectedListValue_MatchesSnapshot()
120120
[Theory]
121121
// https://graphql.github.io/composite-schemas-spec/draft/#sec-SelectedListValue
122122
[InlineData("parts[id]")]
123-
[InlineData("parts[{ id name }]")]
124-
[InlineData("parts[[{ id name }]]")]
125-
[InlineData("{ coordinates: coordinates[{ lat: x lon: y }] }")]
123+
[InlineData("parts[{ id, name }]")]
124+
[InlineData("parts[[{ id, name }]]")]
125+
[InlineData("{ coordinates: coordinates[{ lat: x, lon: y }] }")]
126126
public void ParseAndPrint_SelectedListValueValidExamples_Matches(string sourceText)
127127
{
128128
// arrange & act
@@ -134,7 +134,7 @@ public void ParseAndPrint_SelectedListValueValidExamples_Matches(string sourceTe
134134

135135
[Theory]
136136
// https://graphql.github.io/composite-schemas-spec/draft/#sec-SelectedListValue
137-
[InlineData("parts[id name]")]
137+
[InlineData("parts[id, name]")]
138138
public void Parse_SelectedListValueInvalidExamples_ThrowsSyntaxException(string sourceText)
139139
{
140140
// arrange & act
@@ -176,7 +176,7 @@ public void Parse_SelectedObjectValueNoSelectedValue_MatchesSnapshot()
176176
public void Parse_SelectedObjectValueMultipleFieldsNoSelectedValue_MatchesSnapshot()
177177
{
178178
// arrange
179-
var parser = new FieldSelectionMapParser("{ field1 field2 }");
179+
var parser = new FieldSelectionMapParser("{ field1, field2 }");
180180

181181
// act
182182
var selectedValueNode = parser.Parse();
@@ -187,8 +187,8 @@ public void Parse_SelectedObjectValueMultipleFieldsNoSelectedValue_MatchesSnapsh
187187

188188
[Theory]
189189
// https://graphql.github.io/composite-schemas-spec/draft/#sec-SelectedObjectValue
190-
[InlineData("dimension.{ size weight }")]
191-
[InlineData("{ size: dimensions.size weight: dimensions.weight }")]
190+
[InlineData("dimension.{ size, weight }")]
191+
[InlineData("{ size: dimensions.size, weight: dimensions.weight }")]
192192
public void ParseAndPrint_SelectedObjectValueValidExamples_Matches(string sourceText)
193193
{
194194
// arrange & act

src/HotChocolate/Fusion-vnext/test/Fusion.Language.Tests/FieldSelectionMapReaderTests.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void Read_SelectedObjectValueNoSelectedValue_MatchesSnapshot()
127127
public void Read_SelectedObjectValueMultipleFieldsNoSelectedValue_MatchesSnapshot()
128128
{
129129
// arrange
130-
var reader = new FieldSelectionMapReader("{ field1 field2 }");
130+
var reader = new FieldSelectionMapReader("{ field1, field2 }");
131131
List<SyntaxTokenInfo> readTokens = [];
132132

133133
// act
@@ -157,6 +157,23 @@ public void Read_SelectedValueMultiplePaths_MatchesSnapshot()
157157
readTokens.MatchSnapshot();
158158
}
159159

160+
[Fact]
161+
public void Read_WithoutComma_MatchesSnapshot()
162+
{
163+
// arrange
164+
var reader = new FieldSelectionMapReader("{ field1 field2 }");
165+
List<SyntaxTokenInfo> readTokens = [];
166+
167+
// act
168+
while (reader.Read())
169+
{
170+
readTokens.Add(SyntaxTokenInfo.FromReader(reader));
171+
}
172+
173+
// assert
174+
readTokens.MatchSnapshot();
175+
}
176+
160177
[Fact]
161178
public void Read_WithWhiteSpace_MatchesSnapshot()
162179
{

src/HotChocolate/Fusion-vnext/test/Fusion.Language.Tests/__snapshots__/FieldSelectionMapParserTests.Parse_SelectedObjectValueMultipleFieldsNoSelectedValue_MatchesSnapshot.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"Kind": "SelectedValue",
33
"Location": {
44
"Start": 0,
5-
"End": 17,
5+
"End": 18,
66
"Line": 1,
77
"Column": 1
88
},
99
"SelectedValueEntry": {
1010
"Kind": "SelectedValueEntry",
1111
"Location": {
1212
"Start": 0,
13-
"End": 17,
13+
"End": 18,
1414
"Line": 1,
1515
"Column": 1
1616
},
@@ -21,15 +21,15 @@
2121
"Kind": "SelectedObjectField",
2222
"Location": {
2323
"Start": 2,
24-
"End": 15,
24+
"End": 16,
2525
"Line": 1,
2626
"Column": 3
2727
},
2828
"Name": {
2929
"Kind": "Name",
3030
"Location": {
3131
"Start": 2,
32-
"End": 15,
32+
"End": 16,
3333
"Line": 1,
3434
"Column": 3
3535
},
@@ -40,18 +40,18 @@
4040
{
4141
"Kind": "SelectedObjectField",
4242
"Location": {
43-
"Start": 9,
44-
"End": 17,
43+
"Start": 10,
44+
"End": 18,
4545
"Line": 1,
46-
"Column": 10
46+
"Column": 11
4747
},
4848
"Name": {
4949
"Kind": "Name",
5050
"Location": {
51-
"Start": 9,
52-
"End": 17,
51+
"Start": 10,
52+
"End": 18,
5353
"Line": 1,
54-
"Column": 10
54+
"Column": 11
5555
},
5656
"Value": "field2"
5757
},
@@ -61,7 +61,7 @@
6161
"Kind": "SelectedObjectValue",
6262
"Location": {
6363
"Start": 0,
64-
"End": 17,
64+
"End": 18,
6565
"Line": 1,
6666
"Column": 1
6767
}

src/HotChocolate/Fusion-vnext/test/Fusion.Language.Tests/__snapshots__/FieldSelectionMapReaderTests.Read_SelectedObjectValueMultipleFieldsNoSelectedValue_MatchesSnapshot.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
},
1616
{
1717
"Kind": "Name",
18-
"Start": 9,
19-
"End": 15,
18+
"Start": 10,
19+
"End": 16,
2020
"Line": 1,
21-
"Column": 10
21+
"Column": 11
2222
},
2323
{
2424
"Kind": "RightBrace",
25-
"Start": 16,
26-
"End": 17,
25+
"Start": 17,
26+
"End": 18,
2727
"Line": 1,
28-
"Column": 17
28+
"Column": 18
2929
}
3030
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"Kind": "LeftBrace",
4+
"Start": 0,
5+
"End": 1,
6+
"Line": 1,
7+
"Column": 1
8+
},
9+
{
10+
"Kind": "Name",
11+
"Start": 2,
12+
"End": 8,
13+
"Line": 1,
14+
"Column": 3
15+
},
16+
{
17+
"Kind": "Name",
18+
"Start": 9,
19+
"End": 15,
20+
"Line": 1,
21+
"Column": 10
22+
},
23+
{
24+
"Kind": "RightBrace",
25+
"Start": 16,
26+
"End": 17,
27+
"Line": 1,
28+
"Column": 17
29+
}
30+
]

src/HotChocolate/Fusion-vnext/test/Fusion.Utilities.Tests/Rewriters/SelectedValueToSelectionSetRewriterTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ... on Book {
6262
},
6363
{
6464
"Product",
65-
"dimensions.{ size weight }",
65+
"dimensions.{ size, weight }",
6666
"""
6767
{
6868
dimensions {
@@ -74,7 +74,7 @@ ... on Book {
7474
},
7575
{
7676
"Product",
77-
"{ size: dimensions.size weight: dimensions.weight }",
77+
"{ size: dimensions.size, weight: dimensions.weight }",
7878
"""
7979
{
8080
dimensions {
@@ -97,7 +97,7 @@ ... on Book {
9797
},
9898
{
9999
"Product",
100-
"parts[{ id name }]",
100+
"parts[{ id, name }]",
101101
"""
102102
{
103103
parts {
@@ -109,7 +109,7 @@ ... on Book {
109109
},
110110
{
111111
"Product",
112-
"parts[[{ id name }]]",
112+
"parts[[{ id, name }]]",
113113
"""
114114
{
115115
parts {
@@ -121,7 +121,7 @@ ... on Book {
121121
},
122122
{
123123
"Location",
124-
"{ coordinates: coordinates[{ lat: x lon: y }] }",
124+
"{ coordinates: coordinates[{ lat: x, lon: y }] }",
125125
"""
126126
{
127127
coordinates {

src/HotChocolate/Fusion-vnext/test/Fusion.Utilities.Tests/Validators/FieldSelectionMapValidatorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ input UserInput {
209209
"The selection on one-of input type 'PersonByInput' must include a single field."
210210
})]
211211
[InlineData(
212-
"{ id name }",
212+
"{ id, name }",
213213
new[]
214214
{
215215
"The selection on one-of input type 'PersonByInput' must include a single field."
@@ -268,7 +268,7 @@ public static TheoryData<string, string, string> ValidExamplesData()
268268
{ "FindMediaInput", "Media", "{ bookId: <Book>.id } | { movieId: <Movie>.id }" },
269269
{ "Nested", "Media", "{ nested: { bookId: <Book>.id } | { movieId: <Movie>.id } }" },
270270
// Other tests.
271-
{ "String", "Book", "{ id title }" },
271+
{ "String", "Book", "{ id, title }" },
272272
{ "ID", "Query", "mediaById<Book>.author.id | mediaById<Movie>.id" },
273273
{ "ID", "Media", "{ bookId: <Book>.author.id } | { movieId: <Movie>.id }" }
274274
};
@@ -323,7 +323,7 @@ public static TheoryData<string, string, string, string[]> InvalidExamplesData()
323323
{
324324
"String",
325325
"Book",
326-
"{ id unknownField1 unknownField2 }",
326+
"{ id, unknownField1, unknownField2 }",
327327
[
328328
"The field 'unknownField1' does not exist on the type 'Book'.",
329329
"The field 'unknownField2' does not exist on the type 'Book'."

0 commit comments

Comments
 (0)