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

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