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

     1  //go:generate go run github.com/vektah/dataloaden RuntimeContextLoader ParamRuntimeContext *github.com/kyma-incubator/compass/components/director/pkg/graphql.RuntimeContextPage
     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 loadersKeyRuntimeContext contextKey = "dataloadersRuntimeContext"
    14  
    15  // RuntimeContextLoaders missing godoc
    16  type RuntimeContextLoaders struct {
    17  	RuntimeContextByID RuntimeContextLoader
    18  }
    19  
    20  // ParamRuntimeContext missing godoc
    21  type ParamRuntimeContext struct {
    22  	ID    string
    23  	First *int
    24  	After *graphql.PageCursor
    25  	Ctx   context.Context
    26  }
    27  
    28  // HandlerRuntimeContext missing godoc
    29  func HandlerRuntimeContext(fetchFunc func(keys []ParamRuntimeContext) ([]*graphql.RuntimeContextPage, []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(), loadersKeyRuntimeContext, &RuntimeContextLoaders{
    33  				RuntimeContextByID: RuntimeContextLoader{
    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  // RuntimeContextFor missing godoc
    46  func RuntimeContextFor(ctx context.Context) *RuntimeContextLoaders {
    47  	return ctx.Value(loadersKeyRuntimeContext).(*RuntimeContextLoaders)
    48  }