Skip to content

Commit 9ae7c0b

Browse files
Fix GraphQL enum renaming issue (#2642)
* add tests * use reference instead of type on enums * change test example
1 parent 3554396 commit 9ae7c0b

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/datamodel_code_generator/parser/graphql.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ def parse_field(
482482
data_type.reference = self.references[obj.name]
483483

484484
obj = graphql.assert_named_type(obj)
485-
data_type.type = obj.name
485+
if data_type.reference is None:
486+
data_type.type = obj.name
486487

487488
required = (not self.force_optional_for_required_fields) and (not final_data_type.is_optional)
488489

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# generated by datamodel-codegen:
2+
# filename: enum-casing.graphql
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from enum import Enum
8+
from typing import Literal, Optional
9+
10+
from pydantic import BaseModel, Field
11+
from typing_extensions import TypeAlias
12+
13+
Boolean: TypeAlias = bool
14+
"""
15+
The `Boolean` scalar type represents `true` or `false`.
16+
"""
17+
18+
19+
String: TypeAlias = str
20+
"""
21+
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
22+
"""
23+
24+
25+
class Lowercase(Enum):
26+
foo = 'foo'
27+
28+
29+
class EnumRef(BaseModel):
30+
bar: Lowercase
31+
typename__: Optional[Literal['EnumRef']] = Field('EnumRef', alias='__typename')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum lowercase {
2+
foo
3+
}
4+
5+
type EnumRef {
6+
bar: lowercase!
7+
}

tests/main/graphql/test_main_graphql.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ def test_main_graphql_field_aliases(output_file: Path) -> None:
102102
)
103103

104104

105+
@pytest.mark.skipif(
106+
black.__version__.split(".")[0] == "19",
107+
reason="Installed black doesn't support the old style",
108+
)
109+
def test_main_graphql_enum_casing(output_file: Path) -> None:
110+
"""Test GraphQL code generation with enums."""
111+
run_main_and_assert(
112+
input_path=GRAPHQL_DATA_PATH / "enum-casing.graphql",
113+
output_path=output_file,
114+
input_file_type="graphql",
115+
assert_func=assert_file_content,
116+
expected_file="enum_casing.py",
117+
)
118+
119+
105120
@pytest.mark.skipif(
106121
black.__version__.split(".")[0] == "19",
107122
reason="Installed black doesn't support the old style",

0 commit comments

Comments
 (0)