Skip to main content

Crate libgraphql_parser

Crate libgraphql_parser 

Source
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§

ByteSpan
A compact source span representing a half-open byte range [start, end).
GraphQLErrorNote
An error note providing additional context about an error.
GraphQLParseError
A parse error with location information and contextual notes.
GraphQLParser
A recursive descent parser for GraphQL documents.
GraphQLParserConfig
Configuration for GraphQLParser controlling parser behavior.
GraphQLTokenStream
Streaming lexer that produces GraphQLTokens given some GraphQLTokenSource with a bounded lookahead buffer.
SourceMap
Provides utilities for mapping ByteSpans to SourceSpans, byte offsets to SourcePositions, and extracting content from the original source text.
SourcePosition
Source position information for parsing, with dual column tracking.
SourceSpan
Represents a span of source text from start to end position.

Enums§

GraphQLErrorNoteKind
The kind of an error note (determines how the note is rendered).
GraphQLParseErrorKind
Categorizes parse errors for programmatic handling.
GraphQLStringParsingError
Error returned when parsing a GraphQL string value fails.
ParseResult
The result of a parsing operation.
ReservedNameContext
Contexts where certain names are reserved in GraphQL.
ValueParsingError
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.