7
7
import typing as T
8
8
import uuid
9
9
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
+ )
11
22
from graphene .types .base import BaseType
12
23
from graphene .types .datetime import Date , DateTime , Time
13
24
from pydantic import BaseModel
14
- from pydantic .fields import Field as PydanticField
25
+ from pydantic .fields import ModelField
15
26
16
- from ..util import construct_union_class_name
17
27
from .registry import Registry
28
+ from .util import construct_union_class_name
18
29
19
30
try :
20
31
# Pydantic pre-1.0
@@ -72,8 +83,38 @@ def _get_field(root, _info):
72
83
return _get_field
73
84
74
85
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
+
75
116
def convert_pydantic_field (
76
- field : PydanticField ,
117
+ field : ModelField ,
77
118
registry : Registry ,
78
119
parent_type : T .Type = None ,
79
120
model : T .Type [BaseModel ] = None ,
@@ -83,6 +124,7 @@ def convert_pydantic_field(
83
124
Convert a Pydantic model field into a Graphene type field that we can add
84
125
to the generated Graphene data model type.
85
126
"""
127
+ print ("convert_pydantic_field" , type (field ))
86
128
declared_type = getattr (field , "type_" , None )
87
129
field_kwargs .setdefault (
88
130
"type" ,
@@ -103,8 +145,8 @@ def convert_pydantic_field(
103
145
104
146
def convert_pydantic_type (
105
147
type_ : T .Type ,
106
- field : PydanticField ,
107
- registry : Registry = None ,
148
+ field : ModelField ,
149
+ registry : Registry ,
108
150
parent_type : T .Type = None ,
109
151
model : T .Type [BaseModel ] = None ,
110
152
) -> BaseType : # noqa: C901
@@ -127,8 +169,8 @@ def convert_pydantic_type(
127
169
128
170
def find_graphene_type (
129
171
type_ : T .Type ,
130
- field : PydanticField ,
131
- registry : Registry = None ,
172
+ field : ModelField ,
173
+ registry : Registry ,
132
174
parent_type : T .Type = None ,
133
175
model : T .Type [BaseModel ] = None ,
134
176
) -> BaseType : # noqa: C901
@@ -200,8 +242,8 @@ def find_graphene_type(
200
242
201
243
def convert_generic_python_type (
202
244
type_ : T .Type ,
203
- field : PydanticField ,
204
- registry : Registry = None ,
245
+ field : ModelField ,
246
+ registry : Registry ,
205
247
parent_type : T .Type = None ,
206
248
model : T .Type [BaseModel ] = None ,
207
249
) -> BaseType : # noqa: C901
@@ -253,8 +295,8 @@ def convert_generic_python_type(
253
295
254
296
def convert_union_type (
255
297
type_ : T .Type ,
256
- field : PydanticField ,
257
- registry : Registry = None ,
298
+ field : ModelField ,
299
+ registry : Registry ,
258
300
parent_type : T .Type = None ,
259
301
model : T .Type [BaseModel ] = None ,
260
302
):
0 commit comments