Skip to content

Commit 65a05bf

Browse files
authored
Update readme (#367)
* Update readme * Update
1 parent 39e67be commit 65a05bf

File tree

1 file changed

+79
-19
lines changed

1 file changed

+79
-19
lines changed

README.md

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
# GraphQL.NET Parser
22

3-
[![Publish release to Nuget registry](https://github.com/graphql-dotnet/parser/actions/workflows/publish-release.yml/badge.svg)](https://github.com/graphql-dotnet/parser/actions/workflows/publish-release.yml)
4-
[![Publish preview to GitHub registry](https://github.com/graphql-dotnet/parser/actions/workflows/publish-preview.yml/badge.svg)](https://github.com/graphql-dotnet/parser/actions/workflows/publish-preview.yml)
5-
6-
[![Run unit tests](https://github.com/graphql-dotnet/parser/actions/workflows/test.yml/badge.svg)](https://github.com/graphql-dotnet/parser/actions/workflows/test.yml)
7-
[![CodeQL analysis](https://github.com/graphql-dotnet/parser/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/graphql-dotnet/parser/actions/workflows/codeql-analysis.yml)
3+
[![License](https://img.shields.io/github/license/graphql-dotnet/parser)](LICENSE.md)
84
[![codecov](https://codecov.io/gh/graphql-dotnet/parser/branch/master/graph/badge.svg?token=GEjwg1by60)](https://codecov.io/gh/graphql-dotnet/parser)
9-
10-
[![NuGet](https://img.shields.io/nuget/v/GraphQL-Parser.svg)](https://www.nuget.org/packages/GraphQL-Parser)
115
[![Nuget](https://img.shields.io/nuget/dt/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)
12-
13-
![Activity](https://img.shields.io/github/commit-activity/w/graphql-dotnet/parser)
14-
![Activity](https://img.shields.io/github/commit-activity/m/graphql-dotnet/parser)
15-
![Activity](https://img.shields.io/github/commit-activity/y/graphql-dotnet/parser)
16-
6+
[![Nuget](https://img.shields.io/nuget/v/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)
7+
[![GitHub Release Date](https://img.shields.io/github/release-date/graphql-dotnet/parser?label=released)](https://github.com/graphql-dotnet/parser/releases)
8+
[![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+
[![GitHub contributors](https://img.shields.io/github/contributors/graphql-dotnet/parser)](https://github.com/graphql-dotnet/parser/graphs/contributors)
1710
![Size](https://img.shields.io/github/repo-size/graphql-dotnet/parser)
1811

1912
This library contains a lexer and parser as well as the complete [GraphQL AST model](http://spec.graphql.org/October2021/#sec-Appendix-Grammar-Summary)
@@ -70,15 +63,82 @@ Default implementation traverses all AST nodes of the provided one. You can
7063
inherit from it and override desired methods to implement your own AST
7164
processing algorithm.
7265

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

78138
You can also find a `StructurePrinter` visitor that prints AST into the
79139
provided `TextWriter` as a hierarchy of node types. It can be useful
80140
when debugging for better understanding the AST structure.
81-
Consider GraphQL document
141+
Consider the following GraphQL document:
82142

83143
```graphql
84144
query a { name age }
@@ -97,14 +157,14 @@ Document
97157
Name [age]
98158
```
99159

100-
### Usage
160+
Usage:
101161

102162
```csharp
103-
public static async Task Print(string text)
163+
public static async Task PrintStructure(string sdl)
104164
{
105-
using var document = Parser.Parse(text);
106-
var writer = new StringWriter();
107-
var printer = new SDLPrinter()
165+
var document = Parser.Parse(sdl);
166+
using var writer = new StringWriter();
167+
var printer = new StructurePrinter()
108168
await printer.PrintAsync(document, writer);
109169
var rendered = writer.ToString();
110170
Console.WriteLine(rendered);

0 commit comments

Comments
 (0)