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

     1  //go:generate go run github.com/vektah/dataloaden APIDefLoader ParamAPIDef *github.com/kyma-incubator/compass/components/director/pkg/graphql.APIDefinitionPage
     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  type contextKey string
    14  
    15  const loadersKeyAPIDef contextKey = "dataloadersAPIDef"
    16  
    17  // APIDefLoaders missing godoc
    18  type APIDefLoaders struct {
    19  	APIDefByID APIDefLoader
    20  }
    21  
    22  // ParamAPIDef missing godoc
    23  type ParamAPIDef struct {
    24  	ID    string
    25  	Ctx   context.Context
    26  	First *int
    27  	After *graphql.PageCursor
    28  }
    29  
    30  // HandlerAPIDef missing godoc
    31  func HandlerAPIDef(fetchFunc func(keys []ParamAPIDef) ([]*graphql.APIDefinitionPage, []error), maxBatch int, wait time.Duration) func(next http.Handler) http.Handler {
    32  	return func(next http.Handler) http.Handler {
    33  		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    34  			ctx := context.WithValue(r.Context(), loadersKeyAPIDef, &APIDefLoaders{
    35  				APIDefByID: APIDefLoader{
    36  					maxBatch: maxBatch,
    37  					wait:     wait,
    38  					fetch:    fetchFunc,
    39  				},
    40  			})
    41  			r = r.WithContext(ctx)
    42  			next.ServeHTTP(w, r)
    43  		})
    44  	}
    45  }
    46  
    47  // APIDefFor missing godoc
    48  func APIDefFor(ctx context.Context) *APIDefLoaders {
    49  	return ctx.Value(loadersKeyAPIDef).(*APIDefLoaders)
    50  }