Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for descriptions to all AST nodes according to the draft specification #70

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace GraphQLParser.AST
TypeExtensionDefinition = 35,
DirectiveDefinition = 36,
Comment = 37,
Description = 38,
}
public class GraphQLArgument : GraphQLParser.AST.ASTNode, GraphQLParser.AST.INamedNode
{
Expand All @@ -61,6 +62,12 @@ namespace GraphQLParser.AST
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public string Text { get; set; }
}
public class GraphQLDescription : GraphQLParser.AST.ASTNode
{
public GraphQLDescription(string value) { }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public string Value { get; set; }
}
public class GraphQLDirective : GraphQLParser.AST.ASTNode, GraphQLParser.AST.INamedNode
{
public GraphQLDirective() { }
Expand Down Expand Up @@ -207,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.GraphQLTypeDefinition, GraphQLParser.AST.IHasDescriptionNode, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLObjectTypeDefinition() { }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down Expand Up @@ -251,9 +258,10 @@ namespace GraphQLParser.AST
public string? Value { get; set; }
public override string? ToString() { }
}
public class GraphQLSchemaDefinition : GraphQLParser.AST.ASTNode, GraphQLParser.AST.IHasDirectivesNode
public class GraphQLSchemaDefinition : GraphQLParser.AST.ASTNode, GraphQLParser.AST.IHasDescriptionNode, GraphQLParser.AST.IHasDirectivesNode
{
public GraphQLSchemaDefinition() { }
public GraphQLParser.AST.GraphQLDescription? Description { get; set; }
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.GraphQLOperationTypeDefinition>? OperationTypes { get; set; }
Expand All @@ -268,9 +276,10 @@ namespace GraphQLParser.AST
{
protected GraphQLType() { }
}
public abstract class GraphQLTypeDefinition : GraphQLParser.AST.ASTNode, GraphQLParser.AST.INamedNode
public abstract class GraphQLTypeDefinition : GraphQLParser.AST.ASTNode, GraphQLParser.AST.IHasDescriptionNode, GraphQLParser.AST.INamedNode
{
protected GraphQLTypeDefinition() { }
public GraphQLParser.AST.GraphQLDescription? Description { get; set; }
public GraphQLParser.AST.GraphQLName? Name { get; set; }
}
public class GraphQLTypeExtensionDefinition : GraphQLParser.AST.GraphQLTypeDefinition
Expand Down Expand Up @@ -304,6 +313,10 @@ namespace GraphQLParser.AST
public GraphQLParser.AST.GraphQLType? Type { get; set; }
public GraphQLParser.AST.GraphQLVariable? Variable { get; set; }
}
public interface IHasDescriptionNode
{
GraphQLParser.AST.GraphQLDescription? Description { get; set; }
}
public interface IHasDirectivesNode
{
System.Collections.Generic.List<GraphQLParser.AST.GraphQLDirective>? Directives { get; set; }
Expand Down
45 changes: 45 additions & 0 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,50 @@ public void Should_Parse_Unions(string text)
{
new Parser(new Lexer()).Parse(new Source(text)).ShouldNotBeNull();
}

[Theory]
[InlineData("\"schema description\" schema { query : Query }", "schema description")]
[InlineData("\"scalar description\" scalar DateTime", "scalar description")]
[InlineData("\"type description\" type Query { name : String }", "type description")]
[InlineData("\"interface description\" interface Person { name : String }", "interface description")]
[InlineData("\"union description\" union Person = He | She", "union description")]
[InlineData("\"enum description\" enum Currency { EUR USD }", "enum description")]
[InlineData("\"input description\" input PersonInput { name : String }", "input description")]
[InlineData("\"directive description\" directive @example on FIELD", "directive description")]
public void Should_Parse_TypeDefinition_Description(string text, string expectedDescription)
{
var document = new Parser(new Lexer()).Parse(new Source(text));
((IHasDescriptionNode)document.Definitions[0]).Description.Value.ShouldBe(expectedDescription);
}

[Fact]
public void Should_Parse_FieldDefinition_Description()
{
var text = "type Query { \"field description\" name : String }";
var document = new Parser(new Lexer()).Parse(new Source(text));
var objectTypeDefinition = document.Definitions.OfType<GraphQLObjectTypeDefinition>().First();
objectTypeDefinition.Fields[0].Description.Value.ShouldBe("field description");
}

[Fact]
public void Should_Parse_InputValueDefinition_Description()
{
var text = "type Query { name(\"input value description\" inputValue: Int) : String }";
var document = new Parser(new Lexer()).Parse(new Source(text));
var objectTypeDefinition = document.Definitions.OfType<GraphQLObjectTypeDefinition>().First();
objectTypeDefinition.Fields[0]
.Arguments[0].Description.Value.ShouldBe("input value description");
}

[Fact]
public void Should_Parse_EnumValueDefinition_Description()
{
var text = "enum Currency { \"EUR value description\" EUR \"USD value description\" USD }";
var document = new Parser(new Lexer()).Parse(new Source(text));
var enumTypeDefinition = document.Definitions.OfType<GraphQLEnumTypeDefinition>().First();
enumTypeDefinition.Values
.Select(v=>v.Description.Value)
.ShouldBe(new []{"EUR value description", "USD value description"});
}
}
}
3 changes: 2 additions & 1 deletion src/GraphQLParser/AST/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum ASTNodeKind
TypeExtensionDefinition,
DirectiveDefinition,
Comment,
Description
}

public enum OperationType
Expand All @@ -48,4 +49,4 @@ public enum OperationType
Mutation,
Subscription
}
}
}
14 changes: 14 additions & 0 deletions src/GraphQLParser/AST/GraphQLDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace GraphQLParser.AST
{
public class GraphQLDescription : ASTNode
{
public GraphQLDescription(string value)
{
Value = value;
}

public override ASTNodeKind Kind => ASTNodeKind.Description;

public string Value { get; set; }
}
}
4 changes: 2 additions & 2 deletions 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 : GraphQLTypeDefinition, IHasDirectivesNode, IHasDescriptionNode
{
public List<GraphQLDirective>? Directives { get; set; }

Expand All @@ -12,4 +12,4 @@ public class GraphQLObjectTypeDefinition : GraphQLTypeDefinition, IHasDirectives

public override ASTNodeKind Kind => ASTNodeKind.ObjectTypeDefinition;
}
}
}
6 changes: 4 additions & 2 deletions src/GraphQLParser/AST/GraphQLSchemaDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace GraphQLParser.AST
{
public class GraphQLSchemaDefinition : ASTNode, IHasDirectivesNode
public class GraphQLSchemaDefinition : ASTNode, IHasDirectivesNode, IHasDescriptionNode
{
public List<GraphQLDirective>? Directives { get; set; }

public override ASTNodeKind Kind => ASTNodeKind.SchemaDefinition;

public List<GraphQLOperationTypeDefinition>? OperationTypes { get; set; }

public GraphQLDescription? Description { get; set; }
}
}
}
6 changes: 4 additions & 2 deletions src/GraphQLParser/AST/GraphQLTypeDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace GraphQLParser.AST
{
public abstract class GraphQLTypeDefinition : ASTNode, INamedNode
public abstract class GraphQLTypeDefinition : ASTNode, INamedNode, IHasDescriptionNode
{
public GraphQLName? Name { get; set; }

public GraphQLDescription? Description { get; set; }
}
}
}
7 changes: 7 additions & 0 deletions src/GraphQLParser/AST/IHasDescriptionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace GraphQLParser.AST
{
public interface IHasDescriptionNode
{
GraphQLDescription? Description { get; set; }
}
}
Loading