Skip to content

Commit c411e3c

Browse files
authored
Merge pull request #205 from jakub-bacic/issue-204
Fix generating result types with nullable directives
2 parents 7c9aea5 + da5933a commit c411e3c

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fixed generating operation string for nested inline fragments.
66
- Removed scalars module. Changed generated models and client to use annotated types for custom scalars. Removed `scalars_module_name` option. Removed `generate_scalars_module`, `generate_scalars_cod`, `generate_scalar_annotation` and `generate_scalar_imports` plugin hooks.
77
- Removed pydantic warnings for fields with `model_` prefix.
8+
- Fixed generating result types with nullable directives.
89

910

1011
## 0.8.0 (2023-08-22)

ariadne_codegen/client_generators/result_fields.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def parse_operation_field(
6161
typename_values: Optional[List[str]] = None,
6262
custom_scalars: Optional[Dict[str, ScalarData]] = None,
6363
fragments_definitions: Optional[Dict[str, FragmentDefinitionNode]] = None,
64-
) -> Tuple[Annotation, List[FieldNames]]:
64+
) -> Tuple[Annotation, Optional[ast.expr], List[FieldNames]]:
6565
if field.name and field.name.value == TYPENAME_FIELD_NAME and typename_values:
66-
return generate_typename_annotation(typename_values), []
66+
return generate_typename_annotation(typename_values), None, []
6767

68+
default_value: Optional[ast.expr] = None
6869
annotation, field_types_names = parse_operation_field_type(
6970
schema=schema,
7071
field=field,
@@ -83,7 +84,8 @@ def parse_operation_field(
8384
directives_names = [d.name.value for d in directives]
8485
if any(n in nullable_directives for n in directives_names):
8586
annotation = generate_nullable_annotation(annotation)
86-
return annotation, field_types_names
87+
default_value = generate_constant(None)
88+
return annotation, default_value, field_types_names
8789

8890

8991
def generate_typename_annotation(typename_values: List[str]) -> ast.Subscript:

ariadne_codegen/client_generators/result_types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _parse_type_definition(
246246
field_name = self._get_field_name(field)
247247
name = self._process_field_name(field_name, field=field)
248248
field_definition = self._get_field_from_schema(type_name, field.name.value)
249-
annotation, field_types_names = parse_operation_field(
249+
annotation, default_value, field_types_names = parse_operation_field(
250250
schema=self.schema,
251251
field=field,
252252
type_=cast(CodegenResultFieldType, field_definition.type),
@@ -261,6 +261,7 @@ def _parse_type_definition(
261261
target=name,
262262
annotation=annotation,
263263
lineno=lineno,
264+
value=default_value,
264265
)
265266
field_implementation = self._process_field_implementation(
266267
field_implementation, field_schema_name=field_name, field=field

tests/client_generators/result_types_generator/test_directives.py

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def test_generator_returns_module_with_handled_skip_and_include_directives(direc
185185
value=ast.Name(id=OPTIONAL),
186186
slice=ast.Name(id='"CustomQueryQuery3Field1"'),
187187
),
188+
value=ast.Constant(value=None),
188189
simple=1,
189190
)
190191
generator = ResultTypesGenerator(

tests/client_generators/test_result_fields.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
def test_parse_operation_field_returns_optional_annotation_if_given_nullable_directive(
7777
directive, type_, expected_annotation
7878
):
79-
annotation, _ = parse_operation_field(
79+
annotation, _, _ = parse_operation_field(
8080
schema=GraphQLSchema(),
8181
field=FieldNode(),
8282
type_=type_,
@@ -92,7 +92,7 @@ def test_parse_operation_field_returns_typename_annotation_with_multiple_values(
9292
slice=ast.Tuple(elts=[ast.Name(id='"TypeA"'), ast.Name(id='"TypeB"')]),
9393
)
9494

95-
annotation, _ = parse_operation_field(
95+
annotation, _, _ = parse_operation_field(
9696
schema=GraphQLSchema(),
9797
field=FieldNode(name=NameNode(value=TYPENAME_FIELD_NAME)),
9898
type_=GraphQLNonNull(GraphQLScalarType("String")),
@@ -107,7 +107,7 @@ def test_parse_operation_field_returns_typename_annotation_with_single_value():
107107
value=ast.Name(id=LITERAL), slice=ast.Name(id='"TypeA"')
108108
)
109109

110-
annotation, _ = parse_operation_field(
110+
annotation, _, _ = parse_operation_field(
111111
schema=GraphQLSchema(),
112112
field=FieldNode(name=NameNode(value=TYPENAME_FIELD_NAME)),
113113
type_=GraphQLNonNull(GraphQLScalarType("String")),
@@ -145,7 +145,7 @@ def test_parse_operation_field_returns_annotation_with_annotated_nested_unions()
145145
),
146146
)
147147

148-
annotation, _ = parse_operation_field(
148+
annotation, _, _ = parse_operation_field(
149149
schema=GraphQLSchema(),
150150
field=FieldNode(name=NameNode(value="unionField")),
151151
type_=GraphQLUnionType(

0 commit comments

Comments
 (0)