sling_gql_gen 0.1.1
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 thesling_gqlimport in the generated file. Defaults topackage:sling_gql/sling_gql.dart.--scalar(optional, repeatable): custom scalar mapping,Name=DartType[:converterExpr].--scalar DateTime=DateTimeuses the built-inDateTime.parse/.toIso8601String()converter; aDartTypeother thanDateTimeneeds a converter, e.g.--scalar Money=Decimal:MoneyConvertercallsMoneyConverter.parse/MoneyConverter.serialize(a class with those two static methods you provide). Scalars without a--scalarflag keep the default in the table below.
What it generates #
class Query extends Accessorfor the schema's query root type, withQuery(super.recorder, super.selection, super.path);andQuery.root(Recorder r) : super(r, r.root, const []);constructors.- One
class <Name> extends Accessorper otherOBJECTtype (skipping the mutation/subscription roots and introspection__*types), with a getter per argument-less field and a method (named optional /requiredparams) 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, defaultid) of a keyed type (writing it would corrupt the entity key), every field ofPageInfo, andtotalCount/pageInfoon connection-shaped types (those withpageInfoplusnodesoredges). enum <EnumName> { value('VALUE'), …, unknown('') }for eachENUMtype. Constants are the lowerCamelCase of the wire name (PARTIAL_FAILURE→partialFailure);graphqlNameis the wire value,fromGraphQL(String)maps a wire value back (returningunknownfor a value added to the schema after generation, so aswitchstays exhaustive), andtoGraphQL()is what arguments and input fields serialize with — it throws anArgumentErrorforunknown. Enum fields are read throughAccessor.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 eachINPUT_OBJECTtype.extension SlingCacheAccess on CacheScope<Query>with one method per keyed type (anOBJECTwith a scalar--key-field), returning the cached entity ornullwithout ever fetching:Launch? launch(String id) => entity('Launch', id, Launch.new);— used asclient.cacheScope.launch('launch-181'). The method is the type name with a lowercase first letter; a clash with a Dart keyword, aCacheScopemember (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