Skip to content

how about let mutation operation has transaction ability automatically? #2310

Answered by giautm
bonafideyan asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @bonafideyan, because it depends on the Database driver / ORM that you used in your project. So, I think GQLGen can't do it automatically. However, you can apply the flowing pattern:

  1. Open Transaction use graphql.HandlerExtension, and attach it to the Go's context object.
  2. In your mutations, you can get the Tx object from the context and do the updates. If something go wrong, just return the error and the extension in step 1) will call rollback automatic

We (Ent/EntGQL) applied this pattern in our implementation. So, you can check out the example here for detail and here. This also can apply to the regular SQL driver:

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@bonafideyan
Comment options

@giautm
Comment options

Answer selected by frederikhors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2309 on July 30, 2022 14:33.