npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-to-dart

v2.0.0-beta14

Published

generate dart json_serializable classes using graphql-code-generator

Downloads

123

Readme

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 [email protected] 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 [email protected]

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