pub struct Parser { /* 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

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

Create a new resource limited instance of a parser given an input string and a recursion limit.

Parse the current tokens.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.