github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/dataloaders/fetchRequestApiDefLoaders.go (about)

     1  //go:generate go run github.com/vektah/dataloaden FetchRequestAPIDefLoader ParamFetchRequestAPIDef *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 loadersKeyFetchRequestAPIDef contextKey = "dataloadersFetchRequestAPIDef"
    14  
    15  // LoadersFetchRequestAPIDef missing godoc
    16  type LoadersFetchRequestAPIDef struct {
    17  	FetchRequestAPIDefByID FetchRequestAPIDefLoader
    18  }
    19  
    20  // ParamFetchRequestAPIDef missing godoc
    21  type ParamFetchRequestAPIDef struct {
    22  	ID  string
    23  	Ctx context.Context
    24  }
    25  
    26  // HandlerFetchRequestAPIDef missing godoc
    27  func HandlerFetchRequestAPIDef(fetchFunc func(keys []ParamFetchRequestAPIDef) ([]*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(), loadersKeyFetchRequestAPIDef, &LoadersFetchRequestAPIDef{
    31  				FetchRequestAPIDefByID: FetchRequestAPIDefLoader{
    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  // ForFetchRequestAPIDef missing godoc
    44  func ForFetchRequestAPIDef(ctx context.Context) *LoadersFetchRequestAPIDef {
    45  	return ctx.Value(loadersKeyFetchRequestAPIDef).(*LoadersFetchRequestAPIDef)
    46  }