Skip to content

Commit

Permalink
Merge pull request #758 from franxois/patch-1
Browse files Browse the repository at this point in the history
Update dataloaders.md
  • Loading branch information
vektah authored Jun 26, 2019
2 parents 56f3f92 + d0db28a commit 2dc8423
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions docs/content/reference/dataloaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 2dc8423

Please sign in to comment.