Skip to content

Commit b8dce09

Browse files
Add is_type_of to PydanticObjectType to support Graphene Union
1 parent f1fd06e commit b8dce09

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

graphene_pydantic/objecttype.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pydantic
55
from graphene.types.objecttype import ObjectTypeOptions
66
from graphene.types.utils import yank_fields_from_attrs
7+
from graphql import GraphQLResolveInfo
78

89
from .converters import convert_pydantic_field
910
from .registry import Placeholder, Registry, get_global_registry
@@ -18,11 +19,11 @@ class PydanticObjectTypeOptions(ObjectTypeOptions):
1819

1920

2021
def construct_fields(
21-
obj_type: T.Type["PydanticObjectType"],
22-
model: T.Type[pydantic.BaseModel],
23-
registry: Registry,
24-
only_fields: T.Tuple[str, ...],
25-
exclude_fields: T.Tuple[str, ...],
22+
obj_type: T.Type["PydanticObjectType"],
23+
model: T.Type[pydantic.BaseModel],
24+
registry: Registry,
25+
only_fields: T.Tuple[str, ...],
26+
exclude_fields: T.Tuple[str, ...],
2627
) -> T.Dict[str, graphene.Field]:
2728
"""
2829
Construct all the fields for a PydanticObjectType.
@@ -59,16 +60,16 @@ class PydanticObjectType(graphene.ObjectType):
5960

6061
@classmethod
6162
def __init_subclass_with_meta__(
62-
cls,
63-
model: type = None,
64-
registry: Registry = None,
65-
skip_registry: bool = False,
66-
only_fields: T.Tuple[str, ...] = (),
67-
exclude_fields: T.Tuple[str, ...] = (),
68-
interfaces=(),
69-
id=None,
70-
_meta=None,
71-
**options,
63+
cls,
64+
model: type = None,
65+
registry: Registry = None,
66+
skip_registry: bool = False,
67+
only_fields: T.Tuple[str, ...] = (),
68+
exclude_fields: T.Tuple[str, ...] = (),
69+
interfaces=(),
70+
id=None,
71+
_meta=None,
72+
**options,
7273
):
7374
assert model and issubclass(
7475
model, pydantic.BaseModel
@@ -154,3 +155,7 @@ def resolve_placeholders(cls):
154155
meta.registry.register_object_field(cls, name, pydantic_field)
155156
# update the graphene side of things
156157
meta.fields.update(fields_to_update)
158+
159+
@classmethod
160+
def is_type_of(cls, root, _: GraphQLResolveInfo) -> bool:
161+
return root.__annotations__ == cls._meta.model.__annotations__

0 commit comments

Comments
 (0)