sling_gql_gen 0.1.1 copy "sling_gql_gen: ^0.1.1" to clipboard
sling_gql_gen: ^0.1.1 copied to clipboard

Code generator for sling_gql: turns a GraphQL introspection JSON into typed Accessor classes matching the sling_gql runtime contract.

sling_gql_gen #

A Dart CLI code generator: turns a GraphQL introspection JSON document into typed Accessor classes for the sling_gql runtime. Docs: https://tpucci.github.io/sling_gql/

Usage #

Add it as a dev dependency of your Flutter app and run it against your schema's introspection JSON:

flutter pub add --dev sling_gql_gen
dart run sling_gql_gen \
  --schema graphql/schema.json \
  --out lib/generated/schema.dart

(Inside this repository, the example app runs the same command as dart run ../packages/sling_gql_gen/bin/sling_gql_gen.dart …, or melos run generate.)

This writes lib/generated/schema.dart and formats it with dart format (a formatting failure is logged but does not fail the run). The generated file starts with a header comment explaining the $-rename scheme (type$, $typename, $eq, ...) in one place, so a reader never has to go digging for why an identifier looks the way it does.

Flags:

  • --schema (required): path to a GraphQL introspection JSON file, i.e. the standard {"__schema": {...}} result of the introspection query.
  • --out (required): path of the Dart file to generate. Parent directories are created as needed.
  • --part-of-import (optional): overrides the sling_gql import in the generated file. Defaults to package:sling_gql/sling_gql.dart.
  • --scalar (optional, repeatable): custom scalar mapping, Name=DartType[:converterExpr]. --scalar DateTime=DateTime uses the built-in DateTime.parse/.toIso8601String() converter; a DartType other than DateTime needs a converter, e.g. --scalar Money=Decimal:MoneyConverter calls MoneyConverter.parse/MoneyConverter.serialize (a class with those two static methods you provide). Scalars without a --scalar flag keep the default in the table below.

What it generates #

  • class Query extends Accessor for the schema's query root type, with Query(super.recorder, super.selection, super.path); and Query.root(Recorder r) : super(r, r.root, const []); constructors.
  • One class <Name> extends Accessor per other OBJECT type (skipping the mutation/subscription roots and introspection __* types), with a getter per argument-less field and a method (named optional / required params) per field with arguments. Scalar and enum fields without arguments also get a setter for optimistic writes, except where a write could never be right: the key field (--key-field, default id) of a keyed type (writing it would corrupt the entity key), every field of PageInfo, and totalCount/pageInfo on connection-shaped types (those with pageInfo plus nodes or edges).
  • enum <EnumName> { value('VALUE'), …, unknown('') } for each ENUM type. Constants are the lowerCamelCase of the wire name (PARTIAL_FAILURE → partialFailure); graphqlName is the wire value, fromGraphQL(String) maps a wire value back (returning unknown for a value added to the schema after generation, so a switch stays exhaustive), and toGraphQL() is what arguments and input fields serialize with — it throws an ArgumentError for unknown. Enum fields are read through Accessor.enumValue/enumList (same miss/skeleton semantics as scalars) and their setters write the wire name. Constants that would clash with a Dart keyword or an enum member (unknown, values, index, name, …) get a trailing $: UNKNOWN → unknown$.
  • class <InputName> { const <InputName>({...}); ...; toJson() {...} } for each INPUT_OBJECT type.
  • extension SlingCacheAccess on CacheScope<Query> with one method per keyed type (an OBJECT with a scalar --key-field), returning the cached entity or null without ever fetching: Launch? launch(String id) => entity('Launch', id, Launch.new); — used as client.cacheScope.launch('launch-181'). The method is the type name with a lowercase first letter; a clash with a Dart keyword, a CacheScope member (query, list, entity, evict, …) or another type's method gets a trailing $ (Entity → entity$). Omitted when no type is keyed.

All getters are nullable (T?, R?, List<R>?) regardless of the schema's non-null markers, since cache data may be missing — see packages/sling_gql/lib/src/accessor.dart.

Development #

cd packages/sling_gql_gen
dart pub get
dart analyze
dart test
0
likes
160
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Code generator for sling_gql: turns a GraphQL introspection JSON into typed Accessor classes matching the sling_gql runtime contract.

Homepage
Repository (GitHub)
View/report issues

Topics

#graphql #codegen #cli

License

MIT (license)

Dependencies

args, http, path

More

Packages that depend on sling_gql_gen