Skip to main content

GraphQLParser

Struct GraphQLParser 

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

Source

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());
Source

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>

Source

pub fn from_token_source(token_source: TTokenSource) -> Self

Creates a new parser from a token source with the default configuration.

Source

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.

Source

pub fn parse_schema_document(self) -> ParseResult<'src, Document<'src>>

Parses a schema document (type system definitions only).

Source

pub fn parse_executable_document(self) -> ParseResult<'src, Document<'src>>

Parses an executable document (operations and fragments only).

Source

pub fn parse_mixed_document(self) -> ParseResult<'src, Document<'src>>

Parses a mixed document (both type system and executable definitions).

Auto Trait Implementations§

§

impl<'src, TTokenSource> Freeze for GraphQLParser<'src, TTokenSource>
where TTokenSource: Freeze,

§

impl<'src, TTokenSource> RefUnwindSafe for GraphQLParser<'src, TTokenSource>
where TTokenSource: RefUnwindSafe,

§

impl<'src, TTokenSource> Send for GraphQLParser<'src, TTokenSource>
where TTokenSource: Send,

§

impl<'src, TTokenSource> Sync for GraphQLParser<'src, TTokenSource>
where TTokenSource: Sync,

§

impl<'src, TTokenSource> Unpin for GraphQLParser<'src, TTokenSource>
where TTokenSource: Unpin,

§

impl<'src, TTokenSource> UnsafeUnpin for GraphQLParser<'src, TTokenSource>
where TTokenSource: UnsafeUnpin,

§

impl<'src, TTokenSource> UnwindSafe for GraphQLParser<'src, TTokenSource>
where TTokenSource: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.