MCPcopy
hub / github.com/dosco/graphjin / Example_insertWithTransaction

Function Example_insertWithTransaction

tests/insert_test.go:56–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

54}
55
56func Example_insertWithTransaction() {
57 gql := `mutation {
58 users(insert: {
59 id: $id,
60 email: $email,
61 full_name: $fullName,
62 stripe_id: $stripeID,
63 category_counts: $categoryCounts
64 }) {
65 id
66 email
67 }
68 }`
69
70 vars := json.RawMessage(`{
71 "id": 1007,
72 "email": "user1007@test.com",
73 "fullName": "User 1007",
74 "stripeID": "payment_id_1007",
75 "categoryCounts": [{"category_id": 1, "count": 400},{"category_id": 2, "count": 600}]
76 }`)
77
78 conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
79 gj, err := core.NewGraphJin(conf, db)
80 if err != nil {
81 panic(err)
82 }
83 defer gj.Close()
84
85 c := context.Background()
86 tx, err := db.BeginTx(c, nil)
87 if err != nil {
88 panic(err)
89 }
90 defer tx.Rollback() //nolint:errcheck
91
92 c = context.WithValue(c, core.UserIDKey, 3)
93 res, err := gj.GraphQLTx(c, tx, gql, vars, nil)
94 if err != nil {
95 fmt.Println(err)
96 return
97 }
98 if err := tx.Commit(); err != nil {
99 panic(err)
100 }
101 printJSON(res.Data)
102 // Output: {"users":[{"email":"user1007@test.com","id":1007}]}
103}
104
105func Example_insertInlineWithValidation() {
106 gql := `mutation

Callers

nothing calls this directly

Calls 7

CloseMethod · 0.95
GraphQLTxMethod · 0.95
newConfigFunction · 0.85
printJSONFunction · 0.85
RollbackMethod · 0.65
CommitMethod · 0.65
BeginTxMethod · 0.45

Tested by

no test coverage detected