graphql-python / graphql-python/graphene-django

Support atomic mutations for all Mutations

Open
#1,190 4 comments 6 reactions 0 assignees View on GitHub
🐛bug
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

**What is the current behavior?**

Exceptions thrown in a `graphene.Mutation` do not result in a transaction rollback, even though `ATOMIC_REQUESTS` is on and/or `ATOMIC_MUTATIONS` is on.

The documentation states:

> If the view, a DjangoFormMutation or a DjangoModelFormMutation produces an exception, Django rolls back the transaction

This is an unexpected behavior to me. Many use-cases for mutations do not require a DjangoFormMutation / DjangoModelFormMutation even in a django app, but you would still expect the atomic feature to kick in, especially if ATOMIC_MUTATIONS is on.

**What is the expected behavior?**

Rollback transactions in all mutations if an unhandled exception is raised by the mutation code when ATOMIC_MUTATIONS or ATOMIC_REQUESTS is set.

**Please tell us about your environment:**

- Version: v2.15.0

**Workaround**

I am currently using the following workaround in order for the rollback on error behavior to kick-in:

```python
class ForceAtomicMutationMiddleware:
def resolve(self, next, root, info, **args):
def bound_on_error(error, info=info):
logger.error("Exception occurred in GraphQL resolver.", exc_info=error)
# this ensures ALL mutations will rollback, not just DjangoFormMutations
# see https://docs.graphene-python.org/projects/django/en/latest/mutations/#django-database-transactions
if info and info.context:
setattr(info.context, MUTATION_ERRORS_FLAG, True)

return next(root, info, **args).catch(bound_on_error)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.