graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

Handling database transactions

Open brucedesa opened this issue 6 years ago • 10 comments

I noticed that graphene-django doesn't handle database transactions. I believe this is a issue that people should know or be careful about.

The same logic of DRF could be implemented as in the lines below that run in the dispatch method.

https://github.com/encode/django-rest-framework/blob/d985c7cbb999b2bc18a109249c583e91f4c27aec/rest_framework/views.py#L65-L102

Basically on one exception runs:

atomic_requests = settings.get('ATOMIC_REQUESTS', False)
if atomic_requests and connection.in_atomic_block:
   transaction.set_rollback(True)

In implementing this in graphene-django as we do not have the exception could run the rollback in the error checking of the execution result as in line 171.

https://github.com/graphql-python/graphene-django/blob/968002f1554e3a7a1c0617682be64b67823b2581/graphene_django/views.py#L167-L190

What do you think about this implementation?

brucedesa avatar Dec 24 '19 20:12 brucedesa

Wouldn’t it mean that if part of the query/mutation raised an exception then every database change would be rolled back? Also wouldn’t the whole execution need to be wrapped in a transaction atomic block as well?

jkimbo avatar Dec 24 '19 20:12 jkimbo

As good practice all requests are within an atomic block this is Django's default behavior.

In case of performance Django has utilities to run mutations without atomicity.

But since i use ATOMIC_REQUESTS=True, and the exceptions are not raised up, graphene-django does not care to make the request atomic and rollback on failure.

brucedesa avatar Dec 24 '19 21:12 brucedesa

Ok fair enough, it seems like a good addition to the library. Your suggestion on where to put the transaction rollback is correct. Could you create a PR that adds the rollback under a settings option?

jkimbo avatar Dec 24 '19 22:12 jkimbo

Hello, any progress on this issue or PR?

ace-han avatar Jan 31 '20 16:01 ace-han

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 30 '20 17:05 stale[bot]

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Aug 27 '20 00:08 stale[bot]

@jkimbo, @ulgens, @ace-han and @brucedesa I was faced with an issue similar to the one described in this issue and I created the PR #1039 to solve both use cases.

iorlandini avatar Sep 22 '20 11:09 iorlandini

@jkimbo I tried wrapping mutate method in Django's transaction.atomic decorator and it worked. Why can't we use it ? Just curious

@classmethod
@transaction.atomic
def mutate(cls, root, info, **kwargs):
    # do something

overide avatar Feb 08 '21 07:02 overide

@jkimbo I tried wrapping mutate method in Django's transaction.atomic decorator and it worked. Why can't we use it ? Just curious

@classmethod
@transaction.atomic
def mutate(cls, root, info, **kwargs):
    # do something

You can use it. The PR #1039 is about making graphql-python works as Django is expected when turning on the settings ATOMIC_TRANSACTION https://docs.djangoproject.com/en/3.1/topics/db/transactions/#tying-transactions-to-http-requests

iorlandini avatar Feb 08 '21 11:02 iorlandini

@jkimbo I tried wrapping mutate method in Django's transaction.atomic decorator and it worked. Why can't we use it ? Just curious

@classmethod
@transaction.atomic
def mutate(cls, root, info, **kwargs):
    # do something

You can use it. The PR #1039 is about making graphql-python works as Django is expected when turning on the settings ATOMIC_TRANSACTION https://docs.djangoproject.com/en/3.1/topics/db/transactions/#tying-transactions-to-http-requests

Got it, thanks a lot!

overide avatar Feb 16 '21 13:02 overide