diff --git a/internal/mutations.go b/internal/mutations.go index 4c74b7d..1216fae 100644 --- a/internal/mutations.go +++ b/internal/mutations.go @@ -3,7 +3,9 @@ package s3browser import ( "context" "fmt" + "path/filepath" + "github.com/graph-gophers/dataloader" "github.com/minio/minio-go/v7" ) @@ -16,7 +18,23 @@ func deleteMutation(ctx context.Context, id string) error { // TODO: it is posible to remove multiple objects with a single call. // Is it better to batch this? - return s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{}) + err := s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{}) + + if err != nil { + return err + } + + // 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 } func copyMutation(ctx context.Context, src, dest string) (*File, error) { @@ -38,6 +56,18 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) { return nil, err } + // 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))) + } + return &File{ ID: info.Key, }, nil