add a new package 'dataloader', add a new middleware 'DataLoaderToContext'
This commit is contained in:
parent
4c0bf09b69
commit
03fad75536
|
@ -39,10 +39,10 @@ func UserFromContext(ctx context.Context) (*models.User, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
gc, ok := user.(*models.User)
|
||||
u, ok := user.(*models.User)
|
||||
if !ok {
|
||||
err := fmt.Errorf("*models.User has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return gc, nil
|
||||
return u, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/dataloader"
|
||||
)
|
||||
|
||||
var (
|
||||
dataLoaderToContext contextKey = "data_loader"
|
||||
)
|
||||
|
||||
func DataLoaderToContext(cfg dataloader.Config) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := context.WithValue(c.Request.Context(), dataLoaderToContext, dataloader.New(cfg))
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func DataLoaderFromContext(ctx context.Context) (*dataloader.DataLoader, error) {
|
||||
dataLoader := ctx.Value(dataLoaderToContext)
|
||||
if dataLoader == nil {
|
||||
err := fmt.Errorf("could not retrieve dataloader.DataLoader")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dl, ok := dataLoader.(*dataloader.DataLoader)
|
||||
if !ok {
|
||||
err := fmt.Errorf("dataloader.DataLoader has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return dl, nil
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package dataloader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
QualificationRepo qualification.Repository
|
||||
}
|
||||
|
||||
type DataLoader struct {
|
||||
QualificationByID *QualificationLoader
|
||||
}
|
||||
|
||||
func New(cfg Config) *DataLoader {
|
||||
return &DataLoader{
|
||||
QualificationByID: NewQualificationLoader(QualificationLoaderConfig{
|
||||
Wait: 2 * time.Millisecond,
|
||||
Fetch: func(ids []int) ([]*models.Qualification, []error) {
|
||||
qualificationsNotInOrder, _, err := cfg.QualificationRepo.Fetch(context.Background(), &qualification.FetchConfig{
|
||||
Filter: &models.QualificationFilter{
|
||||
ID: ids,
|
||||
},
|
||||
Count: false,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
}
|
||||
qualificationByID := make(map[int]*models.Qualification)
|
||||
for _, qualification := range qualificationsNotInOrder {
|
||||
qualificationByID[qualification.ID] = qualification
|
||||
}
|
||||
qualifications := make([]*models.Qualification, len(ids))
|
||||
for i, id := range ids {
|
||||
qualifications[i] = qualificationByID[id]
|
||||
}
|
||||
return qualifications, nil
|
||||
},
|
||||
}),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
|
||||
|
||||
package dataloader
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
)
|
||||
|
||||
// QualificationLoaderConfig captures the config to create a new QualificationLoader
|
||||
type QualificationLoaderConfig struct {
|
||||
// Fetch is a method that provides the data for the loader
|
||||
Fetch func(keys []int) ([]*models.Qualification, []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
|
||||
}
|
||||
|
||||
// NewQualificationLoader creates a new QualificationLoader given a fetch, wait, and maxBatch
|
||||
func NewQualificationLoader(config QualificationLoaderConfig) *QualificationLoader {
|
||||
return &QualificationLoader{
|
||||
fetch: config.Fetch,
|
||||
wait: config.Wait,
|
||||
maxBatch: config.MaxBatch,
|
||||
}
|
||||
}
|
||||
|
||||
// QualificationLoader batches and caches requests
|
||||
type QualificationLoader struct {
|
||||
// this method provides the data for the loader
|
||||
fetch func(keys []int) ([]*models.Qualification, []error)
|
||||
|
||||
// how long to done before sending a batch
|
||||
wait time.Duration
|
||||
|
||||
// this will limit the maximum number of keys to send in one batch, 0 = no limit
|
||||
maxBatch int
|
||||
|
||||
// INTERNAL
|
||||
|
||||
// lazily created cache
|
||||
cache map[int]*models.Qualification
|
||||
|
||||
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
// then everything will be sent to the fetch method and out to the listeners
|
||||
batch *qualificationLoaderBatch
|
||||
|
||||
// mutex to prevent races
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type qualificationLoaderBatch struct {
|
||||
keys []int
|
||||
data []*models.Qualification
|
||||
error []error
|
||||
closing bool
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// Load a Qualification by key, batching and caching will be applied automatically
|
||||
func (l *QualificationLoader) Load(key int) (*models.Qualification, error) {
|
||||
return l.LoadThunk(key)()
|
||||
}
|
||||
|
||||
// LoadThunk returns a function that when called will block waiting for a Qualification.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationLoader) LoadThunk(key int) func() (*models.Qualification, error) {
|
||||
l.mu.Lock()
|
||||
if it, ok := l.cache[key]; ok {
|
||||
l.mu.Unlock()
|
||||
return func() (*models.Qualification, error) {
|
||||
return it, nil
|
||||
}
|
||||
}
|
||||
if l.batch == nil {
|
||||
l.batch = &qualificationLoaderBatch{done: make(chan struct{})}
|
||||
}
|
||||
batch := l.batch
|
||||
pos := batch.keyIndex(l, key)
|
||||
l.mu.Unlock()
|
||||
|
||||
return func() (*models.Qualification, error) {
|
||||
<-batch.done
|
||||
|
||||
var data *models.Qualification
|
||||
if pos < len(batch.data) {
|
||||
data = batch.data[pos]
|
||||
}
|
||||
|
||||
var err error
|
||||
// its convenient to be able to return a single error for everything
|
||||
if len(batch.error) == 1 {
|
||||
err = batch.error[0]
|
||||
} else if batch.error != nil {
|
||||
err = batch.error[pos]
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
l.mu.Lock()
|
||||
l.unsafeSet(key, data)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
// sub batches depending on how the loader is configured
|
||||
func (l *QualificationLoader) LoadAll(keys []int) ([]*models.Qualification, []error) {
|
||||
results := make([]func() (*models.Qualification, error), len(keys))
|
||||
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
|
||||
qualifications := make([]*models.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
}
|
||||
return qualifications, errors
|
||||
}
|
||||
|
||||
// LoadAllThunk returns a function that when called will block waiting for a Qualifications.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationLoader) LoadAllThunk(keys []int) func() ([]*models.Qualification, []error) {
|
||||
results := make([]func() (*models.Qualification, error), len(keys))
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
return func() ([]*models.Qualification, []error) {
|
||||
qualifications := make([]*models.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
}
|
||||
return qualifications, errors
|
||||
}
|
||||
}
|
||||
|
||||
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
// and false is returned.
|
||||
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
func (l *QualificationLoader) Prime(key int, value *models.Qualification) bool {
|
||||
l.mu.Lock()
|
||||
var found bool
|
||||
if _, found = l.cache[key]; !found {
|
||||
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
||||
// and end up with the whole cache pointing to the same value.
|
||||
cpy := *value
|
||||
l.unsafeSet(key, &cpy)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return !found
|
||||
}
|
||||
|
||||
// Clear the value at key from the cache, if it exists
|
||||
func (l *QualificationLoader) Clear(key int) {
|
||||
l.mu.Lock()
|
||||
delete(l.cache, key)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *QualificationLoader) unsafeSet(key int, value *models.Qualification) {
|
||||
if l.cache == nil {
|
||||
l.cache = map[int]*models.Qualification{}
|
||||
}
|
||||
l.cache[key] = value
|
||||
}
|
||||
|
||||
// keyIndex will return the location of the key in the batch, if its not found
|
||||
// it will add the key to the batch
|
||||
func (b *qualificationLoaderBatch) keyIndex(l *QualificationLoader, key int) int {
|
||||
for i, existingKey := range b.keys {
|
||||
if key == existingKey {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
pos := len(b.keys)
|
||||
b.keys = append(b.keys, key)
|
||||
if pos == 0 {
|
||||
go b.startTimer(l)
|
||||
}
|
||||
|
||||
if l.maxBatch != 0 && pos >= l.maxBatch-1 {
|
||||
if !b.closing {
|
||||
b.closing = true
|
||||
l.batch = nil
|
||||
go b.end(l)
|
||||
}
|
||||
}
|
||||
|
||||
return pos
|
||||
}
|
||||
|
||||
func (b *qualificationLoaderBatch) startTimer(l *QualificationLoader) {
|
||||
time.Sleep(l.wait)
|
||||
l.mu.Lock()
|
||||
|
||||
// we must have hit a batch limit and are already finalizing this batch
|
||||
if b.closing {
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
l.batch = nil
|
||||
l.mu.Unlock()
|
||||
|
||||
b.end(l)
|
||||
}
|
||||
|
||||
func (b *qualificationLoaderBatch) end(l *QualificationLoader) {
|
||||
b.data, b.error = l.fetch(b.keys)
|
||||
close(b.done)
|
||||
}
|
|
@ -5,8 +5,8 @@ package resolvers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/gin/middleware"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
|
@ -57,5 +57,15 @@ func (r *queryResolver) Questions(
|
|||
}
|
||||
|
||||
func (r *questionResolver) Qualification(ctx context.Context, obj *models.Question) (*models.Qualification, error) {
|
||||
panic(fmt.Errorf("not implemented"))
|
||||
if obj != nil && obj.Qualification != nil {
|
||||
return obj.Qualification, nil
|
||||
}
|
||||
|
||||
if obj != nil || obj.QualificationID > 0 {
|
||||
if dataloader, err := middleware.DataLoaderFromContext(ctx); err == nil && dataloader != nil {
|
||||
return dataloader.QualificationByID.Load(obj.QualificationID)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Reference in New Issue