GraphQL Plugin

Support for using graphql in Dart.

This plugin defines a simple framework for generating graphql entities.

The code generation is based on a set of graphql queries but all affected types are generated using the original type name and all properties. This behavior allows to use a common type like PageInfo everywhere in your code.

Features

  • Generate directly from http endpoint
  • Lazy response parsing
  • Case insensitive enum handling

Getting started

See example project

build.yaml

Add build.yaml to your project:

targets:
  $default:
    builders:
      example|graphql_builder:
        generate_for:
          - "**/*.graphql"
        options:
          gqlEndpoint: "https://gitlab.com/api/graphql"

builders:
  graphql_builder:
    import: "package:dart_graphql/generator/graphql_fetch_generator.dart"
    builder_factories: ["getGqlBuilder"]
    build_extensions:
      .graphql:
        - .graphql.dart
    auto_apply: dependents
    build_to: source
    applies_builders: ["source_gen"]

gitlab.graphql

Create a graphql file:

query GitlabCEIssueLabels($firstN: Int) {
  project(fullPath: "gitlab-org/gitlab-ce") {
    id
    issues(first: $firstN) {
      edges {
        node {
          title
          state
          labels {
            edges {
              node {
                title
              }
            }
          }
        }
      }
    }
  }
}

Build

Run pub run build_runner build --verbose --delete-conflicting-outputs

Use

import 'package:dart_graphql/dart_graphql.dart';
import 'package:example/gitlab.graphql.dart';

void main() async {
  GraphqlClient gqlClient = GraphqlClient("https://gitlab.com/api/graphql");
  var gqlResponse = await gqlClient.query(GitlabCEIssueLabels(firstN: 15));

  print("Issue labels:");
  for (IssueEdge issueEdge in gqlResponse.data.project.issues.edges) {
    List<String> labels =
        issueEdge.node.labels.edges.map((e) => e.node.title).toList();

    print(
        "${issueEdge.node.title} [${issueEdge.node.state}]: ${labels.join(', ')}");
  }
}

Notes

The plugin based on graphql-fetch project ported to dart2 and extended.

Please file feature requests and bugs at the issue tracker.

Libraries

GraphqlSchema
GraphqlSetting
dart_graphql
generator
graphql_client_generator
graphql_fetch.client