gql_build 0.0.8

A package for Dart code generation from GraphQL source.

Usage example #

Builders in this package let you build your GraphQL requests and view the response in a type-safe way.

final result = await link.request(
  FindPokemon()..name = "Charizard",
);
final data = $FindPokemon(result.data);

final pokemon = data.pokemon;

print("Found ${pokemon.name}");
print("ID: ${pokemon.id}");

final weight = pokemon.weight;
final height = pokemon.height;

print(
  "Weight: ${weight.minimum} – ${weight.maximum}",
);
print(
  "Height: ${height.minimum} – ${height.maximum}",
);

Provided builders #

ast_builder #

Generates an AST representation of a GraphQL Document

data_builder #

Generates a typed view on top of data JSON. The generated classes are not supposed to be used directly as that would introduce tight coupling between your GraphQL documents and your application code.

class $ListPokemon {
  const $ListPokemon(this.data);

  final Map<String, dynamic> data;

  List<$ListPokemon$pokemons> get pokemons => (data["pokemons"] as List)
      .map((dynamic e) => $ListPokemon$pokemons(e as Map<String, dynamic>))
      .toList();
}

class $ListPokemon$pokemons {
  const $ListPokemon$pokemons(this.data);

  final Map<String, dynamic> data;

  String get id => data["id"] as String;
  String get name => data["name"] as String;
}

op_builder #

Wraps AST and operation name into an Operation.

import 'package:gql_exec/gql_exec.dart';
import 'find_pokemon.ast.gql.dart' as find_pokemon;

const FindPokemon = Operation(
  document: find_pokemon.document,
  operationName: 'FindPokemon',
);

req_builder #

Extend Request class to use specific Operation and provide ability to build variables. Builder pattern let's you handle nullable variables correctly (GraphQL spec pretty much requires both undefined and null values).

import 'package:gql_exec/gql_exec.dart' as _i1;
import 'find_pokemon.op.gql.dart' as _i2;

class FindPokemon extends _i1.Request {
  FindPokemon()
      : super(operation: _i2.FindPokemon, variables: <String, dynamic>{});

  set name(String value) => variables["name"] = value;
}

enum_builder #

Generates an enum-like class per GraphQL enum type. Defines known enum values to be used in your code, and allows unknown enum values to be used without causing runtime error when handling response data.

class ReleaseType {
  const ReleaseType(this.value);

  final String value;

  static const ReleaseType ALPHA = ReleaseType('ALPHA');

  static const ReleaseType BETA = ReleaseType('BETA');

  static const ReleaseType GAMMA = ReleaseType('GAMMA');

  @override
  int get hashCode => value.hashCode;
  @override
  bool operator ==(Object o) => o is ReleaseType && o.value == value;
}

scalar_builder #

Generates a container for a scalar value to be used when viewing the response data and building request variables.

class ID {
  const ID(this.value);

  final String value;

  @override
  int get hashCode => value.hashCode;
  @override
  bool operator ==(Object o) => o is ID && o.value == value;
}

input_builder #

Generates an input builder to be used to build request variables.

class MutationInput {
  final Map<String, dynamic> input = <String, dynamic>{};

  set a(String value) => input['a'] = value;
}

schema_builder #

Combines enum_builder, input_builder and scalar_builder.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.0.8 #

  • escape reserved and private names

0.0.7 #

  • fix schema url bug

0.0.6 #

  • fix nullable url bug

0.0.5 #

  • upgrade package:gql_code_builder
  • read source as a tree of SourceNodes
  • use allocator to resolve references to other generated files

0.0.4 #

  • upgrade package:gql_code_gen

0.0.3 #

  • Support for fragment builder

0.0.2 #

  • Split off var_builder off of req_builder

0.0.1 #

  • Spun off of package:gql_code_gen

example/README.md

Add gql_build to your pubspec.yaml

dependencies:
  gql: ^0.12.0
  gql_exec: ^0.2.0

dev_dependencies:
  gql_build: ^0.0.1

Enable the builders you want by setting enabled: true.

targets:
  $default:
    builders:
      gql_build|schema_builder:
        enabled: true
      gql_build|ast_builder:
        enabled: true
      gql_build|op_builder:
        enabled: true
        options:
          schema: gql_example_cli|lib/schema.graphql
      gql_build|data_builder:
        enabled: true
        options:
          schema: gql_example_cli|lib/schema.graphql
      gql_build|req_builder:
        enabled: true
        options:
          schema: gql_example_cli|lib/schema.graphql

Build using build_runner

pub run build_runner build

See gql_example_cli for a usage of the generated classes.

Use this package as a library

1. Depend on it

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


dependencies:
  gql_build: ^0.0.8

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

We analyzed this package on Feb 23, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
build ^1.0.0 1.2.2
code_builder ^3.2.0 3.2.1
dart_style ^1.2.9 1.3.3
glob ^1.2.0 1.2.0
gql ^0.12.1 0.12.2
gql_code_builder ^0.0.8 0.0.8
path ^1.6.4 1.6.4
Transitive dependencies
_fe_analyzer_shared 1.0.3
analyzer 0.39.4
args 1.5.2
async 2.4.0
built_collection 4.3.2
built_value 7.0.9
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
fixnum 0.10.11
html 0.14.0+3
js 0.6.1+1
logging 0.11.4
matcher 0.12.6
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
pedantic 1.9.0
pub_semver 1.4.3
quiver 2.1.2+1
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_test ^0.10.7
gql_pedantic ^1.0.1
test ^1.0.0