Struct apollo_parser::Parser

source ·
pub struct Parser<'a> { /* private fields */ }
Expand description

Parse GraphQL schemas or queries into a typed AST.

Example

The API to parse a query or a schema is the same, as the parser currently accepts a &str. Here is an example of parsing a query:

use apollo_parser::Parser;

let query = "
{
    animal
    ...snackSelection
    ... on Pet {
      playmates {
        count
      }
    }
}
";
// Create a new instance of a parser given a query above.
let parser = Parser::new(query);
// Parse the query, and return a SyntaxTree.
let ast = parser.parse();
// Check that are no errors. These are not part of the AST.
assert_eq!(0, ast.errors().len());

// Get the document root node
let doc = ast.document();
// ... continue

Here is how you’d parse a schema:

use apollo_parser::Parser;
let core_schema = r#"
schema @core(feature: "https://specs.apollo.dev/join/v0.1") {
  query: Query
  mutation: Mutation
}

enum join__Graph {
  ACCOUNTS @join__graph(name: "accounts")
}
"#;
let parser = Parser::new(core_schema);
let ast = parser.parse();

assert_eq!(0, ast.errors().len());

let document = ast.document();

Implementations§

source§

impl<'a> Parser<'a>

source

pub fn new(input: &'a str) -> Self

Create a new instance of a parser given an input string.

source

pub fn recursion_limit(self, recursion_limit: usize) -> Self

Configure the recursion limit to use while parsing.

source

pub fn token_limit(self, token_limit: usize) -> Self

Configure the limit on the number of tokens to parse. If an input document is too big, parsing will be aborted.

By default, there is no limit.

source

pub fn parse(self) -> SyntaxTree

Parse the current tokens.

Trait Implementations§

source§

impl<'a> Debug for Parser<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Parser<'a>

§

impl<'a> !Send for Parser<'a>

§

impl<'a> !Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> !UnwindSafe for Parser<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.