Skip to content

Commit

Permalink
fix: request-scoped middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
zenyui committed Jul 20, 2023
1 parent ff79b23 commit fe4e487
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/content/reference/dataloaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ func NewLoaders(conn *sql.DB) *Loaders {

// Middleware injects data loaders into the context
func Middleware(conn *sql.DB, next http.Handler) http.Handler {
// create a request-scoped data loader
loader := NewLoaders(conn)
// return a middleware that injects the loader to the request context
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// create a request-scoped data loader
loader := NewLoaders(conn)
// inject to context
nextCtx := context.WithValue(r.Context(), loadersKey, loader)
r = r.WithContext(nextCtx)
next.ServeHTTP(w, r)
Expand Down

0 comments on commit fe4e487

Please sign in to comment.