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

Commit e89095d

Browse files
JozefielJozef Volak
andauthored
[graphql-pydantic-converter] Migration to pydantic v2 (#47)
* [graphql-pydantic-converter] Migrate to pydantic v2 --------- Co-authored-by: Jozef Volak <jvolak@frinx.io>
1 parent cb8cd27 commit e89095d

7 files changed

Lines changed: 625 additions & 452 deletions

File tree

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING
55

66
from pydantic import BaseModel
7-
from pydantic import Extra
7+
from pydantic import ConfigDict
88

99
if TYPE_CHECKING:
1010
from typing import Any
@@ -87,18 +87,26 @@ class GraphQLType(ENUM):
8787
class Subscription(BaseModel):
8888
...
8989

90-
class Config:
91-
extra = Extra.forbid
90+
model_config = ConfigDict(
91+
extra='forbid'
92+
)
9293

9394

9495
class Interface(BaseModel):
9596
...
9697

97-
class Config:
98-
extra = Extra.forbid
98+
model_config = ConfigDict(
99+
extra='forbid'
100+
)
99101

100102

101103
class Payload(BaseModel):
104+
105+
model_config = ConfigDict(
106+
extra='forbid',
107+
strict=True
108+
)
109+
102110
def dict_to_custom_string(self, any_object: Any) -> str:
103111
pairs = []
104112
match any_object:
@@ -120,15 +128,17 @@ def dict_to_custom_string(self, any_object: Any) -> str:
120128
pairs.append(f'{key}')
121129
return ' '.join(pairs)
122130

123-
class Config:
124-
extra = Extra.forbid
125-
strict = True
126-
127131
def render(self) -> str:
128-
return self.dict_to_custom_string(self.dict(exclude_none=True, by_alias=True))
132+
return self.dict_to_custom_string(self.model_dump(exclude_none=True, by_alias=True))
129133

130134

131135
class Input(BaseModel):
136+
137+
model_config = ConfigDict(
138+
extra='forbid',
139+
strict=True
140+
)
141+
132142
@staticmethod
133143
def _parse_enum(value: Enum) -> str:
134144
return f'{value.name}'
@@ -191,20 +201,17 @@ def dict_to_custom_string(self, any_object: Any) -> str:
191201
pairs.append(f'{key}: {self.parse_inputs(value)}')
192202
return ', '.join(pairs)
193203

194-
class Config:
195-
extra = Extra.forbid
196-
strict = True
197-
198204
def render(self) -> str:
199-
return self.dict_to_custom_string(self.dict(exclude_none=True, by_alias=True))
205+
return self.dict_to_custom_string(self.model_dump(exclude_none=True, by_alias=True))
200206

201207

202208
class Mutation(BaseModel):
203209
payload: Payload | bool
204210
_name: str
205211

206-
class Config:
207-
extra = Extra.forbid
212+
model_config = ConfigDict(
213+
extra='forbid'
214+
)
208215

209216
def dict_to_custom_string(self, value: dict[str, Any]) -> str:
210217
if isinstance(value, Input):
@@ -226,7 +233,7 @@ def render(self) -> str:
226233
if k not in ['_name', 'payload']:
227234
variables.append(f' {k}: {self.dict_to_custom_string( value)}')
228235
variable = ', '.join(variables)
229-
name: str = self._name.__getattribute__('default')
236+
name: str = self._name
230237

231238
return f'mutation {{ { name } ({variable}) {payload} }}'
232239

@@ -235,8 +242,9 @@ class Query(BaseModel):
235242
payload: Payload
236243
_name: str
237244

238-
class Config:
239-
extra = Extra.forbid
245+
model_config = ConfigDict(
246+
extra='forbid'
247+
)
240248

241249
@staticmethod
242250
def _parse_enum(value: Enum) -> str:
@@ -323,10 +331,10 @@ def dict_to_custom_string(self, any_object: Any) -> str:
323331

324332
def render(self) -> str:
325333
variable: str = self.dict_to_custom_input(
326-
self.dict(exclude_none=True, exclude={'_name', 'payload'}, by_alias=True)
334+
self.model_dump(exclude_none=True, exclude={'_name', 'payload'}, by_alias=True)
327335
)
328-
payload: str = self.dict_to_custom_string(self.payload.dict(exclude_none=True, by_alias=True))
329-
name: str = self._name.__getattribute__('default')
336+
payload: str = self.dict_to_custom_string(self.payload.model_dump(exclude_none=True, by_alias=True))
337+
name: str = self._name
330338
if variable:
331339
variable = f' ( {variable} )'
332340
return f'{{ { name }{variable} {{ {payload} }} }}'

0 commit comments

Comments
 (0)