77import typing as T
88import 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+ )
1122from graphene .types .base import BaseType
1223from graphene .types .datetime import Date , DateTime , Time
1324from 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
1727from .registry import Registry
28+ from .util import construct_union_class_name
1829
1930try :
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+
75116def 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
104146def 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
128170def 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
201243def 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
254296def 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):
0 commit comments