When I write a custom resolver for a strawberry_django type, I need to specify the graphql type as the resolver's return type. However, since my resolvers needs to actually return an instance of the model, I get python typing errors.
Is there a way that I can get Python typing to be happy, or is there a way we can update strawberry_django to handle this scenario?
See the following code as an example. It works as I desire, but my Python type checker gives the following complaint about the last line of code.
Expected type 'UserPreferencesType', got 'UserPreferences' instead
import strawberry
import strawberry_django
from strawberry import auto
from strawberry.types import Info
from userprefs.models import UserPreferences
@strawberry_django.type(UserPreferences, name='UserPreferences')
class UserPreferencesType:
date_format: auto
def resolve_user_preferences(info: Info) -> UserPreferencesType:
user = info.context.request.user
if user.is_authenticated:
if prefs := UserPreferences.objects.filter(user__id=user.id).first():
return prefs
# Return an unsaved model instance if user has not customised preferences yet (or is not logged in)
# as that model will contain the default preference values.
return UserPreferences()
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