Skip to content

Commit 0927e22

Browse files
committed
Refactored inputobjectype and objecttype to share converters and registry.
Refactored Registry to be generic. Added convert_pydantic_input_field. Removed unused model param in Registry.register_object_field().
1 parent b505c1c commit 0927e22

19 files changed

+187
-757
lines changed

graphene_pydantic/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .inputobjecttype.types import PydanticInputObjectType
2-
from .objecttype.types import PydanticObjectType
1+
from .inputobjecttype import PydanticInputObjectType
2+
from .objecttype import PydanticObjectType
33

44
__all__ = ["PydanticObjectType", "PydanticInputObjectType"]

graphene_pydantic/objecttype/converters.py renamed to graphene_pydantic/converters.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
import typing as T
88
import uuid
99

10-
from graphene import UUID, Boolean, Enum, Field, Float, Int, List, String, Union
10+
from graphene import (
11+
UUID,
12+
Boolean,
13+
Enum,
14+
Field,
15+
Float,
16+
InputField,
17+
Int,
18+
List,
19+
String,
20+
Union,
21+
)
1122
from graphene.types.base import BaseType
1223
from graphene.types.datetime import Date, DateTime, Time
1324
from pydantic import BaseModel
14-
from pydantic.fields import Field as PydanticField
25+
from pydantic.fields import ModelField
1526

16-
from ..util import construct_union_class_name
1727
from .registry import Registry
28+
from .util import construct_union_class_name
1829

1930
try:
2031
# Pydantic pre-1.0
@@ -72,8 +83,38 @@ def _get_field(root, _info):
7283
return _get_field
7384

7485

86+
def convert_pydantic_input_field(
87+
field: ModelField,
88+
registry: Registry,
89+
parent_type: T.Type = None,
90+
model: T.Type[BaseModel] = None,
91+
**field_kwargs,
92+
) -> InputField:
93+
"""
94+
Convert a Pydantic model field into a Graphene type field that we can add
95+
to the generated Graphene data model type.
96+
"""
97+
print("convert_pydantic_input_field", type(field))
98+
declared_type = getattr(field, "type_", None)
99+
field_kwargs.setdefault(
100+
"type",
101+
convert_pydantic_type(
102+
declared_type, field, registry, parent_type=parent_type, model=model
103+
),
104+
)
105+
field_kwargs.setdefault("required", field.required)
106+
field_kwargs.setdefault("default_value", field.default)
107+
# TODO: find a better way to get a field's description. Some ideas include:
108+
# - hunt down the description from the field's schema, or the schema
109+
# from the field's base model
110+
# - maybe even (Sphinx-style) parse attribute documentation
111+
field_kwargs.setdefault("description", field.__doc__)
112+
113+
return InputField(**field_kwargs)
114+
115+
75116
def convert_pydantic_field(
76-
field: PydanticField,
117+
field: ModelField,
77118
registry: Registry,
78119
parent_type: T.Type = None,
79120
model: T.Type[BaseModel] = None,
@@ -83,6 +124,7 @@ def convert_pydantic_field(
83124
Convert a Pydantic model field into a Graphene type field that we can add
84125
to the generated Graphene data model type.
85126
"""
127+
print("convert_pydantic_field", type(field))
86128
declared_type = getattr(field, "type_", None)
87129
field_kwargs.setdefault(
88130
"type",
@@ -103,8 +145,8 @@ def convert_pydantic_field(
103145

104146
def convert_pydantic_type(
105147
type_: T.Type,
106-
field: PydanticField,
107-
registry: Registry = None,
148+
field: ModelField,
149+
registry: Registry,
108150
parent_type: T.Type = None,
109151
model: T.Type[BaseModel] = None,
110152
) -> BaseType: # noqa: C901
@@ -127,8 +169,8 @@ def convert_pydantic_type(
127169

128170
def find_graphene_type(
129171
type_: T.Type,
130-
field: PydanticField,
131-
registry: Registry = None,
172+
field: ModelField,
173+
registry: Registry,
132174
parent_type: T.Type = None,
133175
model: T.Type[BaseModel] = None,
134176
) -> BaseType: # noqa: C901
@@ -200,8 +242,8 @@ def find_graphene_type(
200242

201243
def convert_generic_python_type(
202244
type_: T.Type,
203-
field: PydanticField,
204-
registry: Registry = None,
245+
field: ModelField,
246+
registry: Registry,
205247
parent_type: T.Type = None,
206248
model: T.Type[BaseModel] = None,
207249
) -> BaseType: # noqa: C901
@@ -253,8 +295,8 @@ def convert_generic_python_type(
253295

254296
def convert_union_type(
255297
type_: T.Type,
256-
field: PydanticField,
257-
registry: Registry = None,
298+
field: ModelField,
299+
registry: Registry,
258300
parent_type: T.Type = None,
259301
model: T.Type[BaseModel] = None,
260302
):

graphene_pydantic/inputobjecttype/types.py renamed to graphene_pydantic/inputobjecttype.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import graphene
44
import pydantic
5+
from graphene import InputField
56
from graphene.types.inputobjecttype import InputObjectTypeOptions
67
from graphene.types.utils import yank_fields_from_attrs
78

8-
from .converters import convert_pydantic_field
9+
from .converters import convert_pydantic_input_field
910
from .registry import Placeholder, Registry, get_global_registry
1011

1112

@@ -23,7 +24,7 @@ def construct_fields(
2324
registry: Registry,
2425
only_fields: T.Tuple[str, ...],
2526
exclude_fields: T.Tuple[str, ...],
26-
) -> T.Dict[str, graphene.Field]:
27+
) -> T.Dict[str, graphene.InputField]:
2728
"""
2829
Construct all the fields for a PydanticInputObjectType.
2930
@@ -43,10 +44,10 @@ def construct_fields(
4344

4445
fields = {}
4546
for name, field in fields_to_convert:
46-
converted = convert_pydantic_field(
47+
converted = convert_pydantic_input_field(
4748
field, registry, parent_type=obj_type, model=model
4849
)
49-
registry.register_object_field(obj_type, name, field, model=model)
50+
registry.register_object_field(obj_type, name, field)
5051
fields[name] = converted
5152
return fields
5253

@@ -83,7 +84,7 @@ def __init_subclass_with_meta__(
8384
)
8485

8586
if not registry:
86-
registry = get_global_registry()
87+
registry = get_global_registry(PydanticInputObjectType)
8788

8889
pydantic_fields = yank_fields_from_attrs(
8990
construct_fields(
@@ -93,7 +94,7 @@ def __init_subclass_with_meta__(
9394
only_fields=only_fields,
9495
exclude_fields=exclude_fields,
9596
),
96-
_as=graphene.Field,
97+
_as=graphene.InputField,
9798
sort=False,
9899
)
99100

graphene_pydantic/inputobjecttype/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)