graphql_parser2 8.0.0
graphql_parser2: ^8.0.0 copied to clipboard
Parses GraphQL queries and schemas. Also includes classes for the GraphQL AST.
Graphql Parser 2 #
Parses GraphQL documents: queries, mutations, subscriptions and fragments, following the GraphQL specification (June 2018).
This library is only a parser: it turns text into an AST (abstract syntax tree). Executing queries is done by package:graphql_server2, and Angel3 users should consider package:angel3_graphql, the simplest way to add GraphQL to a server.
Installation #
Add graphql_parser2 as a dependency in your pubspec.yaml file:
dependencies:
graphql_parser2: ^8.0.0
Usage #
scan turns text into tokens, and a Parser reads a DocumentContext from them by recursive descent. scan throws a SyntaxError on text it cannot read, such as an unterminated string; the parser collects its errors in parser.errors, so check them after parsing:
import 'package:graphql_parser2/graphql_parser2.dart';
void main() {
const text = r'query Todos($done: Boolean = false) { todos(done: $done) { text } }';
try {
var parser = Parser(scan(text));
var document = parser.parseDocument();
if (parser.errors.isNotEmpty) {
for (var error in parser.errors) {
print('${error.message} at ${error.span?.start.toolString}');
}
return;
}
var query = document.definitions.single as OperationDefinitionContext;
print(query.name); // Todos
for (var selection in query.selectionSet.selections) {
print(selection.field?.fieldName.name); // todos
}
} on SyntaxError catch (e) {
print(e);
}
}
Each AST node has a span, its location in the source, for error messages. A value node's computeValue(variables) gives its Dart value, with variables taken from the map given:
- A number with a fractional part or an exponent, such as
1.5or1e3, is adouble; any other number is anint, or adoubleif it is too large for one. - A block string (
"""...""") has its common indentation and leading and trailing blank lines removed, as the specification describes.
The parser follows the specification, and rejects what it forbids, for example:
- integers with leading zeros, such as
007; - a line break in a
"..."string; - a variable in a default value, such as
$a: Int = $b; - a fragment named
on.
See the change log for what changed in 8.0.0.