Skip to content

Commit e966ce0

Browse files
committed
fix: python11 Union using | syntax
1 parent f000a85 commit e966ce0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

graphene_pydantic/converters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import typing as T
99
import uuid
10+
from types import UnionType
1011

1112
import graphene
1213
from bson import ObjectId
@@ -91,6 +92,8 @@ def convert_pydantic_field(
9192
to the generated Graphene data model type.
9293
"""
9394
declared_type = getattr(field, "annotation", None)
95+
if isinstance(declared_type, UnionType):
96+
declared_type = T.Union[declared_type.__args__]
9497
field_kwargs.setdefault(
9598
"type" if GRAPHENE2 else "type_",
9699
convert_pydantic_type(

graphene_pydantic/inputobjecttype.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import typing as T
2+
from types import UnionType
23

34
import graphene
45
import pydantic
@@ -44,6 +45,14 @@ def construct_fields(
4445

4546
fields = {}
4647
for name, field in fields_to_convert:
48+
declared_type = getattr(field, "annotation", None)
49+
50+
# Ignore Union types from Input. Refer https://github.com/graphql/graphql-spec/issues/488
51+
if isinstance(declared_type, UnionType) or (
52+
hasattr(declared_type, "__origin__") and declared_type.__origin__ == T.Union
53+
):
54+
continue
55+
4756
converted = convert_pydantic_input_field(
4857
field, registry, parent_type=obj_type, model=model
4958
)

0 commit comments

Comments
 (0)