GithubHelp home page GithubHelp logo

parser's Introduction

GraphQL.NET Parser

Publish release to Nuget registry Publish preview to GitHub registry

Run unit tests CodeQL analysis codecov

NuGet Nuget

Activity Activity Activity

Size

This library contains a lexer and parser as well as the complete GraphQL AST model that allows you to work with GraphQL documents compatible with the October 2021 spec.

The parser from this library is used by the GraphQL.NET project and was verified by many test data sets.

Preview versions of this package are available on GitHub Packages.

1. 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. lexer example

2. 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.Comments });

By default ParserOptions.Ignore is IgnoreOptions.None. If you want to ignore all comments use IgnoreOptions.Comments. If you don't need information about tokens locations in the source document, then use flag IgnoreOptions.Locations. Or just use IgnoreOptions.All and this will maximize the saving of memory allocated in the managed heap for AST.

3. ASTVisitor

ASTVisitor provides API to traverse AST of the parsed GraphQL document. Default implementation traverses all AST nodes of the provided one. You can inherit from it and override desired methods to implement your own AST processing algorithm.

For printing SDL from AST, you can use SDLPrinter. This is a highly optimized visitor for asynchronous non-blocking SDL output into provided TextWriter. In the majority of cases it does not allocate memory in the managed heap at all.

You can also find a StructurePrinter visitor that prints AST into the provided TextWriter as a hierarchy of node types. It can be useful when debugging for better understanding the AST structure. Consider GraphQL document

query a { name age }

After StructurePrinter processing the output text will be

Document
  OperationDefinition
    Name [a]
    SelectionSet
      Field
        Name [name]
      Field
        Name [age]

Usage

public static async Task Print(string text)
{
    using var document = Parser.Parse(text);
    var writer = new StringWriter(); 
    var printer = new SDLPrinter()
    await printer.PrintAsync(document, writer);
    var rendered = writer.ToString();
    Console.WriteLine(rendered);
}

parser's People

Contributors

anderslaub avatar andyban avatar dependabot[bot] avatar ificator avatar joemcbride avatar mkmarek avatar randyridge avatar shane32 avatar sungam3r avatar vincent-duret avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.