Skip to content

Commit

Permalink
Improve some formatting, fix the bug that directives didn't get print
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonC9018 committed Jan 15, 2024
1 parent 635bd79 commit a48495d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void VisitOperationDefinition(
writer.WriteMany(node.VariableDefinitions, VisitVariableDefinition);
writer.Write(')');
}

WriteDirectives(node.Directives, writer);

if (writeOperation)
Expand All @@ -53,6 +53,8 @@ private void VisitVariableDefinition(VariableDefinitionNode node, ISyntaxWriter
writer.Write(" = ");
writer.WriteValue(node.DefaultValue);
}

WriteDirectives(node.Directives, writer);
}

private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter writer)
Expand Down Expand Up @@ -82,11 +84,8 @@ private void VisitFragmentDefinition(FragmentDefinitionNode node, ISyntaxWriter

WriteDirectives(node.Directives, writer);

if (node.SelectionSet is not null)
{
writer.WriteSpace();
VisitSelectionSet(node.SelectionSet, writer);
}
writer.WriteSpace();
VisitSelectionSet(node.SelectionSet, writer);
}

private void VisitSelectionSet(SelectionSetNode node, ISyntaxWriter writer)
Expand Down Expand Up @@ -230,10 +229,7 @@ private void VisitInlineFragment(InlineFragmentNode node, ISyntaxWriter writer)

WriteDirectives(node.Directives, writer);

if (node.SelectionSet is { })
{
writer.WriteSpace();
VisitSelectionSet(node.SelectionSet, writer);
}
writer.WriteSpace();
VisitSelectionSet(node.SelectionSet, writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ private OperationDefinitionNode ParseOperationDefinition()
var selectionSet = ParseSelectionSet();
var location = CreateLocation(in start);

return new OperationDefinitionNode
(
return new OperationDefinitionNode(
location,
name,
operation,
variableDefinitions,
directives,
selectionSet
);
selectionSet);
}

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

return new OperationDefinitionNode
(
return new OperationDefinitionNode(
location,
null,
name: null,
OperationType.Query,
Array.Empty<VariableDefinitionNode>(),
Array.Empty<DirectiveNode>(),
selectionSet
);
selectionSet);
}

/// <summary>
Expand Down Expand Up @@ -133,18 +129,16 @@ private VariableDefinitionNode ParseVariableDefinition()
? ParseValueLiteral(true)
: null;
var directives =
ParseDirectives(true);
ParseDirectives(isConstant: true);

var location = CreateLocation(in start);

return new VariableDefinitionNode
(
return new VariableDefinitionNode(
location,
variable,
type,
defaultValue,
directives
);
directives);
}

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

return new VariableNode
(
return new VariableNode(
location,
name
);
name);
}

/// <summary>
Expand Down Expand Up @@ -200,11 +192,9 @@ private SelectionSetNode ParseSelectionSet()

var location = CreateLocation(in start);

return new SelectionSetNode
(
return new SelectionSetNode(
location,
selections
);
selections);
}

/// <summary>
Expand Down Expand Up @@ -259,16 +249,14 @@ private FieldNode ParseField()

var location = CreateLocation(in start);

return new FieldNode
(
return new FieldNode(
location,
name,
alias,
required,
directives,
arguments,
selectionSet
);
selectionSet);
}

private INullabilityNode? ParseRequiredStatus()
Expand Down Expand Up @@ -357,11 +345,9 @@ private ArgumentNode ParseArgument(bool isConstant)

var location = CreateLocation(in start);

return new ArgumentNode
(
return new ArgumentNode(
location,
name,
value
);
value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public class QuerySyntaxPrinterTests
public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame()
{
// arrange
var query = "{ foo(s: \"String\") { bar @foo " +
"{ baz @foo @bar } } }";
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
const string query =
"""
{ foo(s: "String") { bar @foo { baz @foo @bar } } }
""";
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString(false);
Expand All @@ -25,9 +27,11 @@ public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame()
public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted()
{
// arrange
var query = "{ foo(s: \"String\") { bar @foo " +
"{ baz @foo @bar } } }";
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
const string query =
"""
{ foo(s: "String") { bar @foo { baz @foo @bar } } }
""";
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString(false);
Expand All @@ -40,8 +44,8 @@ public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted()
public void Serialize_ShortHandQueryWithIndentation_LineBetweenFields()
{
// arrange
var query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }";
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
const string query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }";
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString();
Expand All @@ -55,7 +59,7 @@ public void Serialize_KitchenSinkWithIndentation_OutputIsFormatted()
{
// arrange
var query = FileResource.Open("kitchen-sink.graphql");
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString();
Expand All @@ -69,7 +73,7 @@ public void Serialize_KitchenSinkWithoutIndentation_OutputIsOneLine()
{
// arrange
var query = FileResource.Open("kitchen-sink.graphql");
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString();
Expand All @@ -83,39 +87,39 @@ public void Serialize_KitchenSinkWithIndentation_CanBeParsed()
{
// arrange
var query = FileResource.Open("kitchen-sink.graphql");
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString();

// assert
result.MatchSnapshot();

}

[Fact]
public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed()
{
// arrange
var query = FileResource.Open("kitchen-sink.graphql");
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var serializedQuery = queryDocument.ToString();

// assert
DocumentNode parsedQuery = Utf8GraphQLParser.Parse(serializedQuery);
var parsedQuery = Utf8GraphQLParser.Parse(serializedQuery);
Assert.Equal(serializedQuery, parsedQuery.ToString());
}

[Fact]
public void Serialize_QueryWithVarDeclaration_InOutShouldBeTheSame()
{
// arrange
var query =
"query Foo($bar: [String!]!) { foo(s: \"String\") " +
"{ bar @foo { baz @foo @bar } } }";
DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);
const string query =
"""
query Foo($bar: [String!]!) { foo(s: "String") { bar @foo { baz @foo @bar } } }
""";
var queryDocument = Utf8GraphQLParser.Parse(query);

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

// act
Expand All @@ -138,4 +142,22 @@ public void Serialize_FragmentWithVariableDefs_InOutShouldBeTheSame()
// assert
Assert.Equal(query, result);
}

// https://github.com/ChilliCream/graphql-platform/issues/1997
[Fact]
public void Serialize_QueryWithDirectivesOnVariables_InOutShouldBeTheSame()
{
// arrange
const string query =
"""
query Foo($variable: String @foo) { foo(a: $variable) }
""";
var queryDocument = Utf8GraphQLParser.Parse(query);

// act
var result = queryDocument.ToString(false);

// assert
Assert.Equal(query, result);
}
}

0 comments on commit a48495d

Please sign in to comment.