oxc-graphql-parser
A spec-compliant, error-resilient GraphQL lexer and parser for Rust.
Features
- Typed GraphQL concrete syntax tree based on the October 2021 specification
- Error-resilient lexing and parsing
- GraphQL schema and query parsing
- Standalone lexer API
Installation
Or add it manually:
[]
= "0.0.1"
The Cargo package name uses hyphens. Import it from Rust as oxc_graphql_parser.
Usage
use Parser;
let input = "union SearchResult = Photo | Person | Cat | Dog";
let parser = new;
let cst = parser.parse;
assert_eq!;
Parser::parse always returns a concrete syntax tree, even when lexing or
parsing reports errors. Check cst.errors() before walking the document:
use Parser;
let input = "union SearchResult = Photo | Person | Cat | Dog";
let parser = new;
let cst = parser.parse;
assert_eq!;
let document = cst.document;
for definition in document.definitions
Examples
The examples directory contains integrations for diagnostics and analysis:
- using oxc-graphql-parser with ariadne to display error diagnostics
- using oxc-graphql-parser with annotate_snippets to display error diagnostics
- checking for unused variables
Get Field Names In An Object
use ;
let input = "
type ProductDimension {
size: String
weight: Float @tag(name: \"hi from inventory value type field\")
}
";
let parser = new;
let cst = parser.parse;
assert_eq!;
let document = cst.document;
for definition in document.definitions
Get Variables Used In A Query
use ;
let input = "
query GraphQuery($graph_id: ID!, $variant: String) {
service(id: $graph_id) {
schema(tag: $variant) {
document
}
}
}
";
let parser = new;
let cst = parser.parse;
assert_eq!;
let document = cst.document;
for definition in document.definitions
Rust Versions
oxc-graphql-parser is tested on the latest stable version of Rust.
Older versions may or may not be compatible.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.