From 2219f5dba0bb494414c341422130875dc45807a7 Mon Sep 17 00:00:00 2001 From: Franxois Date: Tue, 25 Jun 2019 15:08:36 +0200 Subject: [PATCH] Update dataloaders.md Make SQL request use requested IDs --- docs/content/reference/dataloaders.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/content/reference/dataloaders.md b/docs/content/reference/dataloaders.md index 1e1cd149c9..c478947bc9 100644 --- a/docs/content/reference/dataloaders.md +++ b/docs/content/reference/dataloaders.md @@ -102,7 +102,7 @@ func DataloaderMiddleware(db *sql.DB, next http.Handler) http.Handler { args := make([]interface{}, len(ids)) for i := 0; i < len(ids); i++ { placeholders[i] = "?" - args[i] = i + args[i] = ids[i] } res := logAndQuery(db, @@ -113,18 +113,21 @@ func DataloaderMiddleware(db *sql.DB, next http.Handler) http.Handler { defer res.Close() - users := make([]*User, len(ids)) - i := 0 + users := make(map[int]*User, len(ids)) for res.Next() { - users[i] = &User{} - err := res.Scan(&users[i].ID, &users[i].Name) + user := &User{} + err := res.Scan(&user.ID, &user.Name) if err != nil { panic(err) } - i++ + users[user.ID] = user } - - return users, nil + + output := make([]*User, len(ids)) + for i, id := range ids { + output[i] = users[id] + } + return output, nil }, } ctx := context.WithValue(r.Context(), userLoaderKey, &userloader)