gql 0.1.0

A package for working with GraphQL documents.

⚠️ This package is under development ⚠️

Usage #

This package exports two libraries: language.dart for working with GraphQL source and ast.dart for working with GraphQL AST nodes.

import "package:gql/language.dart" as lang;
import "package:gql/ast.dart" as ast;

Parsing GraphQL documents #

import "package:gql/language.dart" as lang;
import "package:gql/ast.dart" as ast;
import "package:source_span/source_span.dart";

void main() {
  final ast.DocumentNode doc = lang.parse(
    SourceFile.fromString(
      """
        query UserInfo(\$id: ID!) {
          user(id: \$id) {
            id
            name
          }
        }
      """,
    ),
  );

  print(
    (doc.definitions.first as ast.OperationDefinitionNode).name.value,
  );
}

Printing GraphQL AST to string #

import "package:gql/language.dart" as lang;
import "package:gql/ast.dart" as ast;

void main() {
  print(
    lang.printNode(
      ast.SchemaDefinitionNode(
        operationTypes: [
          ast.OperationTypeDefinitionNode(
            operation: ast.OperationType.query,
            type: ast.NamedTypeNode(
              name: ast.NameNode(value: "MyQuery"),
              isNonNull: false,
            ),
          )
        ],
      ),
    ),
  );
}

Visiting GraphQL AST nodes #

import "package:gql/language.dart" as lang;
import "package:gql/ast.dart" as ast;
import "package:source_span/source_span.dart";

class TypeVisitor extends ast.RecursiveVisitor {
  Iterable<ast.ObjectTypeDefinitionNode> types = [];

  @override
  visitObjectTypeDefinitionNode(
    ast.ObjectTypeDefinitionNode node,
  ) {
    types = types.followedBy([node]);
    super.visitObjectTypeDefinitionNode(node);
  }
}

void main() {
  final ast.DocumentNode doc = lang.parse(
    SourceFile.fromString(
      """
        type A { id: ID! }
        type B { id: ID! }
        type C { id: ID! }
        type D { id: ID! }
        type E { id: ID! }
      """,
    ),
  );

  final TypeVisitor v = TypeVisitor();

  doc.accept(v);

  print(
    v.types
        .map(
          (t) => t.name.value,
        )
        .join("\n"),
  );
}

Transforming GraphQL documents #

import "package:gql/language.dart" as lang;
import "package:gql/ast.dart" as ast;
import "package:source_span/source_span.dart";

class AddTypenames extends ast.TransformingVisitor {
  @override
  visitFieldNode(ast.FieldNode node) {
    if (node.selectionSet == null) {
      return node;
    }

    return ast.FieldNode(
      name: node.name,
      alias: node.alias,
      arguments: node.arguments,
      directives: node.directives,
      selectionSet: ast.SelectionSetNode(
        selections: <ast.SelectionNode>[
          ast.FieldNode(
            name: ast.NameNode(value: "__typename"),
          ),
        ].followedBy(node.selectionSet.selections),
      ),
    );
  }
}

void main() {
  final ast.DocumentNode doc = lang.parse(
    SourceFile.fromString(
      """
        query UserInfo(\$id: ID!, \$articleId: ID!) {
          user(id: \$id) {
            id
            name
          }
          post(id: \$articleId) {
            id
            title
            description
          }
        }
      """,
    ),
  );

  final ast.DocumentNode withTypenames = ast.transform(
    doc,
    [
      AddTypenames(),
    ],
  );

  print(
    lang.printNode(withTypenames),
  );
}

Features and bugs #

Please file feature requests and bugs at the GitHub.

0.1.0 #

  • A functional GraphQL parser, transformer and printer

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  gql: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:gql/ast.dart';
import 'package:gql/language.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
11
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health suggestions

Fix lib/src/validation/rules/executable_definitions.dart. (-0.50 points)

Analysis of lib/src/validation/rules/executable_definitions.dart reported 1 hint:

line 6 col 1: This function has a return type of 'String', but doesn't end with a return statement.

Fix lib/src/validation/validator.dart. (-0.50 points)

Analysis of lib/src/validation/validator.dart reported 1 hint:

line 19 col 1: This function has a return type of 'ValidatingVisitor', but doesn't end with a return statement.

Format lib/src/ast/transformer.dart.

Run dartfmt to format lib/src/ast/transformer.dart.

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and gql.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
meta ^1.1.7 1.1.7
source_span ^1.5.5 1.5.5
Transitive dependencies
charcode 1.1.2
path 1.6.4
term_glyph 1.1.0
Dev dependencies
cats
pedantic ^1.0.0
test ^1.0.0