graphql-to-dart
generate dart json_serializable classes using graphql-code-generator
Last updated 6 months ago by micimize .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install graphql-to-dart 
SYNC missed versions from official npm registry.

graphql-to-dart

Custom templates for graphql-code-generator for generating dart PODOs and json_serializable classes

2.0.0 Beta

This beta makes the api entirely immutable

npm install graphql-to-dart@2.0.0-beta5 includes a number of updates, such as:

  • per-operation file generation (probably non-optional)
  • gql_code_gen support via integrateGqlCodeGenAst
  • transformCharacters support for handling _underscore_prefixed fields defaults to { "^_+": "" }, resulting in "__typename" -> "typename"
  • Models graphql-style "inheritance" by defining a protected _$[Type}Fields type along with every object type, which is then exposed by fragment mixins and selection sets.
  • Fragments are modeled generated as mixins, as well as standalone FragmentNameSelectionSet classes
  • all classes are immutable and use the equatable package

usage

yarn add -D graphql-code-generator graphql graphql-to-dart@1.1.2-beta

write codegen.yaml to customize your build as needed. This is the full working config I'm using in the wild:

schema: schema/__generated__/schema.json
documents:
  # fragments are _private and collected first
  # so we can hack their fields
  - '../app/lib/**/_*.graphql'
  - '../app/lib/**/!(_)*.graphql'
overwrite: true
generates:
  ../app/lib/graphql/schema.dart:
    - graphql-to-dart/schema-types
  ../app/lib/:
    # generate operation types next to their source files
    # NOTE there's actually a coupling between the preset and plugin
    # with respect to `integrateGqlCodeGenAst` atm
    preset: graphql-to-dart
    presetConfig:
      # all required
      extension: .graphql.dart
      packageName: savvy_app
      schemaTypesPath: ../app/lib/graphql/schema.dart
    plugins:
      - graphql-to-dart/documents
config:
  # re-export built gql_code_gen ast files
  integrateGqlCodeGenAst: true
  schema:
    imports:
      # import 'package:foo/bar.dart' show biz; works as well here
      - package:savvy_app/graphql/scalars/scalars.dart
      - package:savvy_app/graphql/base.dart
    exports:
      - package:savvy_app/graphql/scalars/scalars.dart
      - package:savvy_app/graphql/base.dart
  mixins:
    # add `with Entity` when a generated class has these fields
    - when:
        fields:
        - entityId
        - validFrom
        - validUntil
      name: Entity
  scalars:
    Date: DateTime
    # requires a PGDateTimeProvider in the schema lib
    Datetime: PGDateTime
    FiniteDatetime: DateTime
    UUID: String
    Rrule: RecurrenceRule
    Cursor: String
    JSON: Object
  replaceTypes:
    TemporalIdInput: TemporalId
    # Replace with a reference, if I remember correctly
    TemporalId: TemporalId
    GoogleSignInInput: GoogleSignIn
  irreducibleTypes:
   # I have a common in-fragment type
   # that is always inherited the same way,
   # so I just repace it's inputs and make my own irreducible.
   - TemporalId

Then generate with yarn gql-gen (or gql-gen if you have it globally installed), And theeeen generate the actual json serializers (equatable and json_serializable are peer dependencies, but on the flutter side, also gql_code_gen if you're using the ast integration). So:

yarn gql-gen
flutter packages pub run build_runner build
flutter format lib/**/*graphql.dart # you're gunna want this
# or npm install globstar && globstar -- flutter format "lib/**/*.graphql.dart"

Make sure you have a build.yaml like in the example, and the deps in the pubspec.yaml:

# ...
dev_dependencies:
  build_runner: ^0.9.0
  json_serializable: ^0.5.4
  # ...

dependencies:
  json_annotation: ^0.2.3
  equatable: 1.0.2
  # ...
# ...

...Obviously this is not the most user friendly process yet.

Take a look at the example output to see how it generates code, as well as src/build-plugin.ts for the configuration object, which has some docs just aching to be properly generated.

NOTES

  • Base types do not currently have json helpers, but it should probably be configurable in case of the configuration replaceTypes: { "BaseTypeInput": "BaseType" }
  • I probably won't touch this for a while once again.
  • Really what we want is for @klavs's gql_code_gen to become more mature, but I keep sinking energy into this because I can go fast here
  • #import "./fragment.graphql bolton support has been merged into gql_code_gen
  • (possibly outdated): You can have multiple inline fragments on the same document, but they will be named with leading underscores, like Query_TypeInlineFragment, which is ugly

Current Tags

  • 2.0.0-beta14                                ...           latest (6 months ago)

99 Versions

  • 2.0.0-beta14                                ...           6 months ago
  • 2.0.0-beta13                                ...           6 months ago
  • 2.0.0-beta12                                ...           6 months ago
  • 2.0.0-beta11                                ...           6 months ago
  • 2.0.0-beta9                                ...           6 months ago
  • 2.0.0-beta8                                ...           6 months ago
  • 2.0.0-beta7                                ...           6 months ago
  • 2.0.0-beta6                                ...           6 months ago
  • 2.0.0-beta5                                ...           6 months ago
  • 2.0.0-beta4                                ...           6 months ago
  • 2.0.0-beta3                                ...           6 months ago
  • 2.0.0-beta2                                ...           6 months ago
  • 2.0.0-beta1                                ...           6 months ago
  • 2.0.0-alpha9                                ...           6 months ago
  • 2.0.0-alpha8                                ...           6 months ago
  • 2.0.0-alpha7                                ...           6 months ago
  • 2.0.0-alpha6                                ...           6 months ago
  • 2.0.0-alpha5                                ...           6 months ago
  • 2.0.0-alpha4                                ...           6 months ago
  • 2.0.0-alpha3                                ...           6 months ago
  • 2.0.0-alpha2                                ...           6 months ago
  • 2.0.0-alpha1                                ...           6 months ago
  • 2.0.0-prealpha1                                ...           6 months ago
  • 1.2.1-beta                                ...           6 months ago
  • 1.2.0-beta                                ...           6 months ago
  • 1.1.5-beta                                ...           6 months ago
  • 1.1.4-beta                                ...           7 months ago
  • 1.1.2-beta                                ...           7 months ago
  • 1.1.1-beta                                ...           7 months ago
  • 1.1.0-beta                                ...           7 months ago
  • 1.0.1-alpha17                                ...           7 months ago
  • 1.0.1-alpha16                                ...           7 months ago
  • 1.0.1-alpha15                                ...           7 months ago
  • 1.0.1-alpha14                                ...           7 months ago
  • 1.0.1-alpha13                                ...           7 months ago
  • 1.0.1-alpha12                                ...           7 months ago
  • 1.0.1-alpha11                                ...           7 months ago
  • 1.0.1-alpha10                                ...           7 months ago
  • 1.0.1-alpha9                                ...           7 months ago
  • 1.0.1-alpha8                                ...           7 months ago
  • 1.0.1-alpha7                                ...           7 months ago
  • 1.0.1-alpha6                                ...           7 months ago
  • 1.0.1-alpha5                                ...           7 months ago
  • 1.0.1-alpha4                                ...           7 months ago
  • 1.0.1-alpha3                                ...           7 months ago
  • 1.0.1-alpha2                                ...           7 months ago
  • 1.0.1-alpha1                                ...           7 months ago
  • 1.0.1-alpha                                ...           7 months ago
  • 1.0.0-beta-stable4                                ...           7 months ago
  • 1.0.0-beta-stable3                                ...           7 months ago
  • 1.0.0-beta-stable2                                ...           7 months ago
  • 1.0.0-beta-stable1                                ...           7 months ago
  • 1.0.0-beta-stable                                ...           7 months ago
  • 1.0.0-beta2                                ...           7 months ago
  • 1.0.0-beta1                                ...           7 months ago
  • 1.0.0-beta                                ...           7 months ago
  • 1.0.0-alpha.1                                ...           7 months ago
  • 1.0.0-alpha.0                                ...           7 months ago
  • 0.3.6                                ...           a year ago
  • 0.3.5                                ...           a year ago
  • 0.3.4                                ...           a year ago
  • 0.3.3                                ...           a year ago
  • 0.3.2                                ...           a year ago
  • 0.3.1                                ...           a year ago
  • 0.3.0                                ...           a year ago
  • 0.3.0-alpha8                                ...           a year ago
  • 0.3.0-alpha7                                ...           a year ago
  • 0.3.0-alpha6                                ...           a year ago
  • 0.3.0-alpha5                                ...           a year ago
  • 0.3.0-alpha4                                ...           a year ago
  • 0.3.0-alpha3                                ...           a year ago
  • 0.3.0-alpha2                                ...           a year ago
  • 0.3.0-alpha1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.2-alpha                                ...           a year ago
  • 0.1.1-alpha                                ...           a year ago
  • 0.1.0-aplha4                                ...           a year ago
  • 0.1.0-alpha3                                ...           a year ago
  • 0.1.0-alpha2 [deprecated]           ...           a year ago
  • 0.1.0-alpha1 [deprecated]           ...           a year ago
  • 0.0.4-alpha4                                ...           2 years ago
  • 0.0.4-alpha3                                ...           2 years ago
  • 0.0.4-alpha2                                ...           2 years ago
  • 0.0.4-alpha1                                ...           2 years ago
  • 0.0.3-alpha2                                ...           2 years ago
  • 0.0.3-alpha1                                ...           2 years ago
  • 0.0.2-alpha4                                ...           2 years ago
  • 0.0.2-alpha3                                ...           2 years ago
  • 0.0.2-alpha2                                ...           2 years ago
  • 0.0.2-alpha1                                ...           2 years ago
  • 0.0.1-alpha8                                ...           2 years ago
  • 0.0.1-alpha7                                ...           2 years ago
  • 0.0.1-alpha6                                ...           2 years ago
  • 0.0.1-alpha5                                ...           2 years ago
  • 0.0.1-alpha4                                ...           2 years ago
  • 0.0.1-alpha3                                ...           2 years ago
  • 0.0.1-alpha2                                ...           2 years ago
  • 0.0.1-alpha1                                ...           2 years ago
  • 0.0.1-alpha                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |