In trying to use the buffalo-pop/pop/popmw Transaction
middleware, I am not having success writing to the database. No errors are returned, and the debug output shows the SQL statements, but the updates and inserts are not committed.
The handler looks like:
func MyHandler(c buffalo.Context) error {
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.New("no transaction found")
}
f := models.File{
Name: "file.txt",
}
if err := tx.Create(&f); err != nil {
return err
}
return nil
}
app.go:
func App() *buffalo.App {
...
app.GET("/myhandler", MyHandler)
app.Use(popmw.Transaction(models.DB))
...
}
If I use DB, _ := pop.Connect("development")
for my connection, it works correctly. I also observed that the autoincrement value on the table changes each time this handler is hit.
In the real app, we can't call c.Render
to report a response code because we are using gqlgen
as the http handler. It looks like this:
func GQLHandler(c buffalo.Context) error {
h := handler.GraphQL(gqlgen.NewExecutableSchema(gqlgen.Config{Resolvers: &gqlgen.Resolver{}}))
newCtx := context.WithValue(c.Request().Context(), "BuffaloContext", c)
h.ServeHTTP(c.Response(), c.Request().WithContext(newCtx))
return nil
}