micimize / graphql-to-dart

generate dart classes and respective JsonSerializable transcoders
42 stars 8 forks source link

Slice-based inheritance #18

Closed micimize closed 4 years ago

micimize commented 4 years ago

This is a pretty major overhaul that uses a @protected fields approach to dealing with fragment/selection set inheritance.

I've broken the schema and document generators into two plugins. The schema types have protected _{Type}Fields types, which are then exposed via getters and setters by document types like so:

// fragment
mixin Home on Human {
  static final String typeName = "Human";

  @JsonKey(required: false, disallowNullValue: false)
  String get home => fields.homePlanet;
  set home(String home) => fields.homePlanet = home;
}
// selection set
@JsonSerializable()
class HeroForEpisodeHumanInlineFragment extends Human with Home {
  static final String typeName = "Human";

  @JsonKey(required: false, disallowNullValue: false)
  double get height => fields.height;
  set height(double height) => fields.height = height;
  HeroForEpisodeHumanInlineFragment({
    double height,
    String home,
  }) : super(
          height: height,
          homePlanet: home,
        );

  @protected
  Set<String> get missingRequiredFields {
    Set<String> missingFields = Set();
    return missingFields;
  }

  factory HeroForEpisodeHumanInlineFragment.fromJson(
          Map<String, dynamic> json) =>
      _$HeroForEpisodeHumanInlineFragmentFromJson(json);

  Map<String, dynamic> toJson() =>
      _$HeroForEpisodeHumanInlineFragmentToJson(this)
        ..['__typename'] = typeName;
}

The new config looks something like

schema: "http://localhost:8080/graphql"
documents:
  - 'lib/**/_*.gql'
  - 'lib/**/!(_)*.gql'
overwrite: true
generates:
  lib/graphql/schema.dart:
    #- graphql-to-dart/schema-types
    - ../dist/schema-types.js
  lib/:
    preset: near-operation-file
    presetConfig:
      extension: .dart
      baseTypesPath: graphql/schema.dart
      importTypesTemplate: import '{{relativeImportPath}}.dart';
      importFragmentsTemplate: import '{{fragmentImportPath}}.dart' show {{fragmentNames}};
      fragmentImportSuffix: ' '
    plugins:
      - ../dist/documents.js
        #- graphql-to-dart
        #
config:
  schema:
    imports:
      - ./example_mixin.dart
  mixins:
     - name: HelloMixin
       when:
        fields:
          - name
  addInputHelpers:
    exceptFor:
      - ReviewInput
      - Color 
micimize commented 4 years ago

I'm thinking about writing a utility wrapper for build_runner that can call https://pub.dev/packages/gql_code_gen and generate ASTs for runtime. Kind of a hassle though