From ff79b23157c9614e350eeec13d39c555cfd949e5 Mon Sep 17 00:00:00 2001 From: zen Date: Wed, 19 Jul 2023 20:59:10 -0400 Subject: [PATCH 1/2] docs: fix variable names in dataloader sample --- docs/content/reference/dataloaders.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/content/reference/dataloaders.md b/docs/content/reference/dataloaders.md index 7e198d5bd6c..08f817c77c7 100644 --- a/docs/content/reference/dataloaders.md +++ b/docs/content/reference/dataloaders.md @@ -142,7 +142,7 @@ func Middleware(conn *sql.DB, next http.Handler) http.Handler { 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) + nextCtx := context.WithValue(r.Context(), loadersKey, loader) r = r.WithContext(nextCtx) next.ServeHTTP(w, r) }) @@ -155,8 +155,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 From fe4e48702da0ebd55cd49cda3677157f2f5fe3fc Mon Sep 17 00:00:00 2001 From: zen Date: Wed, 19 Jul 2023 22:37:21 -0400 Subject: [PATCH 2/2] fix: request-scoped middleware --- docs/content/reference/dataloaders.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/content/reference/dataloaders.md b/docs/content/reference/dataloaders.md index 08f817c77c7..a986fe1b30d 100644 --- a/docs/content/reference/dataloaders.md +++ b/docs/content/reference/dataloaders.md @@ -138,10 +138,11 @@ 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) { + // 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)