77import sys
88import typing as T
99import uuid
10+ from bson import ObjectId
1011
1112from graphene import (
1213 UUID ,
1920 List ,
2021 String ,
2122 Union ,
23+ JSONString ,
24+ ID
2225)
26+
2327from graphene .types .base import BaseType
2428from graphene .types .datetime import Date , DateTime , Time
2529from pydantic import BaseModel
4751else :
4852 SHAPE_MAPPING = T .cast (T .Tuple , (fields .SHAPE_MAPPING ,))
4953
50-
5154try :
5255 from graphene .types .decimal import Decimal as GrapheneDecimal
5356
5659 # graphene 2.1.5+ is required for Decimals
5760 DECIMAL_SUPPORTED = False
5861
59-
6062NONE_TYPE = None .__class__ # need to do this because mypy complains about type(None)
6163
6264
@@ -77,11 +79,11 @@ def _get_field(root, _info):
7779
7880
7981def convert_pydantic_input_field (
80- field : ModelField ,
81- registry : Registry ,
82- parent_type : T .Type = None ,
83- model : T .Type [BaseModel ] = None ,
84- ** field_kwargs ,
82+ field : ModelField ,
83+ registry : Registry ,
84+ parent_type : T .Type = None ,
85+ model : T .Type [BaseModel ] = None ,
86+ ** field_kwargs ,
8587) -> InputField :
8688 """
8789 Convert a Pydantic model field into a Graphene type field that we can add
@@ -106,11 +108,11 @@ def convert_pydantic_input_field(
106108
107109
108110def convert_pydantic_field (
109- field : ModelField ,
110- registry : Registry ,
111- parent_type : T .Type = None ,
112- model : T .Type [BaseModel ] = None ,
113- ** field_kwargs ,
111+ field : ModelField ,
112+ registry : Registry ,
113+ parent_type : T .Type = None ,
114+ model : T .Type [BaseModel ] = None ,
115+ ** field_kwargs ,
114116) -> Field :
115117 """
116118 Convert a Pydantic model field into a Graphene type field that we can add
@@ -140,11 +142,11 @@ def convert_pydantic_field(
140142
141143
142144def convert_pydantic_type (
143- type_ : T .Type ,
144- field : ModelField ,
145- registry : Registry ,
146- parent_type : T .Type = None ,
147- model : T .Type [BaseModel ] = None ,
145+ type_ : T .Type ,
146+ field : ModelField ,
147+ registry : Registry ,
148+ parent_type : T .Type = None ,
149+ model : T .Type [BaseModel ] = None ,
148150) -> BaseType : # noqa: C901
149151 """
150152 Convert a Pydantic type to a Graphene Field type, including not just the
@@ -164,11 +166,11 @@ def convert_pydantic_type(
164166
165167
166168def find_graphene_type (
167- type_ : T .Type ,
168- field : ModelField ,
169- registry : Registry ,
170- parent_type : T .Type = None ,
171- model : T .Type [BaseModel ] = None ,
169+ type_ : T .Type ,
170+ field : ModelField ,
171+ registry : Registry ,
172+ parent_type : T .Type = None ,
173+ model : T .Type [BaseModel ] = None ,
172174) -> BaseType : # noqa: C901
173175 """
174176 Map a native Python type to a Graphene-supported Field type, where possible,
@@ -188,6 +190,10 @@ def find_graphene_type(
188190 return Boolean
189191 elif type_ == float :
190192 return Float
193+ elif type_ == ObjectId :
194+ return ID
195+ elif type_ == dict :
196+ return JSONString
191197 elif type_ == decimal .Decimal :
192198 return GrapheneDecimal if DECIMAL_SUPPORTED else Float
193199 elif type_ == int :
@@ -198,8 +204,8 @@ def find_graphene_type(
198204 elif registry and registry .get_type_for_model (type_ ):
199205 return registry .get_type_for_model (type_ )
200206 elif registry and (
201- isinstance (type_ , BaseModel )
202- or (inspect .isclass (type_ ) and issubclass (type_ , BaseModel ))
207+ isinstance (type_ , BaseModel )
208+ or (inspect .isclass (type_ ) and issubclass (type_ , BaseModel ))
203209 ):
204210 # If it's a Pydantic model that hasn't yet been wrapped with a ObjectType,
205211 # we can put a placeholder in and request that `resolve_placeholders()`
@@ -243,11 +249,11 @@ def find_graphene_type(
243249
244250
245251def convert_generic_python_type (
246- type_ : T .Type ,
247- field : ModelField ,
248- registry : Registry ,
249- parent_type : T .Type = None ,
250- model : T .Type [BaseModel ] = None ,
252+ type_ : T .Type ,
253+ field : ModelField ,
254+ registry : Registry ,
255+ parent_type : T .Type = None ,
256+ model : T .Type [BaseModel ] = None ,
251257) -> BaseType : # noqa: C901
252258 """
253259 Convert annotated Python generic types into the most appropriate Graphene
@@ -269,17 +275,17 @@ def convert_generic_python_type(
269275 type_ , field , registry , parent_type = parent_type , model = model
270276 )
271277 elif (
272- origin
273- in (
274- T .Tuple ,
275- T .List ,
276- T .Set ,
277- T .Collection ,
278- T .Iterable ,
279- list ,
280- set ,
281- )
282- or issubclass (origin , collections .abc .Sequence )
278+ origin
279+ in (
280+ T .Tuple ,
281+ T .List ,
282+ T .Set ,
283+ T .Collection ,
284+ T .Iterable ,
285+ list ,
286+ set ,
287+ )
288+ or issubclass (origin , collections .abc .Sequence )
283289 ):
284290 # TODO: find a better way of divining that the origin is sequence-like
285291 inner_types = getattr (type_ , "__args__" , [])
@@ -296,19 +302,19 @@ def convert_generic_python_type(
296302 )
297303 )
298304 elif origin in (T .Dict , T .Mapping , collections .OrderedDict , dict ) or issubclass (
299- origin , collections .abc .Mapping
305+ origin , collections .abc .Mapping
300306 ):
301307 raise ConversionError ("Don't know how to handle mappings in Graphene" )
302308 else :
303309 raise ConversionError (f"Don't know how to handle { type_ } (generic: { origin } )" )
304310
305311
306312def convert_union_type (
307- type_ : T .Type ,
308- field : ModelField ,
309- registry : Registry ,
310- parent_type : T .Type = None ,
311- model : T .Type [BaseModel ] = None ,
313+ type_ : T .Type ,
314+ field : ModelField ,
315+ registry : Registry ,
316+ parent_type : T .Type = None ,
317+ model : T .Type [BaseModel ] = None ,
312318):
313319 """
314320 Convert an annotated Python Union type into a Graphene Union.
@@ -337,11 +343,11 @@ def convert_union_type(
337343
338344
339345def convert_literal_type (
340- type_ : T .Type ,
341- field : ModelField ,
342- registry : Registry ,
343- parent_type : T .Type = None ,
344- model : T .Type [BaseModel ] = None ,
346+ type_ : T .Type ,
347+ field : ModelField ,
348+ registry : Registry ,
349+ parent_type : T .Type = None ,
350+ model : T .Type [BaseModel ] = None ,
345351):
346352 """
347353 Convert an annotated Python Literal type into a Graphene Scalar or Union of Scalars.
0 commit comments