How can I do nested mutations in graphene django? #1308
-
Hi guys I want to be able to perform a nested mutation (not sure if it's the correct term) here is my models
and this is my current implementation of creating a customer
I have a mutation for address, and user, I would like to be able to pass those mutations as arguments to the create customer class and my mutation would look something like this
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think that it is not possible with graphql, usually prefer to create many factory functions in a service because of it, is better to create the tests too class CreateCustomer(graphene.Mutation):
customer = graphene.Field(CustomerType)
class Arguments:
user_input = UserInput()
address_input = AddressInput()
def mutate(self, info, user_input, address_input):
user_service_instance = UserService()
created_user = user_service_instance.create_user_factory(user_input)
if address_input:
user_service_instance.update_address(address_input)
return CreateCustomer(customer=created_user) |
Beta Was this translation helpful? Give feedback.
-
I find the graphene-django-cud package very helpful for reducing boilerplate in mutations, and allowing for things like nested fields in create/update. I believe it handles your original use-case. Check out the docs on that here: https://graphene-django-cud.readthedocs.io/en/latest/guide/nested-fields.html |
Beta Was this translation helpful? Give feedback.
I find the graphene-django-cud package very helpful for reducing boilerplate in mutations, and allowing for things like nested fields in create/update. I believe it handles your original use-case. Check out the docs on that here: https://graphene-django-cud.readthedocs.io/en/latest/guide/nested-fields.html