Skip to content

Commit 745ab32

Browse files
committed
fix: allow support for str|None or Union[str|None] in input type
1 parent 5a0eebb commit 745ab32

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

graphene_pydantic/inputobjecttype.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ def construct_fields(
4646
fields = {}
4747
for name, field in fields_to_convert:
4848

49+
# Graphql does not accept union as input. Refer https://github.com/graphql/graphql-spec/issues/488
4950
if isinstance(getattr(field, "annotation", None), UnionType):
50-
field.annotation = T.Union[getattr(field, "annotation", None).__args__]
51+
union_types = field.annotation.__args__
52+
if type(None) not in union_types or len(union_types) > 2:
53+
continue
54+
# But str|None or Union[str, None] is valid input equivalent to Optional[str]
55+
base_type = list(filter(lambda x: x is not type(None), union_types)).pop()
56+
field.annotation = T.Optional[base_type]
5157

5258
converted = convert_pydantic_input_field(
5359
field, registry, parent_type=obj_type, model=model

0 commit comments

Comments
 (0)