Created
January 20, 2020 16:11
-
-
Save fwojciec/4675386863f59cb51459fc835804ff30 to your computer and use it in GitHub Desktop.
gqlserver2: dataloaders10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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