From d2b036444531dea60e72940e3f70ad89cc7b33b1 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 29 Sep 2021 21:41:50 +0200 Subject: [PATCH] added list buckets query --- internal/dataloader.go | 39 +++++++++++++++++++++++++++++++++++++++ internal/gql/schema.go | 15 +++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/internal/dataloader.go b/internal/dataloader.go index e6c293c..4d865c0 100644 --- a/internal/dataloader.go +++ b/internal/dataloader.go @@ -227,6 +227,40 @@ func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result { return results } +func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result { + log.Debug("listBucketsBatch") + var results []*dataloader.Result + + s3Client, ok := c.Value("s3Client").(*minio.Client) + + if !ok { + return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context")) + } + + buckets, err := s3Client.ListBuckets(c) + + if err != nil { + return handleLoaderError(k, err) + } + + var bucketStrings []string + + for _, v := range buckets { + bucketStrings = append(bucketStrings, v.Name) + } + + result := &dataloader.Result{ + Data: bucketStrings, + Error: nil, + } + + for range k { + results = append(results, result) + } + + return results +} + // createDataloader create all dataloaders and return a map of them plus a cache for objects func createDataloader(config types.AppConfig) map[string]*dataloader.Loader { loaderMap := make(map[string]*dataloader.Loader, 0) @@ -256,5 +290,10 @@ func createDataloader(config types.AppConfig) map[string]*dataloader.Loader { dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), ) + loaderMap["listBuckets"] = dataloader.NewBatchedLoader( + listBucketsBatch, + dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), + ) + return loaderMap } diff --git a/internal/gql/schema.go b/internal/gql/schema.go index 0668f70..059f088 100644 --- a/internal/gql/schema.go +++ b/internal/gql/schema.go @@ -100,6 +100,21 @@ func GraphqlSchema() (graphql.Schema, error) { return auth, nil }, }, + "buckets": &graphql.Field{ + Name: "buckets", + Type: graphql.NewNonNull(graphql.NewList(graphql.String)), + Description: "List available buckets", + Resolve: func(p graphql.ResolveParams) (interface{}, error) { + if !helper.IsAuthenticated(p.Context) { + return nil, s3errors.ErrNotAuthenticated + } + + loader := p.Context.Value("loader").(map[string]*dataloader.Loader) + // The only reason we use a dataloader with a empty key is that we want to cache the result + thunk := loader["listBuckets"].Load(p.Context, dataloader.StringKey("")) + return thunk() + }, + }, } mutationFields := graphql.Fields{