github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/dataloaders/fetchRequestDocumentDataLoader.go (about) 1 //go:generate go run github.com/vektah/dataloaden FetchRequestDocumentLoader ParamFetchRequestDocument *github.com/kyma-incubator/compass/components/director/pkg/graphql.FetchRequest 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 loadersKeyFetchRequestDocument contextKey = "dataloadersFetchRequestDocument" 14 15 // LoadersFetchRequestDocument missing godoc 16 type LoadersFetchRequestDocument struct { 17 FetchRequestDocumentByID FetchRequestDocumentLoader 18 } 19 20 // ParamFetchRequestDocument missing godoc 21 type ParamFetchRequestDocument struct { 22 ID string 23 Ctx context.Context 24 } 25 26 // HandlerFetchRequestDocument missing godoc 27 func HandlerFetchRequestDocument(fetchFunc func(keys []ParamFetchRequestDocument) ([]*graphql.FetchRequest, []error), maxBatch int, wait time.Duration) func(next http.Handler) http.Handler { 28 return func(next http.Handler) http.Handler { 29 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 ctx := context.WithValue(r.Context(), loadersKeyFetchRequestDocument, &LoadersFetchRequestDocument{ 31 FetchRequestDocumentByID: FetchRequestDocumentLoader{ 32 maxBatch: maxBatch, 33 wait: wait, 34 fetch: fetchFunc, 35 }, 36 }) 37 r = r.WithContext(ctx) 38 next.ServeHTTP(w, r) 39 }) 40 } 41 } 42 43 // ForFetchRequestDocument missing godoc 44 func ForFetchRequestDocument(ctx context.Context) *LoadersFetchRequestDocument { 45 return ctx.Value(loadersKeyFetchRequestDocument).(*LoadersFetchRequestDocument) 46 }