Skip to content

Commit

Permalink
feat(ferry_generator): support adding in the config of build.yaml in …
Browse files Browse the repository at this point in the history
…your graphql_builder in order to wrap nullable fields into a Value type in order to distinguish between absent and null values in inputs (#549)
  • Loading branch information
knaeckeKami authored Nov 12, 2023
1 parent c54e32a commit b580572
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/ferry_cache/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ dev_dependencies:
test: ^1.16.8
ferry_test_graphql2: ^0.2.4-dev.0+1
gql_exec: ^1.0.0
gql: ^1.0.0
gql: ^1.0.0
gql_tristate_value: ^1.0.0
44 changes: 32 additions & 12 deletions packages/ferry_generator/lib/graphql_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ class GraphqlBuilder implements Builder {
'When extensions require add_typenames to be true. Consider setting add_typenames to true in your build.yaml or disabling when_extensions in your build.yaml.');
}

final triStateValueConfig = config.triStateOptionalsConfig;

final schemaOutputAsset =
outputAssetId(_schemaId!, schemaExtension, config.outputDir);

final schemaUrl = schemaOutputAsset.uri.toString();
final schemaAllocator = GqlAllocator(
buildStep.inputId.uri.toString(),
config.sourceExtension,
outputAssetId(buildStep.inputId, schemaExtension, config.outputDir)
.uri
.toString(),
schemaUrl,
config.outputDir,
);

final varAllocator = schemaAllocator;

final libs = <String, Library>{
astExtension: buildAstLibrary(doc),
dataExtension: buildDataLibrary(
Expand All @@ -90,29 +108,31 @@ class GraphqlBuilder implements Builder {
config.dataClassConfig,
),
varExtension: buildVarLibrary(
doc,
addTypenames(schema),
p.basename(generatedFilePath(buildStep.inputId, varExtension)),
config.typeOverrides,
),
doc,
addTypenames(schema),
p.basename(generatedFilePath(buildStep.inputId, varExtension)),
config.typeOverrides,
varAllocator,
triStateValueConfig),
reqExtension: buildReqLibrary(
doc,
p.basename(generatedFilePath(buildStep.inputId, reqExtension)),
),
schemaExtension: buildSchemaLibrary(
doc,
p.basename(generatedFilePath(buildStep.inputId, schemaExtension)),
config.typeOverrides,
config.enumFallbackConfig,
generatePossibleTypesMap: config.shouldGeneratePossibleTypes,
),
doc,
p.basename(generatedFilePath(buildStep.inputId, schemaExtension)),
config.typeOverrides,
config.enumFallbackConfig,
generatePossibleTypesMap: config.shouldGeneratePossibleTypes,
allocator: schemaAllocator,
triStateValueConfig: triStateValueConfig),
};

for (var entry in libs.entries) {
final generatedAsset =
outputAssetId(buildStep.inputId, entry.key, config.outputDir);
final schemaOutputAsset =
outputAssetId(_schemaId!, schemaExtension, config.outputDir);
outputAssetId(_schemaId, schemaExtension, config.outputDir);

final allocator = GqlAllocator(
buildStep.inputId.uri.toString(),
Expand Down
16 changes: 15 additions & 1 deletion packages/ferry_generator/lib/src/utils/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BuilderConfig {
final String sourceExtension;
final InlineFragmentSpreadWhenExtensionConfig whenExtensionConfig;
final DataClassConfig dataClassConfig;
final TriStateValueConfig triStateOptionalsConfig;

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

DataClassConfig _getDataClassConfig(Map<String, dynamic> config) {
Expand Down Expand Up @@ -130,3 +132,15 @@ Map<String, String> _enumFallbackMap(YamlMap? enumFallbacks) {
),
);
}

TriStateValueConfig _getTriStateOptionalsConfig(Map<String, dynamic>? config) {
final Object? configValue = config?['tristate_optionals'];

if (configValue is bool) {
return configValue
? TriStateValueConfig.onAllNullableFields
: TriStateValueConfig.never;
}

return TriStateValueConfig.never;
}
3 changes: 2 additions & 1 deletion packages/ferry_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ topics:
- codegen
dependencies:
gql: '>=0.14.0 <2.0.0'
gql_code_builder: ^0.8.0
gql_code_builder: ^0.9.1+1
gql_tristate_value: ^1.0.0
built_collection: ^5.0.0
code_builder: ^4.3.0
build: ^2.0.0
Expand Down
1 change: 1 addition & 0 deletions packages/ferry_test_graphql2/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ targets:
enabled: true
options:
schema: ferry_test_graphql2|lib/schema/schema.graphql
tristate_optionals: false
data_class_config:
reuse_fragments: true
type_overrides:
Expand Down
2 changes: 1 addition & 1 deletion packages/ferry_test_graphql2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
ferry_exec: ^0.5.0-dev.0+1
built_value: ^8.0.4
built_collection: ^5.0.0
gql_code_builder: ^0.8.0
gql_code_builder: ^0.9.0
dev_dependencies:
test: ^1.16.8
build_runner: ^2.0.2
Expand Down

0 comments on commit b580572

Please sign in to comment.