Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 966df07

Browse files
JozefielJozef Volak
and
Jozef Volak
authored
[graphql-pydantic-converter] Fix missing inputs in extracted form whe… (#54)
* [graphql-pydantic-converter] Fix missing inputs in extracted form when input as an alias --------- Co-authored-by: Jozef Volak <[email protected]>
1 parent 5a52a00 commit 966df07

File tree

5 files changed

+23
-34
lines changed

5 files changed

+23
-34
lines changed
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# 0.0.3
2-
- Add unit tests to project
3-
- Fix mutation input rendering for list items
4-
- Generate graphql schema request with custom typeOf depth
5-
6-
# 0.1.0
7-
- Changed default payload boolean value from True to False
8-
- In query or mutation must be defined response properties
9-
- Generating of pydantic dataclasses for response parsing
10-
11-
# 0.1.2
12-
- Ignore generating of private graphql schema objects
13-
- Add graphql_pydantic_converter.graphql_types.concatenate_queries function
14-
151
# 1.0.0
162
- Migration to pydantic v2
173

@@ -25,4 +11,8 @@
2511

2612
# 1.1.0
2713
- Support inline and extracted variables for mutation and query
28-
- Stringify fix
14+
- Stringify fix
15+
16+
# 1.1.1
17+
- Fix missing inputs for mutation and query render in extracted format
18+
- ENUM keys always uppercase

utils/graphql-pydantic-converter/graphql_pydantic_converter/graphql_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def _extracted_variables(self, payload: str) -> QueryForm:
212212
for key, value in model.items():
213213
if key == 'payload':
214214
continue
215-
if input_dict.get(key) is None:
215+
if input_dict.get(value.alias or key) is None:
216216
continue
217217
if value.json_schema_extra:
218218
extra_inputs: dict[str, str] = dict(**value.json_schema_extra)
@@ -340,7 +340,7 @@ def _extracted_variables(self, payload: str) -> QueryForm:
340340
for key, value in model.items():
341341
if key == 'payload':
342342
continue
343-
if input_dict.get(key) is None:
343+
if input_dict.get(value.alias or key) is None:
344344
continue
345345
if value.json_schema_extra:
346346
extra_inputs: dict[str, str] = dict(**value.json_schema_extra)

utils/graphql-pydantic-converter/graphql_pydantic_converter/schema_converter.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ def __import_classes(self, items: dict[str, Any], worker_list: list[Optional[str
206206
]
207207
self.__result += '\n'.join(imports)
208208

209-
self.__result += 'from pydantic import BaseModel\n'
210-
self.__result += 'from pydantic import Field\n'
211-
self.__result += 'from pydantic import PrivateAttr\n\n'
212-
213209
if items[GraphqlJsonParser.TypeKind.ENUM]:
214210
self.__result += kv_template.substitute(type=GraphqlJsonParser.ConverterMap.ENUM.value)
215211
if items[GraphqlJsonParser.TypeKind.INPUT_OBJECT]:
@@ -224,7 +220,9 @@ def __import_classes(self, items: dict[str, Any], worker_list: list[Optional[str
224220
self.__result += kv_template.substitute(type=GraphqlJsonParser.ConverterMap.QUERY.value)
225221
if GraphqlJsonParser.ConverterMap.SUBSCRIPTION in worker_list:
226222
self.__result += kv_template.substitute(type=GraphqlJsonParser.ConverterMap.SUBSCRIPTION.value)
227-
223+
self.__result += 'from pydantic import BaseModel\n'
224+
self.__result += 'from pydantic import Field\n'
225+
self.__result += 'from pydantic import PrivateAttr\n'
228226
self.__result += '\n'
229227

230228
def __extract_fields(self, of_type: OfType | Type, previous: list[Any]) -> list[Any]:
@@ -335,17 +333,19 @@ def __create_scalar(self, scalars: list[Type]) -> None:
335333
self.__ignore_enums.append(scalar.name)
336334

337335
def __create_enum(self, enums: list[Type]) -> None:
338-
kv_template = Template("$indent$name = '$name'\n")
336+
kv_template = Template("$indent$key = '$name'\n")
339337
for enum in enums:
340338

341339
if enum.name:
342340
if enum.name.startswith('__') and self.__ignore_private_objects:
343341
return
344-
345342
tmp_enum = [self.__class_template.substitute(name=enum.name, type='ENUM')]
346343
if enum.enum_values:
347344
for i in enum.enum_values:
348-
tmp_enum.append(kv_template.substitute(indent=self.__INDENT, name=i.name))
345+
key: Optional[str] = None
346+
if i.name:
347+
key = i.name.upper()
348+
tmp_enum.append(kv_template.substitute(indent=self.__INDENT, name=i.name, key=key))
349349
self.__result += (''.join(tmp_enum))
350350
if enum.name:
351351
self.__enums.append(enum.name)

utils/graphql-pydantic-converter/pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ packages = [{ include = "graphql_pydantic_converter" }]
1919
name = "graphql-pydantic-converter"
2020
description = "Convert pydantic schema to pydantic datamodel and build request from it"
2121
authors = ["Jozef Volak <[email protected]>"]
22-
version = '1.1.0'
22+
version = '1.1.1'
2323
readme = ["README.md", "CHANGELOG.md"]
2424
keywords = ["graphql", "pydantic"]
2525
license = "Apache 2.0"
@@ -35,11 +35,11 @@ reportGeneralTypeIssues = "none"
3535
line-length = 120
3636
extend-select = ['Q', 'RUF100', 'C90', 'UP', 'I', 'E', 'F', 'N', 'PL']
3737
fixable = ["A", "B", "C", "D", "E", "F", "G", "I", "N", "Q", "S", "T", "W", "ANN", "ARG", "BLE", "COM", "DJ", "DTZ", "EM", "ERA", "EXE", "FBT", "ICN", "INP", "ISC", "NPY", "PD", "PGH", "PIE", "PL", "PT", "PTH", "PYI", "RET", "RSE", "RUF", "SIM", "SLF", "TCH", "TID", "TRY", "UP", "YTT"]
38-
3938
target-version = "py310"
39+
exclude = ['tests']
4040

4141
[tool.ruff.isort]
42-
known-first-party = ['graphql_pydantic_converter']
42+
known-first-party = ['graphql_pydantic_converter', 'tests']
4343
force-single-line = true
4444

4545
[tool.ruff.flake8-quotes]

utils/graphql-pydantic-converter/tests/model.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
import typing
44

5-
from pydantic import BaseModel
6-
from pydantic import Field
7-
from pydantic import PrivateAttr
8-
95
from graphql_pydantic_converter.graphql_types import ENUM
106
from graphql_pydantic_converter.graphql_types import Input
117
from graphql_pydantic_converter.graphql_types import Interface
128
from graphql_pydantic_converter.graphql_types import Mutation
139
from graphql_pydantic_converter.graphql_types import Payload
1410
from graphql_pydantic_converter.graphql_types import Query
1511
from graphql_pydantic_converter.graphql_types import Subscription
12+
from pydantic import BaseModel
13+
from pydantic import Field
14+
from pydantic import PrivateAttr
1615

1716
String: typing.TypeAlias = str
1817
Int: typing.TypeAlias = int
@@ -34,8 +33,8 @@ class DeviceSize(ENUM):
3433

3534

3635
class GraphEdgeStatus(ENUM):
37-
ok = 'ok'
38-
unknown = 'unknown'
36+
OK = 'ok'
37+
UNKNOWN = 'unknown'
3938

4039

4140
class BaseGraphNode(Interface):

0 commit comments

Comments
 (0)