Open ghost opened 2 years ago
Do you have any specific questions you would like to get answer? Your example code looks quite generic, which means that you probably want to ask some of your questions in strawberry core project: https://github.com/strawberry-graphql/strawberry
It was suggested I ask on here when I asked on discord.
My question is, how can I hook into the mutations.*
utilities so that I can add business logic, rather than having to reimplement them with my custom logic. For example, when creating a Foo
I might want to populate an owner
field based on the request context. Or If I am updating Bar
I might want to check user has the correct permissions or perform certain business logic dependent on state.
You can extend existing mutation classes and add your business logic there. Unfortunately I cannot verify the example code just now but you get the idea
from strawberry_django.mutations.fields import DjangoUpdateMutation
class YourUpdateMutation(DjangoUpdateMutation):
def resolver(self, info, source, data, **kwargs):
queryset = super().resolver(info, source, data, **kwargs)
queryset.update(owner=...)
return queryset
@strawberry.type
class Mutation:
update_foo: List[FooNode] = YourUpdateMutation(PatchFooInput)
I too have heavy usage of graphene-django-cud and find it to be critical for reducing a ton of boilerplate, so would not be able to migrate from graphene to strawberry without some equivalent.
I recently came across strawberry-django-extras which seems to support nested mutations and presumably help with DRYing up mutation code. Haven't used it myself yet but looks promising!
@la4de is right, you want to use strawberry_django's DjangoUpdateMutation
, except you want to use the update
method .. in your case it would look something like this:
from strawberry_django.mutations.fields import DjangoUpdateMutation
from strawberry_django.permissions import IsStaff
class YourUpdateMutation(DjangoUpdateMutation):
def update(self, info, instance, data):
# ...business logic... I found you can pretty much copy & paste the logic you used with graphene-django-cud #
self.key_attr = "pk"
self.full_clean = True
return super().update(info, instance, data)
class YourCreateMutation(DjangoCreateMutation):
def create(self, data, info):
# ...business logic... #
return super().create(data, info=info)
@strawberry.type
class Mutation:
update_foo: List[FooNode] = YourUpdateMutation(PatchFooInputPartial, extensions=[IsStaff()])
create_foo: List[FooNode] = YourCreateMutation(CreateFooInput, extensions=[IsStaff()])
also wanted to suggest the use of IsStaff
in strawberry_django.permissions
as it might solve for the permissions logic you're trying to achieve
We are looking into moving away from graphene and strawberry looks like a very nice alternative!
Unfortunately we have some heavy integration with django-graphene-cud which offers a lot of useful features (some are too magic for my liking). I was unable to find a way to use this package in a way that offered us the hooks we needed for some of our mutations and ended up on a boilerplate heavy strawberry only PoC shown below.
Are we missing anything obvious with this library?
Upvote & Fund