Skip to content

Commit

Permalink
feat(ferry_generator): add option to reuse data classes for fragments…
Browse files Browse the repository at this point in the history
… with a single inline fragment spread as selection (#530)

* feat(ferry_generator): add data_class_config: reuse_fragment option in order to re-use selection sets with only a single inline fragment spread. see gql-dart/gql#413 for details.

In order to opt in to this (breaking) change, add

```yaml
          data_class_config:
            reuse_fragments: true
```
To your in the config for your graphql_builder of your build.yaml
  • Loading branch information
knaeckeKami authored Aug 13, 2023
1 parent fbcd8aa commit f8c4210
Show file tree
Hide file tree
Showing 42 changed files with 487 additions and 834 deletions.
2 changes: 1 addition & 1 deletion examples/pokemon_explorer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ dev_dependencies:
ferry_generator: ^0.8.2-dev.0
flutter_test:
sdk: flutter
gql_exec: ^0.4.4
gql_exec: ^1.0.0
flutter:
uses-material-design: true
9 changes: 2 additions & 7 deletions packages/ferry_cache/test/test/data_change_streams_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ final han = GHeroWithFragmentsData_hero(
..friendsConnection
.edges
.add(GHeroWithFragmentsData_hero_friendsConnection_edges(
(b) => b
..node = GHeroWithFragmentsData_hero_friendsConnection_edges_node
.fromJson(luke.toJson())!
.toBuilder(),
(b) => b..node = GheroDataData.fromJson(luke.toJson())!.toBuilder(),
)),
);

Expand Down Expand Up @@ -480,9 +477,7 @@ void main() {
.add(GHeroWithFragmentsData_hero_friendsConnection_edges(
(b) => b
..node =
GHeroWithFragmentsData_hero_friendsConnection_edges_node
.fromJson(vader.toJson())!
.toBuilder(),
GheroDataData.fromJson(vader.toJson())!.toBuilder(),
))));

await Future.delayed(Duration.zero);
Expand Down
1 change: 1 addition & 0 deletions packages/ferry_generator/lib/graphql_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class GraphqlBuilder implements Builder {
p.basename(generatedFilePath(buildStep.inputId, dataExtension)),
config.typeOverrides,
config.whenExtensionConfig,
config.dataClassConfig,
),
varExtension: buildVarLibrary(
doc,
Expand Down
18 changes: 17 additions & 1 deletion packages/ferry_generator/lib/src/utils/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BuilderConfig {
final String outputDir;
final String sourceExtension;
final InlineFragmentSpreadWhenExtensionConfig whenExtensionConfig;
final DataClassConfig dataClassConfig;

BuilderConfig(Map<String, dynamic> config)
: schemaId = config['schema'] == null
Expand All @@ -37,7 +38,22 @@ class BuilderConfig {
enumFallbackConfig = _getEnumFallbackConfig(config),
outputDir = config['output_dir'] ?? '__generated__',
sourceExtension = config['source_extension'] ?? '.graphql',
whenExtensionConfig = _getWhenExtensionConfig(config);
whenExtensionConfig = _getWhenExtensionConfig(config),
dataClassConfig = _getDataClassConfig(config);
}

DataClassConfig _getDataClassConfig(Map<String, dynamic> config) {
final dataClassConfig = switch (config) {
{
'data_class_config': {
'reuse_fragments': final reuseFragments,
}
} =>
DataClassConfig(reuseFragments: reuseFragments == true),
_ => const DataClassConfig(reuseFragments: false),
};

return dataClassConfig;
}

InlineFragmentSpreadWhenExtensionConfig _getWhenExtensionConfig(
Expand Down
6 changes: 3 additions & 3 deletions packages/ferry_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ homepage: https://ferrygraphql.com/
description: Generated types for Ferry GraphQL Client
repository: https://github.com/gql-dart/ferry
environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=3.0.0 <4.0.0'
dependencies:
gql: '>=0.14.0 <2.0.0'
gql_code_builder: ^0.7.1
gql_code_builder: ^0.8.0
built_collection: ^5.0.0
code_builder: ^4.3.0
build: ^2.0.0
path: ^1.8.0
ferry_exec: ^0.3.1
yaml: ^3.1.0
dart_style: ^2.2.4
dart_style: ^2.3.2
glob: ^2.0.1
built_value_generator: ^8.1.1
built_value: ^8.1.2
Expand Down
2 changes: 2 additions & 0 deletions packages/ferry_test_graphql2/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ targets:
enabled: true
options:
schema: ferry_test_graphql2|lib/schema/schema.graphql
data_class_config:
reuse_fragments: true
type_overrides:
Date:
name: DateTime
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f8c4210

Please sign in to comment.