Compare commits

...

2 Commits

Author SHA1 Message Date
33237b6564 moved stuff to helper 2021-08-14 14:13:49 +02:00
447a0647b2 logging on gql errors 2021-08-14 01:33:42 +02:00
4 changed files with 51 additions and 32 deletions

View File

@@ -140,11 +140,7 @@ func graphqlTypes() {
return nil, fmt.Errorf("Failed to parse Source for parent resolve")
}
basename := filepath.Dir(source.ID)
if basename == "." {
basename = "/"
}
basename := getPathFromId(source.ID)
return Directory{
ID: basename,
@@ -213,6 +209,10 @@ func loadFile(p graphql.ResolveParams) (*File, error) {
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID))
result, err := thunk()
if err != nil {
return nil, err
}
file, ok := result.(*File)
if !ok {

32
internal/helper.go Normal file
View File

@@ -0,0 +1,32 @@
package s3browser
import (
"context"
"fmt"
"path/filepath"
"github.com/graph-gophers/dataloader"
)
func invalidateCache(ctx context.Context, id string) error {
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
if !ok {
return fmt.Errorf("Failed to get loader from context")
}
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
loader["getFiles"].Clear(ctx, dataloader.StringKey(getPathFromId(id)))
return nil
}
func getPathFromId(id string) string {
dir := filepath.Dir(id)
if dir == "." {
return "/"
}
return dir + "/"
}

View File

@@ -8,8 +8,8 @@ import (
"net/http"
"path/filepath"
"github.com/graph-gophers/dataloader"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/handler"
"github.com/minio/minio-go/v7"
@@ -23,6 +23,15 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
Pretty: true,
GraphiQL: false,
Playground: true,
FormatErrorFn: func(err error) gqlerrors.FormattedError {
switch err := err.(type) {
case gqlerrors.FormattedError:
log.Error("GQL: ", err.Message)
case *gqlerrors.Error:
log.Errorf("GQL: '%s' at '%v'", err.Message, err.Path)
}
return gqlerrors.FormatError(err)
},
})
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
@@ -85,7 +94,6 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
s3Client := ctx.Value("s3Client").(*minio.Client)
loader := ctx.Value("loader").(map[string]*dataloader.Loader)
id := r.URL.Query().Get("id")
@@ -105,9 +113,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
}
// Invalidate cache
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
invalidateCache(ctx, info.Key)
rw.WriteHeader(http.StatusCreated)
}

View File

@@ -3,9 +3,7 @@ package s3browser
import (
"context"
"fmt"
"path/filepath"
"github.com/graph-gophers/dataloader"
"github.com/minio/minio-go/v7"
)
@@ -25,16 +23,7 @@ func deleteMutation(ctx context.Context, id string) error {
}
// Invalidate cache
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
if !ok {
return fmt.Errorf("Failed to get loader from context")
}
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id)))
return nil
return invalidateCache(ctx, id)
}
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
@@ -57,16 +46,8 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) {
}
// Invalidate cache
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
// TODO: Do we want to error when the operation
// has succeeded but the cache invalidation has failed ?
if ok {
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
}
// TODO: check error
invalidateCache(ctx, info.Key)
return &File{
ID: info.Key,