Skip to content

Commit 2677be3

Browse files
committed
rework for locations and comments
1 parent 03dd0b4 commit 2677be3

40 files changed

+1651
-669
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
query q($id: ID) {
2+
person(
3+
valBool:
4+
#comment for bool
5+
true,
6+
valNull:
7+
#comment for null
8+
null,
9+
valEnum:
10+
#comment for enum
11+
RED,
12+
valList:
13+
#comment for list
14+
[1,2,3],
15+
valObj:
16+
#comment for object
17+
{ a: 1 },
18+
valInt:
19+
#comment for int
20+
7,
21+
valFloat:
22+
#comment for float
23+
1.4,
24+
valString:
25+
#comment for string
26+
"!#$",
27+
valVariable:
28+
#comment for variable
29+
$id
30+
)
31+
{
32+
a
33+
}
34+
}

src/GraphQLParser.Tests/GraphQLAstVisitorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public GraphQLAstVisitorTests()
5555
[Theory]
5656
[InlineData(IgnoreOptions.None)]
5757
[InlineData(IgnoreOptions.Comments)]
58+
[InlineData(IgnoreOptions.Locations)]
5859
[InlineData(IgnoreOptions.All)]
5960
public void Visit_BooleanValueArgument_VisitsOneBooleanValue(IgnoreOptions options)
6061
{
@@ -67,6 +68,7 @@ public void Visit_BooleanValueArgument_VisitsOneBooleanValue(IgnoreOptions optio
6768
[Theory]
6869
[InlineData(IgnoreOptions.None)]
6970
[InlineData(IgnoreOptions.Comments)]
71+
[InlineData(IgnoreOptions.Locations)]
7072
[InlineData(IgnoreOptions.All)]
7173
public void Visit_DefinitionWithSingleFragmentSpread_VisitsFragmentSpreadOneTime(IgnoreOptions options)
7274
{

src/GraphQLParser.Tests/ParserTests.cs

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public class ParserTests
1414

1515
[Theory]
1616
[InlineData(IgnoreOptions.None)]
17-
//[InlineData(IgnoreOptions.IgnoreComments)]
18-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
17+
//[InlineData(IgnoreOptions.Comments)]
18+
[InlineData(IgnoreOptions.Locations)]
19+
//[InlineData(IgnoreOptions.All)]
1920
public void Extra_Comments_Should_Read_Correctly(IgnoreOptions options)
2021
{
2122
string query = "ExtraComments".ReadGraphQLFile();
@@ -71,9 +72,10 @@ public void Comments_Can_Be_Ignored(IgnoreOptions options)
7172

7273
[Theory]
7374
[InlineData(IgnoreOptions.None)]
74-
//[InlineData(IgnoreOptions.IgnoreComments)]
75-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
76-
public void Comments_on_FragmentSpread_Should_Read_Correclty(IgnoreOptions options)
75+
//[InlineData(IgnoreOptions.Comments)]
76+
[InlineData(IgnoreOptions.Locations)]
77+
//[InlineData(IgnoreOptions.All)]
78+
public void Comments_on_FragmentSpread_Should_Read_Correctly(IgnoreOptions options)
7779
{
7880
string query = "CommentsOnFragmentSpread".ReadGraphQLFile();
7981

@@ -89,9 +91,55 @@ public void Comments_on_FragmentSpread_Should_Read_Correclty(IgnoreOptions optio
8991

9092
[Theory]
9193
[InlineData(IgnoreOptions.None)]
92-
//[InlineData(IgnoreOptions.IgnoreComments)]
93-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
94-
public void Comments_on_FragmentInline_Should_Read_Correclty(IgnoreOptions options)
94+
//[InlineData(IgnoreOptions.Comments)]
95+
[InlineData(IgnoreOptions.Locations)]
96+
//[InlineData(IgnoreOptions.All)]
97+
public void Comments_on_Values_Should_Read_Correctly(IgnoreOptions options)
98+
{
99+
string query = "CommentsOnValues".ReadGraphQLFile();
100+
101+
using var document = query.Parse(new ParserOptions { Ignore = options });
102+
document.Definitions.Count.ShouldBe(1);
103+
var def = document.Definitions.First() as GraphQLOperationDefinition;
104+
def.SelectionSet.Selections.Count.ShouldBe(1);
105+
var field = def.SelectionSet.Selections.First() as GraphQLFieldSelection;
106+
field.SelectionSet.Selections.Count.ShouldBe(1);
107+
field.Arguments.Count.ShouldBe(9);
108+
109+
var boolValue = field.Arguments[0].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
110+
boolValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for bool");
111+
112+
var nullValue = field.Arguments[1].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
113+
nullValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for null");
114+
115+
var enumValue = field.Arguments[2].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
116+
enumValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for enum");
117+
118+
var listValue = field.Arguments[3].Value.ShouldBeAssignableTo<GraphQLListValue>();
119+
listValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for list");
120+
121+
var objValue = field.Arguments[4].Value.ShouldBeAssignableTo<GraphQLObjectValue>();
122+
objValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for object");
123+
124+
var intValue = field.Arguments[5].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
125+
intValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for int");
126+
127+
var floatValue = field.Arguments[6].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
128+
floatValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for float");
129+
130+
var stringValue = field.Arguments[7].Value.ShouldBeAssignableTo<GraphQLScalarValue>();
131+
stringValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for string");
132+
133+
var varValue = field.Arguments[8].Value.ShouldBeAssignableTo<GraphQLVariable>();
134+
varValue.Comment.ShouldNotBeNull().Text.ShouldBe("comment for variable");
135+
}
136+
137+
[Theory]
138+
[InlineData(IgnoreOptions.None)]
139+
//[InlineData(IgnoreOptions.Comments)]
140+
[InlineData(IgnoreOptions.Locations)]
141+
//[InlineData(IgnoreOptions.All)]
142+
public void Comments_on_FragmentInline_Should_Read_Correctly(IgnoreOptions options)
95143
{
96144
string query = "CommentsOnInlineFragment".ReadGraphQLFile();
97145

@@ -107,9 +155,10 @@ public void Comments_on_FragmentInline_Should_Read_Correclty(IgnoreOptions optio
107155

108156
[Theory]
109157
[InlineData(IgnoreOptions.None)]
110-
//[InlineData(IgnoreOptions.IgnoreComments)]
111-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
112-
public void Comments_on_Variable_Should_Read_Correclty(IgnoreOptions options)
158+
//[InlineData(IgnoreOptions.Comments)]
159+
[InlineData(IgnoreOptions.Locations)]
160+
//[InlineData(IgnoreOptions.All)]
161+
public void Comments_on_Variable_Should_Read_Correctly(IgnoreOptions options)
113162
{
114163
string query = "CommentsOnVariables".ReadGraphQLFile();
115164

@@ -124,8 +173,9 @@ public void Comments_on_Variable_Should_Read_Correclty(IgnoreOptions options)
124173

125174
[Theory]
126175
[InlineData(IgnoreOptions.None)]
127-
//[InlineData(IgnoreOptions.IgnoreComments)]
128-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
176+
//[InlineData(IgnoreOptions.Comments)]
177+
[InlineData(IgnoreOptions.Locations)]
178+
//[InlineData(IgnoreOptions.All)]
129179
public void Comments_On_SelectionSet_Should_Read_Correctly(IgnoreOptions options)
130180
{
131181
using var document = @"
@@ -147,9 +197,10 @@ public void Comments_On_SelectionSet_Should_Read_Correctly(IgnoreOptions options
147197

148198
[Theory]
149199
[InlineData(IgnoreOptions.None)]
150-
//[InlineData(IgnoreOptions.IgnoreComments)]
151-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
152-
public void Comments_On_Enums_Should_Read_Correctly(IgnoreOptions options)
200+
//[InlineData(IgnoreOptions.Comments)]
201+
[InlineData(IgnoreOptions.Locations)]
202+
//[InlineData(IgnoreOptions.All)]
203+
public void Comments_On_Enum_Definitions_Should_Read_Correctly(IgnoreOptions options)
153204
{
154205
using var document = @"
155206
# different animals
@@ -352,20 +403,31 @@ public void Parse_FieldWithOperationTypeAndNameInput_SelectionSetContainsSingleF
352403

353404
[Theory]
354405
[InlineData(IgnoreOptions.None)]
355-
//[InlineData(IgnoreOptions.IgnoreComments)]
356-
//[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
406+
[InlineData(IgnoreOptions.Comments)]
407+
[InlineData(IgnoreOptions.Locations)]
408+
[InlineData(IgnoreOptions.All)]
357409
public void Parse_KitchenSink_DoesNotThrowError(IgnoreOptions options)
358410
{
359411
using var document = "KitchenSink".ReadGraphQLFile().Parse(new ParserOptions { Ignore = options });
360412
var typeDef = document.Definitions.OfType<GraphQLObjectTypeDefinition>().First(d => d.Name.Value == "Foo");
361413
var fieldDef = typeDef.Fields.First(d => d.Name.Value == "three");
362-
fieldDef.Comment.ShouldNotBeNull().Text.ShouldBe($" multiline comments{_nl} with very importand description #{_nl} # and symbol # and ##");
414+
if (options.HasFlag(IgnoreOptions.Comments))
415+
fieldDef.Comment.ShouldBeNull();
416+
else
417+
fieldDef.Comment.ShouldNotBeNull().Text.ShouldBe($" multiline comments{_nl} with very importand description #{_nl} # and symbol # and ##");
363418

364419
// Schema description
365420
// https://github.com/graphql/graphql-spec/pull/466
366421
var comment = document.Definitions.OfType<GraphQLSchemaDefinition>().First().Comment;
367-
comment.ShouldNotBeNull();
368-
((string)comment.Text).StartsWith(" Copyright (c) 2015, Facebook, Inc.").ShouldBeTrue();
422+
if (options.HasFlag(IgnoreOptions.Comments))
423+
{
424+
comment.ShouldBeNull();
425+
}
426+
else
427+
{
428+
comment.ShouldNotBeNull();
429+
((string)comment.Text).StartsWith(" Copyright (c) 2015, Facebook, Inc.").ShouldBeTrue();
430+
}
369431
}
370432

371433
[Theory]
@@ -777,7 +839,7 @@ directive @TestDirective (
777839
".Parse(new ParserOptions { Ignore = options });
778840
var defs = document.Definitions;
779841
defs.Count.ShouldBe(8);
780-
var parseComments = options == IgnoreOptions.None;
842+
var parseComments = !options.HasFlag(IgnoreOptions.Comments);
781843

782844
var schemaDef = defs.Single(x => x is GraphQLSchemaDefinition) as GraphQLSchemaDefinition;
783845
schemaDef.Description.Value.ShouldBe("Super schema");
@@ -961,7 +1023,7 @@ directive @TestDirective (
9611023
".Parse(new ParserOptions { Ignore = options });
9621024
var defs = document.Definitions;
9631025
defs.Count.ShouldBe(8);
964-
var parseComments = options == IgnoreOptions.None;
1026+
var parseComments = !options.HasFlag(IgnoreOptions.Comments);
9651027

9661028
var schemaDef = defs.Single(x => x is GraphQLSchemaDefinition) as GraphQLSchemaDefinition;
9671029
schemaDef.Description.Value.ShouldBe("Super schema");
@@ -1145,7 +1207,7 @@ directive @TestDirective (
11451207
".Parse(new ParserOptions { Ignore = options });
11461208
var defs = document.Definitions;
11471209
defs.Count.ShouldBe(8);
1148-
var parseComments = options == IgnoreOptions.None;
1210+
var parseComments = !options.HasFlag(IgnoreOptions.Comments);
11491211

11501212
var schemaDef = defs.Single(x => x is GraphQLSchemaDefinition) as GraphQLSchemaDefinition;
11511213
schemaDef.Description.Value.ShouldBe("Super schema");

src/GraphQLParser/AST/GraphQLArgument.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ public class GraphQLArgument : ASTNode, INamedNode
1111
public GraphQLValue? Value { get; set; }
1212
}
1313

14+
internal sealed class GraphQLArgumentWithLocation : GraphQLArgument
15+
{
16+
private GraphQLLocation _location;
17+
18+
public override GraphQLLocation Location
19+
{
20+
get => _location;
21+
set => _location = value;
22+
}
23+
}
24+
25+
internal sealed class GraphQLArgumentWithComment : GraphQLArgument
26+
{
27+
private GraphQLComment? _comment;
28+
29+
public override GraphQLComment? Comment
30+
{
31+
get => _comment;
32+
set => _comment = value;
33+
}
34+
}
35+
1436
internal sealed class GraphQLArgumentFull : GraphQLArgument
1537
{
1638
private GraphQLLocation _location;

src/GraphQLParser/AST/GraphQLComment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class GraphQLComment : ASTNode
2424
//}
2525
}
2626

27-
internal class GraphQLCommentFull : GraphQLComment
27+
internal class GraphQLCommentWithLocation : GraphQLComment
2828
{
2929
private GraphQLLocation _location;
3030

src/GraphQLParser/AST/GraphQLDescription.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@ public class GraphQLDescription : ASTNode
1010

1111
public ROM Value { get; set; }
1212
}
13+
14+
internal sealed class GraphQLDescriptionWithLocation : GraphQLDescription
15+
{
16+
private GraphQLLocation _location;
17+
18+
public override GraphQLLocation Location
19+
{
20+
get => _location;
21+
set => _location = value;
22+
}
23+
}
1324
}

src/GraphQLParser/AST/GraphQLDirective.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,43 @@ public class GraphQLDirective : ASTNode, INamedNode
1616
public GraphQLName? Name { get; set; }
1717
}
1818

19+
internal sealed class GraphQLDirectiveWithLocation : GraphQLDirective
20+
{
21+
private GraphQLLocation _location;
22+
23+
public override GraphQLLocation Location
24+
{
25+
get => _location;
26+
set => _location = value;
27+
}
28+
}
29+
30+
internal sealed class GraphQLDirectiveWithComment : GraphQLDirective
31+
{
32+
private GraphQLComment? _comment;
33+
34+
public override GraphQLComment? Comment
35+
{
36+
get => _comment;
37+
set => _comment = value;
38+
}
39+
}
40+
1941
internal sealed class GraphQLDirectiveFull : GraphQLDirective
2042
{
2143
private GraphQLLocation _location;
22-
//private GraphQLComment? _comment;
44+
private GraphQLComment? _comment;
2345

2446
public override GraphQLLocation Location
2547
{
2648
get => _location;
2749
set => _location = value;
2850
}
2951

30-
// TODO: this property is not set anywhere (yet), so it makes no sense to create a field for it
31-
//public override GraphQLComment? Comment
32-
//{
33-
// get => _comment;
34-
// set => _comment = value;
35-
//}
52+
public override GraphQLComment? Comment
53+
{
54+
get => _comment;
55+
set => _comment = value;
56+
}
3657
}
3758
}

src/GraphQLParser/AST/GraphQLDirectiveDefinition.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ public class GraphQLDirectiveDefinition : GraphQLTypeDefinition
2828
public bool Repeatable { get; set; }
2929
}
3030

31+
internal sealed class GraphQLDirectiveDefinitionWithLocation : GraphQLDirectiveDefinition
32+
{
33+
private GraphQLLocation _location;
34+
35+
public override GraphQLLocation Location
36+
{
37+
get => _location;
38+
set => _location = value;
39+
}
40+
}
41+
42+
internal sealed class GraphQLDirectiveDefinitionWithComment : GraphQLDirectiveDefinition
43+
{
44+
private GraphQLComment? _comment;
45+
46+
public override GraphQLComment? Comment
47+
{
48+
get => _comment;
49+
set => _comment = value;
50+
}
51+
}
52+
3153
internal sealed class GraphQLDirectiveDefinitionFull : GraphQLDirectiveDefinition
3254
{
3355
private GraphQLLocation _location;

0 commit comments

Comments
 (0)