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>
impl<'input> Parser<'input>
sourcepub fn new(input: &'input str) -> Self
pub fn new(input: &'input str) -> Self
Create a new instance of a parser given an input string.
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<Document>
pub fn parse(self) -> SyntaxTree<Document>
Parse the current tokens.
sourcepub fn parse_selection_set(self) -> SyntaxTree<SelectionSet>
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
.
sourcepub fn parse_type(self) -> SyntaxTree<Type>
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
.