forked from ent/contrib
/
transaction.go
110 lines (97 loc) · 3.08 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2019-present Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package entgql
import (
"context"
"database/sql/driver"
"errors"
"sync"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
// TxOpener represents types than can open transactions.
type TxOpener interface {
OpenTx(ctx context.Context) (context.Context, driver.Tx, error)
}
// The TxOpenerFunc type is an adapter to allow the use of
// ordinary functions as tx openers.
type TxOpenerFunc func(ctx context.Context) (context.Context, driver.Tx, error)
// OpenTx returns f(ctx).
func (f TxOpenerFunc) OpenTx(ctx context.Context) (context.Context, driver.Tx, error) {
return f(ctx)
}
// Transactioner for graphql mutations.
type Transactioner struct{ TxOpener }
var _ interface {
graphql.HandlerExtension
graphql.OperationContextMutator
graphql.ResponseInterceptor
} = Transactioner{}
// ExtensionName returns the extension name.
func (Transactioner) ExtensionName() string {
return "EntGQLTransactioner"
}
// Validate is called when adding an extension to the server, it allows validation against the servers schema.
func (t Transactioner) Validate(graphql.ExecutableSchema) error {
if t.TxOpener == nil {
return errors.New("entgql: tx opener is nil")
}
return nil
}
// MutateOperationContext serializes field resolvers during mutations.
func (Transactioner) MutateOperationContext(_ context.Context, oc *graphql.OperationContext) *gqlerror.Error {
if op := oc.Operation; op != nil && op.Operation == ast.Mutation {
previous := oc.ResolverMiddleware
var mu sync.Mutex
oc.ResolverMiddleware = func(ctx context.Context, next graphql.Resolver) (interface{}, error) {
mu.Lock()
defer mu.Unlock()
return previous(ctx, next)
}
}
return nil
}
// InterceptResponse runs graphql mutations under a transaction.
func (t Transactioner) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
if op := graphql.GetOperationContext(ctx).Operation; op == nil || op.Operation != ast.Mutation {
return next(ctx)
}
txCtx, tx, err := t.OpenTx(ctx)
if err != nil {
return graphql.ErrorResponse(ctx,
"cannot create transaction: %s", err.Error(),
)
}
ctx = txCtx
defer func() {
if r := recover(); r != nil {
_ = tx.Rollback()
panic(r)
}
}()
rsp := next(ctx)
if len(rsp.Errors) > 0 {
_ = tx.Rollback()
return &graphql.Response{
Errors: rsp.Errors,
}
}
if err := tx.Commit(); err != nil {
return graphql.ErrorResponse(ctx,
"cannot commit transaction: %s", err.Error(),
)
}
return rsp
}