Skip to content

Commit

Permalink
Fix docs (#2722)
Browse files Browse the repository at this point in the history
* docs: fix variable names in dataloader sample

* fix: request-scoped middleware
  • Loading branch information
zenyui authored Jul 20, 2023
1 parent b233a01 commit 16c9eb6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions docs/content/reference/dataloaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ 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) {
nextCtx := context.WithValue(r.Context(), loadersKey, loaders)
// 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 All @@ -155,8 +156,8 @@ func For(ctx context.Context) *Loaders {

// GetUser wraps the User dataloader for efficient retrieval by user ID
func GetUser(ctx context.Context, userID string) (*model.User, error) {
loaders := For(ctx)
thunk := loaders.UserLoader.Load(ctx, dataloader.StringKey(userID))
loader := For(ctx)
thunk := loader.UserLoader.Load(ctx, dataloader.StringKey(userID))
result, err := thunk()
if err != nil {
return nil, err
Expand Down

0 comments on commit 16c9eb6

Please sign in to comment.