Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Created January 20, 2020 16:11
Show Gist options
  • Save fwojciec/4675386863f59cb51459fc835804ff30 to your computer and use it in GitHub Desktop.
Save fwojciec/4675386863f59cb51459fc835804ff30 to your computer and use it in GitHub Desktop.
gqlserver2: dataloaders10
func newAgentByAuthorID(ctx context.Context, repo pg.Repository) *AgentLoader {
return NewAgentLoader(AgentLoaderConfig{
MaxBatch: 100,
Wait: 5 * time.Millisecond,
Fetch: func(authorIDs []int64) ([]*pg.Agent, []error) {
// db query
res, err := repo.ListAgentsByAuthorIDs(ctx, authorIDs)
if err != nil {
return nil, []error{err}
}
// map
groupByAuthorID := make(map[int64]*pg.Agent, len(authorIDs))
for _, r := range res {
groupByAuthorID[r.AuthorID] = &pg.Agent{
ID: r.ID,
Name: r.Name,
Email: r.Email,
}
}
// order
result := make([]*pg.Agent, len(authorIDs))
for i, authorID := range authorIDs {
result[i] = groupByAuthorID[authorID]
}
return result, nil
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment