graphql-dotnet / Parser
Projects that are alternatives of or similar to Parser
GraphQL Dotnet Parser
This library contains a lexer and parser classes as well as the complete GraphQL AST model.
The parser from this library is used in GraphQL for .NET.
Preview versions of this package are available on GitHub Packages.
Lexer
Generates token based on input text. Lexer takes advantage of ReadOnlyMemory<char>
and in most cases
does not allocate memory on the managed heap at all.
Usage
var token = Lexer.Lex("\"str\"");
Lex method always returns the first token it finds. In this case case the result would look like following.
Parser
Parses provided GraphQL expression into AST (abstract syntax tree). Parser also takes advantage of
ReadOnlyMemory<char>
but still allocates memory for AST.
Usage
var ast1 = Parser.Parse(@"
{
field
}");
var ast2 = Parser.Parse(@"
{
field
}", new ParserOptions { Ignore = IgnoreOptions.IgnoreComments });
By default ParserOptions.Ignore
is IgnoreOptions.IgnoreComments
to improve performance.
If you don't need information about tokens locations in the source document, then use IgnoreOptions.IgnoreCommentsAndLocations
.
This will maximize the saving of memory allocated in the managed heap for AST.
Example of json representation of the resulting AST
{
"Definitions": [{
"Directives": [],
"Kind": 2,
"Name": null,
"Operation": 0,
"SelectionSet": {
"Kind": 5,
"Selections": [{
"Alias": null,
"Arguments": [],
"Directives": [],
"Kind": 6,
"Name": {
"Kind": 0,
"Value": "field",
"Location": {
"End": 50,
"Start": 31
}
},
"SelectionSet": null,
"Location": {
"End": 50,
"Start": 31
}
}],
"Location": {
"End": 50,
"Start": 13
}
},
"VariableDefinitions": null,
"Location": {
"End": 50,
"Start": 13
}
}],
"Kind": 1,
"Location": {
"End": 50,
"Start": 13
}
}