Skip to content

Commit

Permalink
Add support for description parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Jun 28, 2021
1 parent 8e0ba08 commit cae3960
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 31 deletions.
25 changes: 15 additions & 10 deletions src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace GraphQLParser.AST
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public GraphQLParser.AST.GraphQLName? Name { get; set; }
}
public class GraphQLDirectiveDefinition : GraphQLParser.AST.GraphQLTypeDefinition
public class GraphQLDirectiveDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription
{
public GraphQLDirectiveDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLInputValueDefinition>? Arguments { get; set; }
Expand All @@ -86,20 +86,20 @@ namespace GraphQLParser.AST
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
}
public class GraphQLEnumTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLEnumTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLEnumTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLEnumValueDefinition>? Values { get; set; }
}
public class GraphQLEnumValueDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLEnumValueDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLEnumValueDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
}
public class GraphQLFieldDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLFieldDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLFieldDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLInputValueDefinition>? Arguments { get; set; }
Expand Down Expand Up @@ -138,22 +138,22 @@ namespace GraphQLParser.AST
public GraphQLParser.AST.GraphQLSelectionSet? SelectionSet { get; set; }
public GraphQLParser.AST.GraphQLNamedType? TypeCondition { get; set; }
}
public class GraphQLInputObjectTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLInputObjectTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLInputObjectTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLInputValueDefinition>? Fields { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
}
public class GraphQLInputValueDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLInputValueDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLInputValueDefinition() { }
public GraphQLParser.AST.GraphQLValue? DefaultValue { get; set; }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public GraphQLParser.AST.GraphQLType? Type { get; set; }
}
public class GraphQLInterfaceTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLInterfaceTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLInterfaceTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down Expand Up @@ -214,7 +214,7 @@ namespace GraphQLParser.AST
public GraphQLParser.AST.GraphQLName? Name { get; set; }
public GraphQLParser.AST.GraphQLValue? Value { get; set; }
}
public class GraphQLObjectTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLObjectTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLObjectTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down Expand Up @@ -245,7 +245,7 @@ namespace GraphQLParser.AST
public GraphQLParser.AST.OperationType Operation { get; set; }
public GraphQLParser.AST.GraphQLNamedType? Type { get; set; }
}
public class GraphQLScalarTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLScalarTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLScalarTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down Expand Up @@ -280,13 +280,18 @@ namespace GraphQLParser.AST
protected GraphQLTypeDefinition() { }
public GraphQLParser.AST.GraphQLName? Name { get; set; }
}
public abstract class GraphQLTypeDefinitionWithDescription : GraphQLParser.AST.GraphQLTypeDefinition
{
protected GraphQLTypeDefinitionWithDescription() { }
public GraphQLParser.AST.GraphQLScalarValue? Description { get; set; }
}
public class GraphQLTypeExtensionDefinition : GraphQLParser.AST.GraphQLTypeDefinition
{
public GraphQLTypeExtensionDefinition() { }
public GraphQLParser.AST.GraphQLObjectTypeDefinition? Definition { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
}
public class GraphQLUnionTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLUnionTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinitionWithDescription, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLUnionTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down
125 changes: 125 additions & 0 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,5 +519,130 @@ public void Should_Throw_On_Empty_Types_With_Braces(string text, int line, int c
ex.Column.ShouldBe(column);
ex.Message.ShouldContain("Expected Name, found }");
}

[Theory]
[InlineData(IgnoreOptions.None)]
[InlineData(IgnoreOptions.IgnoreComments)]
[InlineData(IgnoreOptions.IgnoreCommentsAndLocations)]
public void Descriptions_Should_Read_Correctly(IgnoreOptions options)
{
using var document = @"
""A JSON scalar""
scalar JSON
""""""
Human type
""""""
type Human {
""""""
Name of human
""""""
name: String
""Test""
test(
""desc""
arg: Int
): Int
}
""Test interface""
interface TestInterface {
""Object name""
name: String
}
""""""
Test union
""""""
union TestUnion = Test1 | Test2
""Example enum""
enum Colors {
""Red"" RED
""Blue"" BLUE
}
""""""
This is an example input object
Line two of the description
""""""
input TestInputObject {
""""""
The value of the input object
(any JSON value is accepted)
""""""
Value: JSON
}
""Test directive""
directive @TestDirective (
""Example""
Value: Int
) on QUERY
".Parse(new ParserOptions { Ignore = options });
var defs = document.Definitions;
defs.Count.ShouldBe(7);

var scalarDef = defs.Single(x => x is GraphQLScalarTypeDefinition) as GraphQLScalarTypeDefinition;
scalarDef.Name.Value.ShouldBe("JSON");
scalarDef.Description.Value.ShouldBe("A JSON scalar");

var objectDef = defs.Single(x => x is GraphQLObjectTypeDefinition) as GraphQLObjectTypeDefinition;
objectDef.Name.Value.ShouldBe("Human");
objectDef.Description.Value.ShouldBe("Human type");
objectDef.Fields.Count.ShouldBe(2);
objectDef.Fields[0].Name.Value.ShouldBe("name");
objectDef.Fields[0].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("String");
objectDef.Fields[0].Description.Value.ShouldBe("Name of human");
objectDef.Fields[1].Name.Value.ShouldBe("test");
objectDef.Fields[1].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("Int");
objectDef.Fields[1].Description.Value.ShouldBe("Test");
objectDef.Fields[1].Arguments.Count.ShouldBe(1);
objectDef.Fields[1].Arguments[0].Name.Value.ShouldBe("arg");
objectDef.Fields[1].Arguments[0].Description.Value.ShouldBe("desc");
objectDef.Fields[1].Arguments[0].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("Int");

var interfaceDef = defs.Single(x => x is GraphQLInterfaceTypeDefinition) as GraphQLInterfaceTypeDefinition;
interfaceDef.Name.Value.ShouldBe("TestInterface");
interfaceDef.Description.Value.ShouldBe("Test interface");
interfaceDef.Fields.Count.ShouldBe(1);
interfaceDef.Fields[0].Name.Value.ShouldBe("name");
interfaceDef.Fields[0].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("String");
interfaceDef.Fields[0].Description.Value.ShouldBe("Object name");

var unionDef = defs.Single(x => x is GraphQLUnionTypeDefinition) as GraphQLUnionTypeDefinition;
unionDef.Name.Value.ShouldBe("TestUnion");
unionDef.Description.Value.ShouldBe("Test union");
unionDef.Types.Count.ShouldBe(2);
unionDef.Types[0].Name.Value.ShouldBe("Test1");
unionDef.Types[1].Name.Value.ShouldBe("Test2");

var enumDef = defs.Single(x => x is GraphQLEnumTypeDefinition) as GraphQLEnumTypeDefinition;
enumDef.Name.Value.ShouldBe("Colors");
enumDef.Description.Value.ShouldBe("Example enum");
enumDef.Values.Count.ShouldBe(2);
enumDef.Values[0].Name.Value.ShouldBe("RED");
enumDef.Values[0].Description.Value.ShouldBe("Red");
enumDef.Values[1].Name.Value.ShouldBe("BLUE");
enumDef.Values[1].Description.Value.ShouldBe("Blue");

var inputDef = defs.Single(x => x is GraphQLInputObjectTypeDefinition) as GraphQLInputObjectTypeDefinition;
inputDef.Name.Value.ShouldBe("TestInputObject");
inputDef.Description.Value.ShouldBe("This is an example input object\nLine two of the description");
inputDef.Fields.Count.ShouldBe(1);
inputDef.Fields[0].Name.Value.ShouldBe("Value");
inputDef.Fields[0].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("JSON");
inputDef.Fields[0].Description.Value.ShouldBe("The value of the input object\n (any JSON value is accepted)");

var directiveDef = defs.Single(x => x is GraphQLDirectiveDefinition) as GraphQLDirectiveDefinition;
directiveDef.Name.Value.ShouldBe("TestDirective");
directiveDef.Description.Value.ShouldBe("Test directive");
directiveDef.Arguments.Count.ShouldBe(1);
directiveDef.Arguments[0].Name.Value.ShouldBe("Value");
directiveDef.Arguments[0].Type.ShouldBeAssignableTo<GraphQLNamedType>().Name.Value.ShouldBe("Int");
directiveDef.Arguments[0].Description.Value.ShouldBe("Example");
}

}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLDirectiveDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace GraphQLParser.AST
/// <summary>
/// Represents a directive definition.
/// </summary>
public class GraphQLDirectiveDefinition : GraphQLTypeDefinition
public class GraphQLDirectiveDefinition : GraphQLTypeDefinitionWithDescription
{
public List<GraphQLInputValueDefinition>? Arguments { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLEnumTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLEnumTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLEnumTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLEnumValueDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLEnumValueDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLEnumValueDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLFieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLFieldDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLFieldDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
public List<GraphQLInputValueDefinition>? Arguments { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLInputObjectTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLInputObjectTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLInputObjectTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLInputValueDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLInputValueDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLInputValueDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
public GraphQLValue? DefaultValue { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLInterfaceTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLInterfaceTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLInterfaceTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLObjectTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLObjectTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLObjectTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLScalarTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLScalarTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLScalarTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions src/GraphQLParser/AST/GraphQLTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ public abstract class GraphQLTypeDefinition : ASTNode, INamedNode
/// <inheritdoc/>
public GraphQLName? Name { get; set; }
}

public abstract class GraphQLTypeDefinitionWithDescription : GraphQLTypeDefinition
{
/// <summary>
/// Description of the node as represented by a nested node.
/// </summary>
public GraphQLScalarValue? Description { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLUnionTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQLParser.AST
{
public class GraphQLUnionTypeDefinition : GraphQLTypeDefinition, IHasDirectivesNode
public class GraphQLUnionTypeDefinition : GraphQLTypeDefinitionWithDescription, IHasDirectivesNode
{
/// <inheritdoc/>
public List<GraphQLDirective>? Directives { get; set; }
Expand Down
Loading

0 comments on commit cae3960

Please sign in to comment.