{"id":13429498,"url":"https://github.com/graphql-dotnet/parser","last_synced_at":"2026-02-02T10:28:05.824Z","repository":{"id":10243571,"uuid":"65084202","full_name":"graphql-dotnet/parser","owner":"graphql-dotnet","description":"A lexer and parser for GraphQL in .NET","archived":false,"fork":false,"pushed_at":"2024-11-15T11:34:19.000Z","size":3562,"stargazers_count":221,"open_issues_count":8,"forks_count":43,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-03T20:11:12.341Z","etag":null,"topics":["ast","graphql","lexer","parse"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphql-dotnet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-06T13:39:41.000Z","updated_at":"2025-03-27T20:20:52.000Z","dependencies_parsed_at":"2023-10-12T14:40:34.160Z","dependency_job_id":"940ad9af-ef7b-471f-b9bf-42343b11cd09","html_url":"https://github.com/graphql-dotnet/parser","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-dotnet%2Fparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-dotnet%2Fparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-dotnet%2Fparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-dotnet%2Fparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-dotnet","download_url":"https://codeload.github.com/graphql-dotnet/parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248608289,"owners_count":21132691,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","graphql","lexer","parse"],"created_at":"2024-07-31T02:00:40.716Z","updated_at":"2026-02-02T10:28:05.817Z","avatar_url":"https://github.com/graphql-dotnet.png","language":"C#","readme":"# GraphQL.NET Parser\n\n[![License](https://img.shields.io/github/license/graphql-dotnet/parser)](LICENSE.md)\n[![codecov](https://codecov.io/gh/graphql-dotnet/parser/branch/master/graph/badge.svg?token=GEjwg1by60)](https://codecov.io/gh/graphql-dotnet/parser)\n[![Nuget](https://img.shields.io/nuget/dt/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)\n[![Nuget](https://img.shields.io/nuget/v/GraphQL-Parser)](https://www.nuget.org/packages/GraphQL-Parser)\n[![GitHub Release Date](https://img.shields.io/github/release-date/graphql-dotnet/parser?label=released)](https://github.com/graphql-dotnet/parser/releases)\n[![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)\n[![GitHub contributors](https://img.shields.io/github/contributors/graphql-dotnet/parser)](https://github.com/graphql-dotnet/parser/graphs/contributors)\n![Size](https://img.shields.io/github/repo-size/graphql-dotnet/parser)\n\nThis library contains a lexer and parser as well as the complete [GraphQL AST model](http://spec.graphql.org/October2021/#sec-Appendix-Grammar-Summary)\nthat allows you to work with GraphQL documents compatible with the [October 2021 spec](https://spec.graphql.org/October2021/).\n\nThe parser from this library is used by the [GraphQL.NET](https://github.com/graphql-dotnet/graphql-dotnet) project\nand was [verified](https://codecov.io/gh/graphql-dotnet/parser) by many test data sets.\n\nPreview versions of this package are available on [GitHub Packages](https://github.com/orgs/graphql-dotnet/packages?repo_name=parser).\n\n## 1. Lexer\n\nGenerates token based on input text. Lexer takes advantage of `ReadOnlyMemory\u003cchar\u003e` and in most cases\ndoes not allocate memory on the managed heap at all.\n\nUsage:\n\n```csharp\nvar token = Lexer.Lex(\"\\\"str\\\"\");\n```\n\nLex method always returns the first token it finds. In this case the result would look like following.\n![lexer example](assets/lexer-example.png)\n\n## 2. Parser\n\nParses provided GraphQL expression into AST (abstract syntax tree). Parser also takes advantage of\n`ReadOnlyMemory\u003cchar\u003e` but still allocates memory for AST.\n\nUsage:\n\n```csharp\nvar ast1 = Parser.Parse(@\"\n{\n  field\n}\");\n\nvar ast2 = Parser.Parse(@\"\n{\n  field\n}\", new ParserOptions { Ignore = IgnoreOptions.Comments });\n```\n\nBy default `ParserOptions.Ignore` is `IgnoreOptions.None`. If you want\nto ignore all comments use `IgnoreOptions.Comments`. If you don't need\ninformation about tokens locations in the source document, then use flag\n`IgnoreOptions.Locations`. Or just use `IgnoreOptions.All` and this\nwill maximize the saving of memory allocated in the managed heap for AST.\n\nYou can parse not only entire `GraphQLDocument` but also concrete AST\nnodes. Use generic overload.\n\n```csharp\nstring text1 = \"enum Color { RED }\"\nvar ast1 = Parser.Parse\u003cGraphQLEnumTypeDefinition\u003e(text1);\n\nstring text2 = \"{ a: 1, b: \\\"abc\\\", c: RED, d: $id }\";\nvar ast2 = Parser.Parse\u003cGraphQLValue\u003e(text2); // returns GraphQLObjectValue\n```\n\n## 3. ASTVisitor\n\n`ASTVisitor` provides API to traverse AST of the parsed GraphQL document.\nDefault implementation traverses all AST nodes of the provided one. You can\ninherit from it and override desired methods to implement your own AST\nprocessing algorithm.\n\n### SDLPrinter\n\nFor printing SDL from AST, you can use `SDLPrinter`. This is a highly\noptimized visitor for asynchronous non-blocking SDL output into provided\n`TextWriter`. In the majority of cases it does not allocate memory in\nthe managed heap at all. Extension methods are also provided for printing\ndirectly to a string, which utilize the `StringBuilder` and `StringWriter`\nclasses.\n\n```csharp\nvar document = Parser.Parse(\"query { hero { name age } }\");\n\n// print to a string with default options\nvar sdl = new SDLPrinter().Print(document);\n\n// print to a string builder\nvar sb = new StringBuilder();\nnew SDLPrinter().Print(document, sb);\n\n// print to a string with some options\nvar sdlPrinter = new SDLPrinter(\n    new SDLPrinterOptions\n    {\n        PrintComments = true,\n        EachDirectiveLocationOnNewLine = true,\n        EachUnionMemberOnNewLine = true,\n    });\nvar sdl = sdlPrinter.Print(document);\n\n// print to a stream asynchronously\nusing var writer = new StreamWriter(stream);\nawait sdlPrinter.PrintAsync(document, writer, default);\nawait writer.FlushAsync();\n```\n\nOutput:\n\n```graphql\nquery {\n  hero {\n    name\n    age\n  }\n}\n```\n\n### SDLSorter\n\nAn AST document can be sorted with the `SDLSorter` using a predefined\nsort order. You can specify the string comparison; by default it uses\na culture-invariant case-insensitive comparison. Any futher customization\nis possible by deriving from `SDLSorterOptions` and overriding the `Compare`\nmethods.\n\n```csharp\nvar document = Parser.Parse(\"query { hero { name age } }\");\nSDLSorter.Sort(document);\nvar sdl = new SDLPrinter().Print(document);\n```\n\nOutput:\n\n```graphql\nquery {\n  hero {\n    age\n    name\n  }\n}\n```\n\n### StructurePrinter\n\nYou can also find a `StructurePrinter` visitor that prints AST into the\nprovided `TextWriter` as a hierarchy of node types. It can be useful\nwhen debugging for better understanding the AST structure.\nConsider the following GraphQL document:\n\n```graphql\nquery a { name age }\n```\n\nAfter `StructurePrinter` processing the output text will be\n\n```\nDocument\n  OperationDefinition\n    Name [a]\n    SelectionSet\n      Field\n        Name [name]\n      Field\n        Name [age]\n```\n\nUsage:\n\n```csharp\npublic static async Task PrintStructure(string sdl)\n{\n    var document = Parser.Parse(sdl);\n    using var writer = new StringWriter(); \n    var printer = new StructurePrinter()\n    await printer.PrintAsync(document, writer);\n    var rendered = writer.ToString();\n    Console.WriteLine(rendered);\n}\n```\n\n## Contributors\n\nThis project exists thanks to all the people who contribute. \n\u003ca href=\"https://github.com/graphql-dotnet/parser/graphs/contributors\"\u003e\u003cimg src=\"https://contributors-img.web.app/image?repo=graphql-dotnet/parser\" /\u003e\u003c/a\u003e\n\nPRs are welcome! Looking for something to work on? The list of [open issues](https://github.com/graphql-dotnet/parser/issues)\nis a great place to start. You can help the project by simply responding to some of the [asked questions](https://github.com/graphql-dotnet/parser/issues?q=is%3Aissue+is%3Aopen+label%3Aquestion).\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","API"],"sub_categories":["API"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-dotnet%2Fparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-dotnet%2Fparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-dotnet%2Fparser/lists"}