Created
January 20, 2020 15:57
-
-
Save fwojciec/71b59660ccbc01316403b8e861548e3f to your computer and use it in GitHub Desktop.
gqlserver2: dataloaders2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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