Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Created January 20, 2020 15:57
Show Gist options
  • Save fwojciec/71b59660ccbc01316403b8e861548e3f to your computer and use it in GitHub Desktop.
Save fwojciec/71b59660ccbc01316403b8e861548e3f to your computer and use it in GitHub Desktop.
gqlserver2: dataloaders2
package dataloaders
import (
"context"
"net/http"
"github.com/[username]/gqlgen-sqlc-example/pg" // update the username
)
// Middleware stores Loaders as a request-scoped context value.
func Middleware(repo pg.Repository) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
loaders := newLoaders(ctx, repo)
augmentedCtx := context.WithValue(ctx, key, loaders)
r = r.WithContext(augmentedCtx)
next.ServeHTTP(w, r)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment