Skip to content

Commit

Permalink
Merge pull request #292 from salmannotkhan/fix/include-directive-resu…
Browse files Browse the repository at this point in the history
…lt-type

Fix: Include directive result type when using `convert_to_snake_case` option
  • Loading branch information
rafalp committed Apr 25, 2024
2 parents 05353b7 + 8063b7e commit cb576f8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added `ClientForwardRefsPlugin` to standard plugins.
- Re-added `model_rebuild` calls for input types with forward references.
- Fixed fragments on interfaces being omitted from generated client.
- Fixed `@Include` directive result type when using `convert_to_snake_case` option.


## 0.13.0 (2024-03-4)
Expand Down
1 change: 1 addition & 0 deletions ariadne_codegen/client_generators/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
PYDANTIC_MODULE = "pydantic"
FIELD_CLASS = "Field"
ALIAS_KEYWORD = "alias"
DEFAULT_KEYWORD = "default"
DISCRIMINATOR_KEYWORD = "discriminator"
MODEL_VALIDATE_METHOD = "model_validate"
PLAIN_SERIALIZER = "PlainSerializer"
Expand Down
6 changes: 6 additions & 0 deletions ariadne_codegen/client_generators/result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
ANY,
BASE_MODEL_CLASS_NAME,
BEFORE_VALIDATOR,
DEFAULT_KEYWORD,
DISCRIMINATOR_KEYWORD,
FIELD_CLASS,
LIST,
Expand Down Expand Up @@ -437,6 +438,11 @@ def _process_field_implementation(
if is_union(field_implementation.annotation):
keywords[DISCRIMINATOR_KEYWORD] = generate_constant(TYPENAME_ALIAS)

if keywords and isinstance(field_implementation.value, ast.Constant):
keywords[DEFAULT_KEYWORD] = generate_constant(
field_implementation.value.value
)

if keywords:
field_implementation.value = generate_pydantic_field(keywords)

Expand Down
1 change: 1 addition & 0 deletions tests/client_generators/result_types_generator/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
field1: CustomType1!
field2: CustomType1!
field3: CustomType1
camelCaseField: CustomType1
}
enum CustomEnum {
Expand Down
47 changes: 47 additions & 0 deletions tests/client_generators/result_types_generator/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ariadne_codegen.client_generators.constants import (
BASE_MODEL_CLASS_NAME,
FIELD_CLASS,
INCLUDE_DIRECTIVE_NAME,
MIXIN_FROM_NAME,
MIXIN_IMPORT_NAME,
Expand Down Expand Up @@ -215,3 +216,49 @@ def test_generator_returns_module_with_handled_skip_and_include_directives(direc
assert class_def.name == "CustomQueryQuery3"
assert compare_ast(class_def.body[0], expected_field_def_1)
assert compare_ast(class_def.body[2], expected_field_def_2)


@pytest.mark.parametrize("directive", [INCLUDE_DIRECTIVE_NAME, SKIP_DIRECTIVE_NAME])
def test_generator_returns_module_with_handled_skip_and_include_directives_snake_case(
directive,
):
query_str = f"""
query CustomQuery {{
query3 {{
camelCaseField @{directive} {{
fielda
}}
}}
}}
"""
expected_field_def_3 = ast.AnnAssign(
target=ast.Name(id="camel_case_field"),
annotation=ast.Subscript(
value=ast.Name(id=OPTIONAL),
slice=ast.Name(id='"CustomQueryQuery3CamelCaseField"'),
),
value=ast.Call(
func=ast.Name(id=FIELD_CLASS),
args=[],
keywords=[
ast.keyword(arg="alias", value=ast.Constant(value="camelCaseField")),
ast.keyword(arg="default", value=ast.Constant(value=None)),
],
),
simple=1,
)

generator = ResultTypesGenerator(
schema=build_ast_schema(parse(SCHEMA_STR)),
operation_definition=cast(
OperationDefinitionNode, parse(query_str).definitions[0]
),
enums_module_name="enums",
convert_to_snake_case=True,
)

module = generator.generate()

class_def = get_class_def(module, 1)
assert class_def.name == "CustomQueryQuery3"
assert compare_ast(class_def.body[0], expected_field_def_3)

0 comments on commit cb576f8

Please sign in to comment.