github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/dataloaders/bundleLoaders.go (about) 1 //go:generate go run github.com/vektah/dataloaden BundleLoader ParamBundle *github.com/kyma-incubator/compass/components/director/pkg/graphql.BundlePage 2 3 package dataloader 4 5 import ( 6 "context" 7 "net/http" 8 "time" 9 10 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 11 ) 12 13 const loadersKeyBundle contextKey = "dataloadersBundle" 14 15 // BundleLoaders missing godoc 16 type BundleLoaders struct { 17 BundleByID BundleLoader 18 } 19 20 // ParamBundle missing godoc 21 type ParamBundle struct { 22 ID string 23 First *int 24 After *graphql.PageCursor 25 Ctx context.Context 26 } 27 28 // HandlerBundle missing godoc 29 func HandlerBundle(fetchFunc func(keys []ParamBundle) ([]*graphql.BundlePage, []error), maxBatch int, wait time.Duration) func(next http.Handler) http.Handler { 30 return func(next http.Handler) http.Handler { 31 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 32 ctx := context.WithValue(r.Context(), loadersKeyBundle, &BundleLoaders{ 33 BundleByID: BundleLoader{ 34 maxBatch: maxBatch, 35 wait: wait, 36 fetch: fetchFunc, 37 }, 38 }) 39 r = r.WithContext(ctx) 40 next.ServeHTTP(w, r) 41 }) 42 } 43 } 44 45 // BundleFor missing godoc 46 func BundleFor(ctx context.Context) *BundleLoaders { 47 return ctx.Value(loadersKeyBundle).(*BundleLoaders) 48 }