Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix docs #2722

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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