Skip to main content

libgraphql_parser/
lib.rs

1//! `libgraphql-parser` provides a lossless, error-tolerant, and
2//! highly-optimized
3//! [GraphQL tokenizer](crate::token::StrGraphQLTokenSource) and
4//! [GraphQL parser](GraphQLParser). Capable of parsing schema documents,
5//! executable documents, and mixed schema + executable documents.
6//!
7//! By default, `libgraphql-parser` targets the
8//! [September 2025 GraphQL Spec](https://spec.graphql.org/September2025/).
9//!
10//! ## Usage
11//!
12//! ##### Parse valid documents
13//!
14//! ```rust
15//! # use libgraphql_parser;
16//! # use libgraphql_parser::GraphQLParser;
17//! # use libgraphql_parser::ParseResult;
18//! use libgraphql_parser::ast;
19//!
20//! // Parse any GraphQL document
21//! let parse_result = libgraphql_parser::parse(r#"
22//!   type User { firstName: String, lastName: String }
23//!   type Query { me: User }
24//!
25//!   query GetUserFullName {
26//!     me {
27//!       firstName,
28//!       lastName,
29//!     }
30//!   }
31//! "#);
32//! # assert!(parse_result.valid().is_some());
33//!
34//! // Count and print the number of top-level definitions parsed out of the
35//! // GraphQL document.
36//! let ast: &ast::Document<'_> = parse_result.ast();
37//! println!("Parsed {} GraphQL definitions.", ast.definitions.len());
38//! ```
39//!
40//! ##### Parse documents with errors
41//!
42//! ```rust
43//! # use libgraphql_parser;
44//! // Parse GraphQL documents with errors
45//! let parse_result = libgraphql_parser::parse(r#"
46//!   type User { firstName String }
47//!   type Query { me: User }
48//! "#);
49//! # assert!(!parse_result.errors().is_empty(), "Expected a 'missing : token' error");
50//!
51//! // Access an "error recovered" version of the AST -- best-effort parsing.
52//! let (recovered_ast, parse_errors, _) = parse_result.recovered().unwrap();
53//! # assert_eq!(recovered_ast.definitions.len(), 1, "Expected 1 recovered definition");
54//! # assert_eq!(parse_errors.len(), 1, "Expected 1 parse error");
55//! // Print nicely-formatted output for all parse errors
56//! eprintln!(
57//!   "Found {} errors while parsing:\n{}",
58//!   parse_errors.len(),
59//!   parse_result.formatted_errors(),
60//! );
61//!
62//! println!(
63//!   "Found {} definitions after best-effort parse error recovery.",
64//!   recovered_ast.definitions.len(),
65//! );
66//! ```
67//!
68//! This crate provides a unified token-based parser infrastructure with support for multiple token
69//! sources (string input, proc-macro input, etc.).
70
71pub mod ast;
72mod byte_span;
73pub mod compat;
74mod graphql_error_note;
75mod graphql_error_note_kind;
76mod graphql_parse_error;
77mod graphql_parse_error_kind;
78mod graphql_parser;
79mod graphql_parser_config;
80mod source_span;
81mod graphql_string_parsing_error;
82mod graphql_token_stream;
83mod parse_result;
84mod reserved_name_context;
85pub mod smallvec;
86mod source_map;
87mod source_position;
88pub mod token;
89mod value_parsing_error;
90
91pub use byte_span::ByteSpan;
92pub use graphql_error_note::GraphQLErrorNote;
93pub use graphql_error_note_kind::GraphQLErrorNoteKind;
94pub use graphql_parse_error::GraphQLParseError;
95pub use graphql_parse_error_kind::GraphQLParseErrorKind;
96pub use graphql_parser::GraphQLParser;
97pub use graphql_parser_config::GraphQLParserConfig;
98pub use source_span::SourceSpan;
99pub use graphql_string_parsing_error::GraphQLStringParsingError;
100pub use graphql_token_stream::GraphQLTokenStream;
101pub use parse_result::ParseResult;
102pub use reserved_name_context::ReservedNameContext;
103pub use source_map::SourceMap;
104pub use source_position::SourcePosition;
105pub use value_parsing_error::ValueParsingError;
106
107/// Parses a schema document from a string.
108pub fn parse_schema(
109    source: &str,
110) -> ParseResult<'_, ast::Document<'_>> {
111    let parser = GraphQLParser::new(source);
112    parser.parse_schema_document()
113}
114
115/// Parses an executable document (operations and
116/// fragments) from a string.
117pub fn parse_executable(
118    source: &str,
119) -> ParseResult<'_, ast::Document<'_>> {
120    let parser = GraphQLParser::new(source);
121    parser.parse_executable_document()
122}
123
124/// Parses a mixed document (both schema and executable
125/// definitions) from a string.
126pub fn parse(
127    source: &str,
128) -> ParseResult<'_, ast::Document<'_>> {
129    let parser = GraphQLParser::new(source);
130    parser.parse_mixed_document()
131}
132
133#[cfg(test)]
134mod tests;
135
136#[cfg(doctest)]
137#[doc = include_str!("../README.md")]
138struct _ReadmeDocTests;