It seems as if types created with strawberry.django.type cannot be used with interfaces currently.
Consider the following models and types:
# models.py
class Meal(Model):
is_draft = BooleanField()
# graphql.py
@strawberry.interface
class DiaryEntry:
is_draft: bool
@strawberry.django.type(models.Meal)
class Meal(DiaryEntry):
pass
@strawberry.type
class Query:
@strawberry.field
def diary(self, info: Info) -> List[DiaryEntry]:
return Meal.objects.all()
When trying to query the diary entries the following error would be raised:
"Abstract type 'DiaryEntry' must resolve to an Object type at runtime for field 'Query.diary'. Either the 'DiaryEntry' type should provide a 'resolve_type' function or each possible type should provide an 'is_type_of' function."
The easiest fix that I've found would be to allow the type's django model in the is_type_of function:
# strawberry/schema/schema_converter.py:265
# Current:
is_type_of = (
(lambda obj, _: isinstance(obj, object_type.origin))
if object_type.interfaces
else None
)
# Fixed:
if type_definition.interfaces:
def is_type_of(obj, _):
classes = (type_definition.origin,)
if hasattr(object_type, "_django_type"):
classes = (*classes, object_type._django_type.model)
return isinstance(obj, classes)
else:
is_type_of = None
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too