Skip to content

Commit da93743

Browse files
committed
Merge from v8
2 parents 388fbaf + 65a05bf commit da93743

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

README.md

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
[![License](https://img.shields.io/github/license/graphql-dotnet/parser)](LICENSE.md)
44
[![codecov](https://codecov.io/gh/graphql-dotnet/parser/branch/master/graph/badge.svg?token=GEjwg1by60)](https://codecov.io/gh/graphql-dotnet/parser)
55
[![Nuget](https://img.shields.io/nuget/dt/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)
6-
[![NuGet](https://img.shields.io/nuget/v/GraphQL-Parser.svg)](https://www.nuget.org/packages/GraphQL-Parser)
6+
[![Nuget](https://img.shields.io/nuget/v/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)
77
[![GitHub Release Date](https://img.shields.io/github/release-date/graphql-dotnet/parser?label=released)](https://github.com/graphql-dotnet/parser/releases)
88
[![GitHub commits since latest release (by date)](https://img.shields.io/github/commits-since/graphql-dotnet/parser/latest?label=new+commits)](https://github.com/graphql-dotnet/parser/commits/master)
9-
![Size](https://img.shields.io/github/repo-size/graphql-dotnet/parser)
10-
119
[![GitHub contributors](https://img.shields.io/github/contributors/graphql-dotnet/parser)](https://github.com/graphql-dotnet/parser/graphs/contributors)
12-
![Activity](https://img.shields.io/github/commit-activity/w/graphql-dotnet/parser)
13-
![Activity](https://img.shields.io/github/commit-activity/m/graphql-dotnet/parser)
14-
![Activity](https://img.shields.io/github/commit-activity/y/graphql-dotnet/parser)
10+
![Size](https://img.shields.io/github/repo-size/graphql-dotnet/parser)
1511

1612
This library contains a lexer and parser as well as the complete [GraphQL AST model](http://spec.graphql.org/October2021/#sec-Appendix-Grammar-Summary)
1713
that allows you to work with GraphQL documents compatible with the [October 2021 spec](https://spec.graphql.org/October2021/).
@@ -78,15 +74,82 @@ Default implementation traverses all AST nodes of the provided one. You can
7874
inherit from it and override desired methods to implement your own AST
7975
processing algorithm.
8076

77+
### SDLPrinter
78+
8179
For printing SDL from AST, you can use `SDLPrinter`. This is a highly
8280
optimized visitor for asynchronous non-blocking SDL output into provided
8381
`TextWriter`. In the majority of cases it does not allocate memory in
84-
the managed heap at all.
82+
the managed heap at all. Extension methods are also provided for printing
83+
directly to a string, which utilize the `StringBuilder` and `StringWriter`
84+
classes.
85+
86+
```csharp
87+
var document = Parser.Parse("query { hero { name age } }");
88+
89+
// print to a string with default options
90+
var sdl = new SDLPrinter().Print(document);
91+
92+
// print to a string builder
93+
var sb = new StringBuilder();
94+
new SDLPrinter().Print(document, sb);
95+
96+
// print to a string with some options
97+
var sdlPrinter = new SDLPrinter(
98+
new SDLPrinterOptions {
99+
PrintComments = true,
100+
EachDirectiveLocationOnNewLine = true,
101+
EachUnionMemberOnNewLine = true,
102+
});
103+
var sdl = sdlPrinter.Print(document);
104+
105+
// print to a stream asynchronously
106+
using var writer = new StreamWriter(stream);
107+
await sdlPrinter.PrintAsync(document, writer, default);
108+
await writer.FlushAsync();
109+
```
110+
111+
Output:
112+
113+
```graphql
114+
query {
115+
hero {
116+
name
117+
age
118+
}
119+
}
120+
```
121+
122+
### SDLSorter
123+
124+
An AST document can be sorted with the `SDLSorter` using a predefined
125+
sort order. You can specify the string comparison; by default it uses
126+
a culture-invariant case-insensitive comparison. Any futher customization
127+
is possible by deriving from `SDLSorterOptions` and overriding the `Compare`
128+
methods.
129+
130+
```csharp
131+
var document = Parser.Parse("query { hero { name age } }");
132+
SDLSorter.Sort(document);
133+
var sdl = new SDLPrinter().Print(document);
134+
```
135+
136+
Output:
137+
138+
```graphql
139+
query {
140+
hero {
141+
age
142+
name
143+
}
144+
}
145+
```
146+
147+
### StructurePrinter
85148

86149
You can also find a `StructurePrinter` visitor that prints AST into the
87150
provided `TextWriter` as a hierarchy of node types. It can be useful
88151
when debugging for better understanding the AST structure.
89-
Consider GraphQL document
152+
Consider the following GraphQL document:
90153

91154
```graphql
92155
query a { name age }
@@ -105,14 +168,14 @@ Document
105168
Name [age]
106169
```
107170

108-
### Usage
171+
Usage:
109172

110173
```csharp
111-
public static async Task Print(string text)
174+
public static async Task PrintStructure(string sdl)
112175
{
113-
using var document = Parser.Parse(text);
114-
var writer = new StringWriter();
115-
var printer = new SDLPrinter()
176+
var document = Parser.Parse(sdl);
177+
using var writer = new StringWriter();
178+
var printer = new StructurePrinter()
116179
await printer.PrintAsync(document, writer);
117180
var rendered = writer.ToString();
118181
Console.WriteLine(rendered);

src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
1111
<PackageReference Include="Shouldly" Version="4.2.1" />
12-
<PackageReference Include="xunit" Version="2.5.2" />
12+
<PackageReference Include="xunit" Version="2.5.3" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1" />
1414
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
1515
</ItemGroup>

src/GraphQLParser.Tests/GraphQLParser.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</PackageReference>
2929
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
3030
<PackageReference Include="Shouldly" Version="4.2.1" />
31-
<PackageReference Include="xunit" Version="2.5.2" />
31+
<PackageReference Include="xunit" Version="2.5.3" />
3232
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
3333
</ItemGroup>
3434

0 commit comments

Comments
 (0)