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

Commit fea9dd5

Browse files
Jozef VolakJozefiel
Jozef Volak
authored andcommitted
[graphql-pydantic-converter] Deduplication of interface response object
1 parent 182965d commit fea9dd5

File tree

8 files changed

+35
-27
lines changed

8 files changed

+35
-27
lines changed

inventory/python/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
start_period: 40s
3535

3636
converter:
37-
image: frinx/graphql-pydantic-converter:0.1.0
37+
image: frinx/graphql-pydantic-converter:0.1.1
3838
depends_on:
3939
inventory:
4040
condition: service_healthy

resource-manager/python/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ services:
2929
retries: 5
3030

3131
converter:
32-
image: frinx/graphql-pydantic-converter:0.1.0
32+
image: frinx/graphql-pydantic-converter:0.1.1
3333
depends_on:
3434
resource-manager:
3535
condition: service_healthy

schellar/python/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ services:
3535
start_period: 20s
3636

3737
converter:
38-
image: frinx/graphql-pydantic-converter:0.1.0
38+
image: frinx/graphql-pydantic-converter:0.1.1
3939
depends_on:
4040
schellar:
4141
condition: service_healthy

topology-discovery/python/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ services:
3333
VIEWER: viewer-1,viewer-2,viewer-3
3434

3535
converter:
36-
image: frinx/graphql-pydantic-converter:0.1.0
36+
image: frinx/graphql-pydantic-converter:0.1.1
3737
depends_on:
3838
topology-discovery:
3939
condition: service_healthy

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,30 +460,38 @@ def __create_specific_response(self, payload: Type) -> None:
460460
if field.name and field.type:
461461

462462
if field.args:
463+
464+
class_name = field.name[0].upper() + field.name[1:]
465+
463466
self.__result += self.__class_template.substitute(
464-
name=f'{field.name[0].upper() + field.name[1:] + payload.name}Response',
467+
name=f'{class_name + payload.name}Response',
465468
type='BaseModel',
466469
)
467470

468471
self.__refs += self.__refs_template.substitute(
469-
class_name=f'{field.name[0].upper() + field.name[1:] + payload.name}Response'
472+
class_name=f'{class_name + payload.name}Response'
470473
)
471474

472-
self.__result += kv_template.substitute(
473-
indent=self.__INDENT,
474-
name='data',
475-
val=f'typing.Optional[{field.name[0].upper() + field.name[1:]}Data]'
476-
)
475+
if class_name not in (self.__enums + self.__interfaces):
476+
self.__result += kv_template.substitute(
477+
indent=self.__INDENT,
478+
name='data',
479+
val=f'typing.Optional[{class_name}Data]'
480+
)
481+
else:
482+
self.__result += kv_template.substitute(
483+
indent=self.__INDENT,
484+
name='data',
485+
val=f'typing.Optional[{class_name}]'
486+
)
477487

478488
self.__result += kv_template.substitute(
479489
indent=self.__INDENT,
480490
name='errors',
481491
val='typing.Optional[typing.Any]'
482492
)
483493

484-
class_name = field.name[0].upper() + field.name[1:]
485-
486-
if class_name not in self.__interfaces:
494+
if class_name not in (self.__enums + self.__interfaces):
487495
self.__result += self.__class_template.substitute(
488496
name=f'{class_name}Data',
489497
type='BaseModel',

utils/graphql-pydantic-converter/pyproject.toml

Lines changed: 1 addition & 1 deletion
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 = '0.1.0'
22+
version = '0.1.1'
2323
readme = ["README.md", "CHANGELOG.md"]
2424
keywords = ["graphql", "pydantic"]
2525
license = "Apache 2.0"

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class UpdateBlueprintMutation(Mutation):
166166
payload: UpdateBlueprintPayload
167167

168168

169-
class AddBlueprintResponse(BaseModel):
169+
class AddBlueprintMutationResponse(BaseModel):
170170
data: typing.Optional[AddBlueprintData]
171171
errors: typing.Optional[typing.Any]
172172

@@ -175,7 +175,7 @@ class AddBlueprintData(BaseModel):
175175
add_blueprint: AddBlueprintPayloadPayload = Field(alias='addBlueprint')
176176

177177

178-
class DeleteBlueprintResponse(BaseModel):
178+
class DeleteBlueprintMutationResponse(BaseModel):
179179
data: typing.Optional[DeleteBlueprintData]
180180
errors: typing.Optional[typing.Any]
181181

@@ -184,7 +184,7 @@ class DeleteBlueprintData(BaseModel):
184184
delete_blueprint: DeleteBlueprintPayloadPayload = Field(alias='deleteBlueprint')
185185

186186

187-
class UpdateBlueprintResponse(BaseModel):
187+
class UpdateBlueprintMutationResponse(BaseModel):
188188
data: typing.Optional[UpdateBlueprintData]
189189
errors: typing.Optional[typing.Any]
190190

@@ -228,7 +228,7 @@ class BlueprintsQuery(Query):
228228
payload: BlueprintConnection
229229

230230

231-
class BlueprintsResponse(BaseModel):
231+
class BlueprintsQueryResponse(BaseModel):
232232
data: typing.Optional[BlueprintsData]
233233
errors: typing.Optional[typing.Any]
234234

@@ -245,7 +245,7 @@ class UniconfigShellSubscription(Subscription):
245245
payload: Boolean
246246

247247

248-
class UniconfigShellResponse(BaseModel):
248+
class UniconfigShellSubscriptionResponse(BaseModel):
249249
data: typing.Optional[UniconfigShellData]
250250
errors: typing.Optional[typing.Any]
251251

@@ -283,21 +283,21 @@ class UpdateBlueprintPayloadPayload(BaseModel):
283283
AddBlueprintMutation.update_forward_refs()
284284
DeleteBlueprintMutation.update_forward_refs()
285285
UpdateBlueprintMutation.update_forward_refs()
286-
AddBlueprintResponse.update_forward_refs()
286+
AddBlueprintMutationResponse.update_forward_refs()
287287
AddBlueprintData.update_forward_refs()
288-
DeleteBlueprintResponse.update_forward_refs()
288+
DeleteBlueprintMutationResponse.update_forward_refs()
289289
DeleteBlueprintData.update_forward_refs()
290-
UpdateBlueprintResponse.update_forward_refs()
290+
UpdateBlueprintMutationResponse.update_forward_refs()
291291
UpdateBlueprintData.update_forward_refs()
292292
GraphNodeInterface.update_forward_refs()
293293
GraphNodeInterfacePayload.update_forward_refs()
294294
PageInfo.update_forward_refs()
295295
PageInfoPayload.update_forward_refs()
296296
BlueprintsQuery.update_forward_refs()
297-
BlueprintsResponse.update_forward_refs()
297+
BlueprintsQueryResponse.update_forward_refs()
298298
BlueprintsData.update_forward_refs()
299299
UniconfigShellSubscription.update_forward_refs()
300-
UniconfigShellResponse.update_forward_refs()
300+
UniconfigShellSubscriptionResponse.update_forward_refs()
301301
UniconfigShellData.update_forward_refs()
302302
UpdateBlueprintPayload.update_forward_refs()
303303
UpdateBlueprintPayloadPayload.update_forward_refs()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_render_input_advanced(self) -> None:
175175
assert reference == query_render
176176

177177
def test_parse_response(self) -> None:
178-
from model import BlueprintsResponse
178+
from model import BlueprintsQueryResponse
179179

180180
response: typing.Any = {
181181
'data': {
@@ -192,7 +192,7 @@ def test_parse_response(self) -> None:
192192
}
193193
}
194194

195-
response = BlueprintsResponse(**response)
195+
response = BlueprintsQueryResponse(**response)
196196

197197
assert 'cli_device_import' == response.data.blueprints.edges[0].node.name
198198

0 commit comments

Comments
 (0)