graphql_parser3 3.2.1 copy "graphql_parser3: ^3.2.1" to clipboard
graphql_parser3: ^3.2.1 copied to clipboard

Parses GraphQL queries and schemas into an AST whose every node carries a source span. The lexer and the recursive-descent parser behind graphql_server3.

example/main.dart

import 'package:graphql_parser3/graphql_parser3.dart';

/// Parses a query and walks the selections of its single operation.
void main() {
  const query = r'''
query Profile($id: ID!) {
  user(id: $id) {
    name
    ...contact
  }
}

fragment contact on User {
  email
}
''';

  final document = Parser(
    scan(query, sourceUrl: 'profile.graphql'),
  ).parseDocument();

  final operation = document.definitions
      .whereType<OperationDefinitionContext>()
      .single;

  print('operation: ${operation.name}'); // Profile
  print('is a query: ${operation.isQuery}'); // true

  for (final selection in operation.selectionSet.selections) {
    final field = selection.field;
    if (field == null) continue;

    // Every node carries the span it was read from, so an error can point at
    // the source rather than describe it.
    print('${field.fieldName.name} at line ${field.span?.start.line}');
  }

  final fragment = document.definitions
      .whereType<FragmentDefinitionContext>()
      .single;

  print('fragment ${fragment.name} on ${fragment.typeCondition.typeName.name}');
}
0
likes
160
points
134
downloads

Documentation

API reference

Publisher

verified publishercomapps.be

Weekly Downloads

Parses GraphQL queries and schemas into an AST whose every node carries a source span. The lexer and the recursive-descent parser behind graphql_server3.

Repository (GitHub)
View/report issues

Topics

#graphql #parser #ast

License

BSD-3-Clause (license)

Dependencies

source_span, string_scanner

More

Packages that depend on graphql_parser3