dart_graphql 0.4.2
dart_graphql: ^0.4.2

Dart native js
Flutter Android iOS web

Generates dart code for GraphQL endpoints using the original type names and all properties.

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 #

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(', ')}");
  }
}

File upload #

On your server create a new scalar type called 'Upload' and use the generator. The generator will create a FileUploadInput everywhere where it sees Upload. Before the request the library goes into the query variables recursively and collect all files and their paths in the tree. You can restore them on your server as described in https://github.com/jaydenseric/graphql-multipart-request-spec All file fields will have null value in the request. You can save bandwidth if you use the same FileUploadInput instance if you want to use the same file.

Notes #

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

Please file feature requests and bugs at the issue tracker.

1
likes
90
pub points
46%
popularity

Publisher

blacksalt.io

Generates dart code for GraphQL endpoints using the original type names and all properties.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD (LICENSE)

Dependencies

async, build, build_runner, code_builder, dart_style, graphql_parser, http, json_object_lite, meta, path

More

Packages that depend on dart_graphql