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 datastore_test | |
import ( | |
"bufio" | |
"bytes" | |
"context" | |
"fmt" | |
"net/http" | |
"os" | |
"os/exec" |
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 *bookResolver) Authors(ctx context.Context, obj *pg.Book) ([]pg.Author, error) { | |
return r.DataLoaders.Retrieve(ctx).AuthorsByBookID.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 newAuthorsByBookID(ctx context.Context, repo pg.Repository) *AuthorSliceLoader { | |
return NewAuthorSliceLoader(AuthorSliceLoaderConfig{ | |
MaxBatch: 100, | |
Wait: 5 * time.Millisecond, | |
Fetch: func(bookIDs []int64) ([][]pg.Author, []error) { | |
// db query | |
res, err := repo.ListAuthorsByBookIDs(ctx, bookIDs) | |
if err != nil { | |
return nil, []error{err} | |
} |
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 { | |
// ... | |
// author queries | |
// ... | |
ListAuthorsByBookIDs(ctx context.Context, bookIDs []int64) ([]ListAuthorsByBookIDsRow, 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: ListAuthorsByBookIDs :many | |
SELECT authors.*, book_authors.book_id FROM authors, book_authors | |
WHERE book_authors.author_id = authors.id AND book_authors.book_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 | |
AuthorsByBookID *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
func (r *agentResolver) Authors(ctx context.Context, obj *pg.Agent) ([]pg.Author, error) { | |
return r.DataLoaders.Retrieve(ctx).AuthorsByAgentID.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{ | |
AgentByAuthorID: newAgentByAuthorID(ctx, repo), | |
AuthorsByAgentID: newAuthorsByAgentID(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
func newAuthorsByAgentID(ctx context.Context, repo pg.Repository) *AuthorSliceLoader { | |
return NewAuthorSliceLoader(AuthorSliceLoaderConfig{ | |
MaxBatch: 100, | |
Wait: 5 * time.Millisecond, | |
Fetch: func(agentIDs []int64) ([][]pg.Author, []error) { | |
// db query | |
res, err := repo.ListAuthorsByAgentIDs(ctx, agentIDs) | |
if err != nil { | |
return nil, []error{err} | |
} |
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 { | |
// ... | |
// author queries | |
// ... | |
ListAuthorsByAgentIDs(ctx context.Context, agentIDs []int64) ([]Author, error) | |
} |
NewerOlder