Skip to content

Instantly share code, notes, and snippets.

View fwojciec's full-sized avatar

Filip Wojciechowski fwojciec

View GitHub Profile
@fwojciec
fwojciec / queries.sql
Created January 20, 2020 16:21
gqlserver2: dataloaders20
-- name: ListAuthorsByAgentIDs :many
SELECT authors.* FROM authors, agents
WHERE authors.agent_id = agents.id AND agents.id = ANY($1::bigint[]);
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:21
gqlserver2: dataloaders19
type Loaders struct {
AgentByAuthorID *AgentLoader
AuthorsByAgentID *AuthorSliceLoader
}
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:20
gqlserver2: dataloaders18
package dataloaders
// ...
//go:generate dataloaden AuthorSliceLoader int64 []github.com/[username]/gqlgen-sqlc-example/pg.Author
@fwojciec
fwojciec / resolvers.go
Created January 20, 2020 16:18
gqlserver2: dataloaders17
func (r *authorResolver) Agent(ctx context.Context, obj *pg.Author) (*pg.Agent, error) {
return r.DataLoaders.Retrieve(ctx).AgentByAuthorID.Load(obj.ID)
}
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:17
gqlserver2: dataloaders16
func newLoaders(ctx context.Context, repo pg.Repository) *Loaders {
return &Loaders{
// individual loaders will be initialized here
AgentByAuthorID: newAgentByAuthorID(ctx, repo),
}
}
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:16
gqlserver2: dataloaders15
// order
result := make([]*pg.Agent, len(authorIDs))
for i, authorID := range authorIDs {
result[i] = groupByAuthorID[authorID]
}
return result, nil
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:15
gqlserver2: dataloaders14
// group
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,
}
}
@fwojciec
fwojciec / pg.go
Created January 20, 2020 16:14
gqlserver2: dataloaders13
type Repository interface {
// agent queries
// ...
ListAgentsByAuthorIDs(ctx context.Context, authorIDs []int64) ([]ListAgentsByAuthorIDsRow, error)
}
@fwojciec
fwojciec / queries.sql
Created January 20, 2020 16:13
gqlserver2: dataloaders12
-- name: ListAgentsByAuthorIDs :many
SELECT agents.*, authors.id AS author_id FROM agents, authors
WHERE agents.id = authors.agent_id AND authors.id = ANY($1::bigint[]);
@fwojciec
fwojciec / dataloaders.go
Created January 20, 2020 16:12
gqlserver2: dataloaders11
// db query
res, err := repo.ListAgentsByAuthorIDs(ctx, authorIDs)
if err != nil {
return nil, []error{err}
}