diff --git a/docs/content/reference/dataloaders.md b/docs/content/reference/dataloaders.md index 7e198d5bd6c..a986fe1b30d 100644 --- a/docs/content/reference/dataloaders.md +++ b/docs/content/reference/dataloaders.md @@ -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) }) @@ -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