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
-- name: ListAuthorsByAgentIDs :many | |
SELECT authors.* FROM authors, agents | |
WHERE authors.agent_id = agents.id AND agents.id = ANY($1::bigint[]); |
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
type Loaders struct { | |
AgentByAuthorID *AgentLoader | |
AuthorsByAgentID *AuthorSliceLoader | |
} |
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
package dataloaders | |
// ... | |
//go:generate dataloaden AuthorSliceLoader int64 []github.com/[username]/gqlgen-sqlc-example/pg.Author |
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 (r *authorResolver) Agent(ctx context.Context, obj *pg.Author) (*pg.Agent, error) { | |
return r.DataLoaders.Retrieve(ctx).AgentByAuthorID.Load(obj.ID) | |
} |
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 newLoaders(ctx context.Context, repo pg.Repository) *Loaders { | |
return &Loaders{ | |
// individual loaders will be initialized here | |
AgentByAuthorID: newAgentByAuthorID(ctx, repo), | |
} | |
} |
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
// order | |
result := make([]*pg.Agent, len(authorIDs)) | |
for i, authorID := range authorIDs { | |
result[i] = groupByAuthorID[authorID] | |
} | |
return result, nil |
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
// 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, | |
} | |
} |
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
type Repository interface { | |
// agent queries | |
// ... | |
ListAgentsByAuthorIDs(ctx context.Context, authorIDs []int64) ([]ListAgentsByAuthorIDsRow, error) | |
} |
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
-- 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[]); |
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
// db query | |
res, err := repo.ListAgentsByAuthorIDs(ctx, authorIDs) | |
if err != nil { | |
return nil, []error{err} | |
} |