Expand description
libgraphql-parser provides a lossless, error-tolerant, and
highly-optimized
GraphQL tokenizer and
GraphQL parser. Capable of parsing schema documents,
executable documents, and mixed schema + executable documents.
By default, libgraphql-parser targets the
September 2025 GraphQL Spec.
§Usage
§Parse valid documents
use libgraphql_parser::ast;
// Parse any GraphQL document
let parse_result = libgraphql_parser::parse(r#"
type User { firstName: String, lastName: String }
type Query { me: User }
query GetUserFullName {
me {
firstName,
lastName,
}
}
"#);
// Count and print the number of top-level definitions parsed out of the
// GraphQL document.
let ast: &ast::Document<'_> = parse_result.ast();
println!("Parsed {} GraphQL definitions.", ast.definitions.len());§Parse documents with errors
// Parse GraphQL documents with errors
let parse_result = libgraphql_parser::parse(r#"
type User { firstName String }
type Query { me: User }
"#);
// Access an "error recovered" version of the AST -- best-effort parsing.
let (recovered_ast, parse_errors, _) = parse_result.recovered().unwrap();
// Print nicely-formatted output for all parse errors
eprintln!(
"Found {} errors while parsing:\n{}",
parse_errors.len(),
parse_result.formatted_errors(),
);
println!(
"Found {} definitions after best-effort parse error recovery.",
recovered_ast.definitions.len(),
);This crate provides a unified token-based parser infrastructure with support for multiple token sources (string input, proc-macro input, etc.).
Modules§
- ast
- AST types for representing parsed GraphQL documents.
- compat
- smallvec
- token
- This module provides the core token types used by GraphQL lexers and the parser.
Structs§
- Byte
Span - A compact source span representing a half-open byte range
[start, end). - GraphQL
Error Note - An error note providing additional context about an error.
- GraphQL
Parse Error - A parse error with location information and contextual notes.
- GraphQL
Parser - A recursive descent parser for GraphQL documents.
- GraphQL
Parser Config - Configuration for
GraphQLParsercontrolling parser behavior. - GraphQL
Token Stream - Streaming lexer that produces
GraphQLTokens given someGraphQLTokenSourcewith a bounded lookahead buffer. - Source
Map - Provides utilities for mapping
ByteSpans toSourceSpans, byte offsets toSourcePositions, and extracting content from the original source text. - Source
Position - Source position information for parsing, with dual column tracking.
- Source
Span - Represents a span of source text from start to end position.
Enums§
- GraphQL
Error Note Kind - The kind of an error note (determines how the note is rendered).
- GraphQL
Parse Error Kind - Categorizes parse errors for programmatic handling.
- GraphQL
String Parsing Error - Error returned when parsing a GraphQL string value fails.
- Parse
Result - The result of a parsing operation.
- Reserved
Name Context - Contexts where certain names are reserved in GraphQL.
- Value
Parsing Error - Errors that occur when parsing literal values.
Functions§
- parse
- Parses a mixed document (both schema and executable definitions) from a string.
- parse_
executable - Parses an executable document (operations and fragments) from a string.
- parse_
schema - Parses a schema document from a string.