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>
impl<'a> Parser<'a>
sourcepub fn recursion_limit(self, recursion_limit: usize) -> Self
pub fn recursion_limit(self, recursion_limit: usize) -> Self
Configure the recursion limit to use while parsing.
sourcepub fn token_limit(self, token_limit: usize) -> Self
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.
sourcepub fn parse(self) -> SyntaxTree
pub fn parse(self) -> SyntaxTree
Parse the current tokens.