gql 0.11.1

  • Readme
  • Changelog
  • Installing
  • 87

A package for working with GraphQL documents.

⚠️ This package is under development ⚠️

This package exports several libraries:

  • package:gql/language.dart for working with GraphQL source;
  • package:gql/ast.dart for working with GraphQL AST nodes;

package:gql/language.dart #

Parsing GraphQL documents #

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

void main() {
  final ast.DocumentNode doc = lang.parseString(
    """
    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,
            ),
          )
        ],
      ),
    ),
  );
}

package:gql/ast.dart #

Visiting GraphQL AST nodes #

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

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.parseString(
    """
    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;

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.parseString(
    """
    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.11.1 #

  • fix for parsing empty list and object literals

0.11.0 #

  • expose Request and Response extensions via Context
  • add convenience methods to work with Context
  • split off Link to package:gql_link

0.10.0 #

  • rework Context to consist of ContextEntry map with equality checks

0.9.0 #

  • split off code generation to package:gql_code_gen

0.8.3 #

  • downgrade some dependencies for Flutter compatibility

0.8.2 #

  • package:gql/link: add const constructor to ResponseParser and RequestSerializer

0.8.1 #

  • package:gql/execution: add Context to Response
  • make Link extendable
  • package:gql/link: implement ResponseParser and RequestSerializer

0.8.0 #

  • package:gql/execution: add Context to Request

0.7.4 #

  • Implement GraphQLError.toString
  • Use package:gql_http_link in ast_builder example

0.7.3 #

  • Fix equality for package:gql/execution ErrorLocation

0.7.2 #

  • Implement equality checks for all package:gql/execution classes

0.7.1 #

  • Fix AST equality
  • Extract lint rules

0.7.0 #

  • Breaking: use List instead of Iterbale
  • Add documentation to experimental package:gql/execution and package:gql/link

0.6.0 #

  • Experimental package:gql/execution to define request/response types
  • Experimental package:gql/link to implement Link based on DocumentNode queries

0.5.0 #

  • Implement package:gql/ast_builder to parse *.graphql documents at build-time
  • Stricter lint rules

0.4.0 #

  • Define name on DefinitionNode to simplify name access for DocumentNode.definitions
  • Rework Node.visitChildren and implement Node.== and Node.hashCode
  • Lower SDK requirement to >=2.2.2

0.3.0 #

  • Dart code generation via fromNode from package:gql/dart.dart using code_gen

0.2.0 #

  • parseString to parse directly from String
  • Improve parsing exceptions
  • AccumulatingVisitor for validation rules
  • Expose experimental Validation API

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.11.1

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/document.dart';
import 'package:gql/execution.dart';
import 'package:gql/language.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
87
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

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.2.2 <3.0.0
collection ^1.14.11 1.14.12
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
gql_pedantic ^1.0.0
test ^1.0.0