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} | |
} |
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
// Loaders holds references to the individual dataloaders. | |
type Loaders struct { | |
AgentByAuthorID *AgentLoader | |
} |
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
// AgentLoaderConfig captures the config to create a new AgentLoader | |
type AgentLoaderConfig struct { | |
// Fetch is a method that provides the data for the loader | |
Fetch func(keys []int64) ([]*pg.Agent, []error) | |
// Wait is how long wait before sending a batch | |
Wait time.Duration | |
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit | |
MaxBatch int |
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 go run github.com/vektah/dataloaden AgentLoader int64 *github.com/[username]/gqlgen-sqlc-example/pg.Agent |
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 main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"github.com/[username]/gqlgen-sqlc-example/dataloaders" // update the username | |
"github.com/[username]/gqlgen-sqlc-example/gqlgen" // update the username | |
"github.com/[username]/gqlgen-sqlc-example/pg" // update the username |
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
// NewHandler returns a new graphql endpoint handler. | |
func NewHandler(repo pg.Repository, dl dataloaders.Retriever) http.Handler { | |
return handler.GraphQL(NewExecutableSchema(Config{ | |
Resolvers: &Resolver{ | |
Repository: repo, | |
DataLoaders: dl, | |
}, | |
})) | |
} |
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
// Resolver connects individual resolvers with the datalayer. | |
type Resolver struct { | |
Repository pg.Repository | |
DataLoaders dataloaders.Retriever | |
} |
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
// Retriever retrieves dataloaders from the request context. | |
type Retriever interface { | |
Retrieve(context.Context) *Loaders | |
} | |
type retriever struct { | |
key contextKey | |
} | |
func (r *retriever) Retrieve(ctx context.Context) *Loaders { |
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 | |
import ( | |
"context" | |
"net/http" | |
"github.com/[username]/gqlgen-sqlc-example/pg" // update the username | |
) | |
// Middleware stores Loaders as a request-scoped context value. |
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 | |
import ( | |
"context" | |
"github.com/[username]/gqlgen-sqlc-example/pg" // update the username | |
) | |
type contextKey string |