stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
1.4 KiB

package loaders
import (
"context"
"git.aiterp.net/stufflog/server/database/repositories"
"git.aiterp.net/stufflog/server/internal/slerrors"
"git.aiterp.net/stufflog/server/models"
"time"
)
// go run github.com/vektah/dataloaden ActivityLoader string \*git.aiterp.net/stufflog/server/models.Activity
var activityLoaderCtxKey = "ctx.stufflog.ActivityLoader"
func ActivityLoaderFromContext(ctx context.Context) *ActivityLoader {
return ctx.Value(activityLoaderCtxKey).(*ActivityLoader)
}
func NewActivityLoader(ctx context.Context, activityRepo repositories.ActivityRepository) *ActivityLoader {
return &ActivityLoader{
fetch: func(keys []string) ([]*models.Activity, []error) {
results := make([]*models.Activity, len(keys))
errors := make([]error, len(keys))
activities, err := activityRepo.List(ctx, models.ActivityFilter{ActivityIDs: keys})
if err != nil {
for i := range errors {
errors[i] = err
}
return results, errors
}
for i, key := range keys {
found := false
for j, activity := range activities {
if activity.ID == key {
found = true
results[i] = activity
activities[j] = activities[len(activities)-1]
activities = activities[:len(activities)-1]
break
}
}
if !found {
errors[i] = slerrors.NotFound("Activity")
}
}
return results, errors
},
wait: time.Millisecond / 2,
maxBatch: 16,
}
}