pub struct GraphQLParser<'src, TTokenSource: GraphQLTokenSource<'src> = StrGraphQLTokenSource<'src>> { /* private fields */ }Expand description
A recursive descent parser for GraphQL documents.
GraphQLParser is generic over the token source
where StrGraphQLTokenSource provides for &str common-case.
Parameterization of the token source allows GraphQLParser to parse types
of inputs other than just &str; For example,
libgraphql-macros
implements a
RustMacroGraphQLTokenSource
in order to provide the
graphql_schema! {}
macro.
§Usage
use libgraphql_parser::ast;
use libgraphql_parser::GraphQLParser;
let parser = GraphQLParser::new("type Query { hello: String }");
let parse_result = parser.parse_schema_document();
assert!(!parse_result.has_errors());
if let Some((doc, source_map)) = parse_result.into_valid() {
// The first definition in the document is an `ast::TypeDefinition`
let first_def = &doc.definitions[0];
assert!(matches!(first_def, ast::Definition::TypeDefinition(_)));
// The first definition's `name` is "Query"
assert!(matches!(first_def.name_value(), Some("Query")));
// Extract the starting line/col of the `Query` type definition
let source_position = first_def.source_span(&source_map).unwrap();
let (start_line, start_col) = source_position.start_inclusive.line_col();
}Implementations§
Source§impl<'src> GraphQLParser<'src, StrGraphQLTokenSource<'src>>
impl<'src> GraphQLParser<'src, StrGraphQLTokenSource<'src>>
Sourcepub fn new<S: AsRef<str> + ?Sized>(source: &'src S) -> Self
pub fn new<S: AsRef<str> + ?Sized>(source: &'src S) -> Self
Creates a new parser from a string-like source.
Accepts any type that can be referenced as a str,
including &str, &String, and &Cow<str>.
§Example
use libgraphql_parser::GraphQLParser;
let source = "type Query { hello: String }";
let parser = GraphQLParser::new(source);
let result = parser.parse_schema_document();
assert!(!result.has_errors());Sourcepub fn with_config<S: AsRef<str> + ?Sized>(
source: &'src S,
config: GraphQLParserConfig,
) -> Self
pub fn with_config<S: AsRef<str> + ?Sized>( source: &'src S, config: GraphQLParserConfig, ) -> Self
Creates a new parser from a string-like source with the given configuration.
§Example
use libgraphql_parser::GraphQLParser;
use libgraphql_parser::GraphQLParserConfig;
let source = "type Query { hello: String }";
let config = GraphQLParserConfig::lean();
let parser = GraphQLParser::with_config(source, config);
let result = parser.parse_schema_document();
assert!(!result.has_errors());Source§impl<'src, TTokenSource: GraphQLTokenSource<'src>> GraphQLParser<'src, TTokenSource>
impl<'src, TTokenSource: GraphQLTokenSource<'src>> GraphQLParser<'src, TTokenSource>
Sourcepub fn from_token_source(token_source: TTokenSource) -> Self
pub fn from_token_source(token_source: TTokenSource) -> Self
Creates a new parser from a token source with the default configuration.
Sourcepub fn from_token_source_with_config(
token_source: TTokenSource,
config: GraphQLParserConfig,
) -> Self
pub fn from_token_source_with_config( token_source: TTokenSource, config: GraphQLParserConfig, ) -> Self
Creates a new parser from a token source with the given configuration.
Sourcepub fn parse_schema_document(self) -> ParseResult<'src, Document<'src>>
pub fn parse_schema_document(self) -> ParseResult<'src, Document<'src>>
Parses a schema document (type system definitions only).
Sourcepub fn parse_executable_document(self) -> ParseResult<'src, Document<'src>>
pub fn parse_executable_document(self) -> ParseResult<'src, Document<'src>>
Parses an executable document (operations and fragments only).
Sourcepub fn parse_mixed_document(self) -> ParseResult<'src, Document<'src>>
pub fn parse_mixed_document(self) -> ParseResult<'src, Document<'src>>
Parses a mixed document (both type system and executable definitions).