Struct apollo_parser::Parser

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

Parse GraphQL schemas or queries into a typed CST.

§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 cst = parser.parse();
// Check that are no errors. These are not part of the CST.
assert_eq!(0, cst.errors().len());

// Get the document root node
let doc = cst.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 cst = parser.parse();

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

let document = cst.document();

Implementations§

source§

impl<'input> Parser<'input>

source

pub fn new(input: &'input 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<Document>

Parse the current tokens.

source

pub fn parse_selection_set(self) -> SyntaxTree<SelectionSet>

Parse a selection set with optional outer braces. This is the expected format of the string value of the fields argument of some directives like @requires.

source

pub fn parse_type(self) -> SyntaxTree<Type>

Parse a GraphQL type. This is the expected format of the string value of the type argument of some directives like @field.

Trait Implementations§

source§

impl<'input> Debug for Parser<'input>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'input> Freeze for Parser<'input>

§

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

§

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

§

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

§

impl<'input> Unpin for Parser<'input>

§

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

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

§

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

§

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.