Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable atomic nested mutations #628

Open
1 task done
keithhackbarth opened this issue Sep 13, 2024 · 3 comments
Open
1 task done

Enable atomic nested mutations #628

keithhackbarth opened this issue Sep 13, 2024 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed proposal Proposal

Comments

@keithhackbarth
Copy link
Contributor

keithhackbarth commented Sep 13, 2024

Strawberry supports serialization for nested mutations but Strawberry-Django doesn't seem to be able do it in an atomic way

Feature Request Type

  • New behavior

Description

In the below example, I expect these things to happen in an atomic way so that if a credit card charge fails we do have to go back out previous transactions.

I've seen similar offers from other libraries such as Prisma.

mutation (
  $createInvoice: CreateInvoiceInput!
  $chargeCreditCard: ChargeCreditCardInput!
  atomic: true
) {
  createInvoice: invoice {
    createInvoice(input: $createInvoice) {
      id
    }
  }

  chargeCreditCard: invoice {
    chargeCreditCard(input: $chargeCreditCard) {
      id
    }
  }
}

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@keithhackbarth keithhackbarth changed the title Nested Mutations Should Nested Mutations Should Be Atomic Sep 14, 2024
@keithhackbarth keithhackbarth changed the title Nested Mutations Should Be Atomic Enable atomic nested mutations Sep 14, 2024
@bellini666
Copy link
Member

That should be the behavior.

GraphQL states that queries should be resolved in parallel while mutations should happen sequentially, meaning that if one fails, the remaining ones will not be executed.

Having said that, a fail means a GraphQL fail, which could not be the case when using "nice" error returns, such as those: https://strawberry.rocks/docs/django/guide/mutations#django-errors-handling

Is that not what you are seeing? If so, could you provide an example? Asking because that would probably be an issue at https://github.com/graphql-python/graphql-core level.

@keithhackbarth
Copy link
Contributor Author

keithhackbarth commented Sep 25, 2024

Thanks, @bellini666. My question is whether we should have a better mechanism to roll back previous actions if one fails. Should we be treating these mutations as an atomic transaction?

Right now, during checkout, we execute four or five different mutations (invoicing, payment, shipping, etc.). If one, like shipping, fails, we end up in an incomplete state. This might be acceptable for now, but I’m curious how other GraphQL libraries handle this. The detailed implementation of atomicity is more framework-specific (e.g., Django), so I wonder if it is less something that would be in the core but more framework-specific as well? Or if everyone is creating very specific custom mutations (ie checkout)?

Happy to close as "won't do" was just more curious about what the Django approach is?

@bellini666
Copy link
Member

@keithhackbarth hrm I see what you mean...

From the top of my head, I would say that this could be achieved by using extensions, more specifically the on_execute lifecycle hook.

It can check if the operation is a mutation, and if it is, it starts a migration in it.

Not sure if we could do anything on the django integration side to facilitate that. We should try to check how other frameworks do that (if they do) to get some ideas :)

@bellini666 bellini666 added enhancement New feature or request help wanted Extra attention is needed proposal Proposal labels Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed proposal Proposal
Projects
None yet
Development

No branches or pull requests

2 participants