Skip to content

Commit

Permalink
fix(gql_code_builder): Fixing nullables and type overrides for vars c…
Browse files Browse the repository at this point in the history
…reate factories (#444)
  • Loading branch information
caffeineflo authored Jan 27, 2024
1 parent 650c517 commit fa44d74
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 23 deletions.

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.

8 changes: 4 additions & 4 deletions codegen/gql_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: gql_build
version: 0.11.0+1
description: Useful builders for your GraphQL SDL and documents. Based on package:gql_code_builder and package:build
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
analyzer: '>=4.6.0 <7.0.0'
build: ^2.1.0
built_collection: ^5.0.0
Expand All @@ -18,10 +18,10 @@ dependencies:
path: ^1.8.0
yaml: ^3.1.0
gql_tristate_value: ^1.0.0
dev_dependencies:
dev_dependencies:
build_test: ^2.0.0
gql_pedantic: ^1.0.2
topics:
topics:
- graphql
- gql
- codegen
19 changes: 12 additions & 7 deletions codegen/gql_code_builder/lib/src/required_vars_constructor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Constructor builtCreateConstructor({
required String name,
required Iterable<Method> getters,
required SourceNode schemaSource,
Map<String, Reference> typeOverrides = const {},
}) {
final className = builtClassName(name);

Expand All @@ -21,25 +22,29 @@ Constructor builtCreateConstructor({
);

// Create parameters for the factory constructor
final factoryParameters = filteredGetters.map(
(g) => Parameter(
final factoryParameters = filteredGetters.map((g) {
final isNullable = (g.returns! as TypeReference).isNullable ?? false;

return Parameter(
(b) => b
..name = g.name!
..named = true
..required = true
..required = !isNullable
..type = g.returns,
),
);
);
});

final assignments = filteredGetters.map((g) {
final typeDefinitionNode = getTypeDefinitionNode(
schemaSource.document, g.returns!.symbol!.replaceFirst("G", ""));
final isBuiltType = typeDefinitionNode is InputObjectTypeDefinitionNode ||
typeDefinitionNode is ScalarTypeDefinitionNode;

// Check if the type is nullable
final isTypeOverride = typeOverrides.values.contains(g.returns!);
final isNullable = (g.returns! as TypeReference).isNullable ?? false;
final assignment = isBuiltType

// If the type is a built type and not a TypeOverride, we need to call toBuilder() on it.
final assignment = isBuiltType && !isTypeOverride
? "${g.name} = ${g.name}${isNullable ? '?' : ''}.toBuilder()"
: "${g.name} = ${g.name}";

Expand Down
1 change: 1 addition & 0 deletions codegen/gql_code_builder/lib/var.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Library buildVarLibrary(
),
),
schemaSource: schemaSource,
typeOverrides: typeOverrides,
),
],
initializers: switch (useTriStateValueForNullableTypes) {
Expand Down
8 changes: 4 additions & 4 deletions codegen/gql_code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: gql_code_builder
version: 0.10.0
description: Dart code builders taking *.graphql documents and SDL to build useful classes.
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
analyzer: '>=4.6.0 <7.0.0'
built_collection: ^5.0.0
built_value: ^8.0.6
Expand All @@ -14,11 +14,11 @@ dependencies:
gql_exec: ^1.0.0
path: ^1.8.0
gql_tristate_value: ^1.0.0
dev_dependencies:
dev_dependencies:
build_runner: ^2.1.0
gql_pedantic: ^1.0.2
test: ^1.16.8
topics:
topics:
- graphql
- gql
- codegen

0 comments on commit fa44d74

Please sign in to comment.