strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
420 stars 122 forks source link

How to override the logic on the Basic create, retrieve, update and delete (CRUD) types and mutations #165

Open sisocobacho opened 2 years ago

sisocobacho commented 2 years ago

I will like to override the logic inside basic create mutation, much like in django restframework modelviewsets or serializers. What is the best way of doing this?

Upvote & Fund

Fund with Polar

bellini666 commented 2 years ago

Hi @sisocobacho ,

What exactly are you trying to override?

You can probably override the field to call your own function instead of the ones the current ones call for example. Or you can have a basic mutation and call the create/retrieve/update/delete functions for example.

If you want to give an example of a use case I can suggest something for you

sisocobacho commented 2 years ago

Hello, Thanks for the suggestions. Basically what I am trying to do is insert business logic inside those mutation without having to rewrite a new one from 0 and make use off the functionality that those mutations already have. For example doing something before or after saving the model in the create mutation.

nrbnlulu commented 2 years ago

Yeah hooks are a great idea..

arseniid commented 4 months ago

Hello everyone, are there any updates on the issue?

As an example use-case I would be interested in having something similar to:

@strawberry.type
class Mutation:
    @strawberry_django.mutation(description="Create my model.")
    def create_my_model(self, data: MyModelInputType, info) -> MyModelType:
        create_mutation = strawberry_django.mutations.create(MyModelInputType)
        my_django_model = create_mutation(...)
        # additional business logic goes here...

Since this issue only has a 'documentation' label, is this already possible? If yes, would you please provide a simple code snippet on how to achieve it? I appreciate any help you can provide.

bellini666 commented 4 months ago

@arseniid yes, that should be possible. It is unfortunatly missing documentation, but in the meantime you can take a look at how DjangoCreateMutation works. Nut a quick example would be:

from strawberry_django.mutations import resolvers

@strawberry.type
class Mutation:
    @strawberry_django.mutation(description="Create my model.")
    def create_my_model(self, data: MyModelInputType, info: Info) -> MyModelType:
        input_data = resolvers.parse_input(info, vars(data))
        my_django_model = resolvers.create(
            info,
            MyModel,
            input_data,
        )
        # additional business logic goes here...

resolvers.parse_input and resolvers.create have some extra inputs that you might be interested. Also, the original code disables the DjangoOptimizerExtension while creating the model, which is optional and it is not a big deal if you don't do that.

The same goes for resolvers.update and resolvers.delete

arseniid commented 4 months ago

@bellini666 thanks a lot for the quick response - works like a charm! I was missing the resolvers entry point here.