Skip to content

Instantly share code, notes, and snippets.

View fwojciec's full-sized avatar

Filip Wojciechowski fwojciec

View GitHub Profile
@fwojciec
fwojciec / emulator_test.go
Last active August 21, 2020 13:32
Wrapper around gcloud datastore emulator -- for use in tests...
package datastore_test
import (
"bufio"
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/exec"
func (r *bookResolver) Authors(ctx context.Context, obj *pg.Book) ([]pg.Author, error) {
return r.DataLoaders.Retrieve(ctx).AuthorsByBookID.Load(obj.ID)
}
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}
}
type Repository interface {
// ...
// author queries
// ...
ListAuthorsByBookIDs(ctx context.Context, bookIDs []int64) ([]ListAuthorsByBookIDsRow, error)
}
-- 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[]);
type Loaders struct {
AgentByAuthorID *AgentLoader
AuthorsByAgentID *AuthorSliceLoader
AuthorsByBookID *AuthorSliceLoader
}
func (r *agentResolver) Authors(ctx context.Context, obj *pg.Agent) ([]pg.Author, error) {
return r.DataLoaders.Retrieve(ctx).AuthorsByAgentID.Load(obj.ID)
}
func newLoaders(ctx context.Context, repo pg.Repository) *Loaders {
return &Loaders{
AgentByAuthorID: newAgentByAuthorID(ctx, repo),
AuthorsByAgentID: newAuthorsByAgentID(ctx, repo),
}
}
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}
}
@fwojciec
fwojciec / pg.go
Created January 20, 2020 16:22
gqlserver2: dataloaders21
type Repository interface {
// ...
// author queries
// ...
ListAuthorsByAgentIDs(ctx context.Context, agentIDs []int64) ([]Author, error)
}