Skip to content

Commit

Permalink
chore(vars-create-input): Adding Create Factories to Input types (#451)
Browse files Browse the repository at this point in the history
* chore(vars-generator): Applying vars generation to input types

* Adding codegen artifacts to e2e tests

* Adding codegen artifacts to e2e tristate

* Adding Unit Test
  • Loading branch information
caffeineflo authored Mar 5, 2024
1 parent 44c3686 commit bfafca5
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 10 deletions.
1 change: 1 addition & 0 deletions codegen/end_to_end_test/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ targets:
gql_build|schema_builder:
enabled: true
options:
vars_create_factories: true
enum_fallbacks:
LengthUnit: METER
type_overrides:
Expand Down

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

7 changes: 7 additions & 0 deletions codegen/end_to_end_test/test/schema/input_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ void main() {
..favorite_color.red = 225);
});

test('can be instantiated via create factory', () {
GReviewInput.create(
stars: 4,
commentary: "This was a great movie!",
favorite_color: GColorInput.create(red: 255, green: 123, blue: 225));
});

test('can be serialized and deserialized', () {
final input = GReviewInput((b) => b
..stars = 4
Expand Down
1 change: 1 addition & 0 deletions codegen/end_to_end_test_tristate/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ targets:
gql_build|schema_builder:
enabled: true
options:
vars_create_factories: true
tristate_optionals: true
enum_fallbacks:
LengthUnit: METER
Expand Down

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

3 changes: 2 additions & 1 deletion codegen/gql_build/lib/gql_build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ Builder schemaBuilder(
typeOverrideMap(options.config["type_overrides"]),
enumFallbackConfig(options.config),
generatePossibleTypesConfig(options.config),
triStateOptionalsConfig(options.config));
triStateOptionalsConfig(options.config),
varsCreateFactoriesConfig(options.config));

/// Builds an aggregate Serlializers object for [built_value]s
///
Expand Down
10 changes: 8 additions & 2 deletions codegen/gql_build/lib/src/schema_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ class SchemaBuilder implements Builder {
final EnumFallbackConfig enumFallbackConfig;
final bool generatePossibleTypesMap;
final TriStateValueConfig triStateValueConfig;
final bool generateVarsCreateFactories;

SchemaBuilder(this.typeOverrides, this.enumFallbackConfig,
this.generatePossibleTypesMap, this.triStateValueConfig);
SchemaBuilder(
this.typeOverrides,
this.enumFallbackConfig,
this.generatePossibleTypesMap,
this.triStateValueConfig,
this.generateVarsCreateFactories);

@override
Map<String, List<String>> get buildExtensions => {
Expand Down Expand Up @@ -49,6 +54,7 @@ class SchemaBuilder implements Builder {
generatePossibleTypesMap: generatePossibleTypesMap,
allocator: allocator,
triStateValueConfig: triStateValueConfig,
generateVarsCreateFactories: generateVarsCreateFactories,
);

return writeDocument(
Expand Down
12 changes: 9 additions & 3 deletions codegen/gql_code_builder/lib/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ Library buildSchemaLibrary(SourceNode schemaSource, String partUrl,
Map<String, Reference> typeOverrides, EnumFallbackConfig enumFallbackConfig,
{bool generatePossibleTypesMap = false,
Allocator? allocator,
TriStateValueConfig triStateValueConfig = TriStateValueConfig.never}) {
final lib = buildSchema(schemaSource, typeOverrides, enumFallbackConfig,
allocator ?? Allocator(), triStateValueConfig) as Library;
TriStateValueConfig triStateValueConfig = TriStateValueConfig.never,
bool generateVarsCreateFactories = false}) {
final lib = buildSchema(
schemaSource,
typeOverrides,
enumFallbackConfig,
allocator ?? Allocator(),
triStateValueConfig,
generateVarsCreateFactories) as Library;

final Code? possibleTypes;
if (generatePossibleTypesMap && lib.body.isNotEmpty) {
Expand Down
17 changes: 13 additions & 4 deletions codegen/gql_code_builder/lib/src/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Spec? buildSchema(
EnumFallbackConfig enumFallbackConfig,
Allocator allocator,
TriStateValueConfig triStateValueConfig,
bool generateVarsCreateFactories,
) =>
schemaSource.document
.accept(
Expand All @@ -24,6 +25,7 @@ Spec? buildSchema(
enumFallbackConfig,
allocator,
triStateValueConfig,
generateVarsCreateFactories,
),
)
?.first;
Expand All @@ -36,8 +38,15 @@ class _SchemaBuilderVisitor extends SimpleVisitor<List<Spec>?> {
final Allocator allocator;
final TriStateValueConfig triStateValueConfig;

const _SchemaBuilderVisitor(this.schemaSource, this.typeOverrides,
this.enumFallbackConfig, this.allocator, this.triStateValueConfig);
final bool generateVarsCreateFactories;

const _SchemaBuilderVisitor(
this.schemaSource,
this.typeOverrides,
this.enumFallbackConfig,
this.allocator,
this.triStateValueConfig,
this.generateVarsCreateFactories);

@override
List<Spec> visitDocumentNode(
Expand All @@ -57,8 +66,8 @@ class _SchemaBuilderVisitor extends SimpleVisitor<List<Spec>?> {
List<Spec> visitInputObjectTypeDefinitionNode(
InputObjectTypeDefinitionNode node,
) {
final inputClass =
buildInputClass(node, schemaSource, typeOverrides, triStateValueConfig);
final inputClass = buildInputClass(node, schemaSource, typeOverrides,
triStateValueConfig, generateVarsCreateFactories);

return switch (triStateValueConfig) {
TriStateValueConfig.never => [inputClass],
Expand Down
21 changes: 21 additions & 0 deletions codegen/gql_code_builder/lib/src/schema/input.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "package:code_builder/code_builder.dart";
import "package:gql/ast.dart";
import "package:gql_code_builder/src/config/tristate_optionals_config.dart";
import "package:gql_code_builder/src/required_vars_constructor.dart";
import "package:gql_code_builder/src/tristate_optionals.dart";

import "../../source.dart";
Expand All @@ -12,6 +13,7 @@ List<Class> buildInputClasses(
Map<String, Reference> typeOverrides,
Allocator allocator,
TriStateValueConfig triStateValueConfig,
bool generateVarsCreateFactories,
) =>
schemaSource.document.definitions
.whereType<InputObjectTypeDefinitionNode>()
Expand All @@ -21,6 +23,7 @@ List<Class> buildInputClasses(
schemaSource,
typeOverrides,
triStateValueConfig,
generateVarsCreateFactories,
);
final serializer = nullAwareJsonSerializerClass(
inputClass, allocator, schemaSource, typeOverrides);
Expand All @@ -32,6 +35,7 @@ Class buildInputClass(
SourceNode schemaSource,
Map<String, Reference> typeOverrides,
TriStateValueConfig triStateValueConfig,
bool generateVarsCreateFactories,
) =>
builtClass(
name: node.name.value,
Expand All @@ -46,6 +50,23 @@ Class buildInputClass(
),
hasCustomSerializer:
triStateValueConfig == TriStateValueConfig.onAllNullableFields,
constructors: [
if (generateVarsCreateFactories)
builtCreateConstructor(
name: node.name.value,
getters: node.fields.map<Method>(
(node) => buildOptionalGetter(
nameNode: node.name,
typeNode: node.type,
schemaSource: schemaSource,
typeOverrides: typeOverrides,
useTriStateValueForNullableTypes: triStateValueConfig,
),
),
schemaSource: schemaSource,
typeOverrides: typeOverrides,
),
],
initializers: {
if (triStateValueConfig == TriStateValueConfig.onAllNullableFields)
..._inputClassValueInitializers(node)
Expand Down

0 comments on commit bfafca5

Please sign in to comment.