Transactional Mutations

In this section, we continue the GraphQL example by explaining how to set our GraphQL mutations to be transactional. That means, to automatically wrap our GraphQL mutations with a database transaction and either commit at the end, or rollback the transaction in case of a GraphQL error.

Clone the code (optional)

The code for this tutorial is available under github.com/a8m/ent-graphql-example, and tagged (using Git) in each step. If you want to skip the basic setup and start with the initial version of the GraphQL server, you can clone the repository and checkout v0.1.0 as follows:

  1. git clone git@github.com:a8m/ent-graphql-example.git
  2. cd ent-graphql-example
  3. go run ./cmd/todo/

Usage

The GraphQL extensions provides a handler named entgql.Transactioner that executes each GraphQL mutation in a transaction. The injected client for the resolver is a transactional ent.Client. Hence, GraphQL resolvers that uses ent.Client won’t need to be changed. In order to add it to our todo list application we follow these steps:

  1. Edit the cmd/todo/main.go and add to the GraphQL server initialization the entgql.Transactioner handler as follows:

cmd/todo/main.go

  1. srv := handler.NewDefaultServer(todo.NewSchema(client))
  2. +srv.Use(entgql.Transactioner{TxOpener: client})
  1. Then, in the GraphQL mutations, use the client from context as follows:

todo.resolvers.go

  1. }
  2. +func (mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
  3. + client := ent.FromContext(ctx)
  4. + return client.Todo.Create().SetInput(input).Save(ctx)
  5. -func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
  6. - return r.client.Todo.Create().SetInput(input).Save(ctx)
  7. }

Isolation Levels

If you’d like to tweak the transaction’s isolation level, you can do so by implementing your own TxOpener. For example:

cmd/todo/main.go

  1. srv.Use(entgql.Transactioner{
  2. TxOpener: entgql.TxOpenerFunc(func(ctx context.Context) (context.Context, driver.Tx, error) {
  3. tx, err := client.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
  4. if err != nil {
  5. return nil, nil, err
  6. }
  7. ctx = ent.NewTxContext(ctx, tx)
  8. ctx = ent.NewContext(ctx, tx.Client())
  9. return ctx, tx, nil
  10. }),
  11. })

Skip Operations

By default, entgql.Transactioner wraps all mutations within a transaction. However, there are mutations or operations that don’t require database access or need special handling. In these cases, you can instruct entgql.Transactioner to skip the transaction by setting a custom SkipTxFunc function or using one of the built-in ones.

cmd/todo/main.go

  1. srv.Use(entgql.Transactioner{
  2. TxOpener: client,
  3. // Skip the given operation names from running under a transaction.
  4. SkipTxFunc: entgql.SkipOperations("operation1", "operation2"),
  5. })
  6. srv.Use(entgql.Transactioner{
  7. TxOpener: client,
  8. // Skip if the operation has a mutation field with the given names.
  9. SkipTxFunc: entgql.SkipIfHasFields("field1", "field2"),
  10. })
  11. srv.Use(entgql.Transactioner{
  12. TxOpener: client,
  13. // Custom skip function.
  14. SkipTxFunc: func(*ast.OperationDefinition) bool {
  15. // ...
  16. },
  17. })

Great! With a few lines of code, our application now supports automatic transactional mutations. Please continue to the next section where we explain how to extend the Ent code generator and generate GraphQL input types for our GraphQL mutations.