Compare commits
3 Commits
31e4586d68
...
926701125d
| Author | SHA1 | Date | |
|---|---|---|---|
| 926701125d | |||
| ad80860368 | |||
| 818264093f |
107
internal/mutations.go
Normal file
107
internal/mutations.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package s3browser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/graph-gophers/dataloader"
|
||||||
|
"github.com/minio/minio-go/v7"
|
||||||
|
)
|
||||||
|
|
||||||
|
func deleteMutation(ctx context.Context, id string) error {
|
||||||
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Failed to get s3Client from context")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: it is posible to remove multiple objects with a single call.
|
||||||
|
// Is it better to batch this?
|
||||||
|
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) {
|
||||||
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
||||||
|
Bucket: bucketName,
|
||||||
|
Object: dest,
|
||||||
|
}, minio.CopySrcOptions{
|
||||||
|
Bucket: bucketName,
|
||||||
|
Object: src,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func moveMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||||
|
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is no (spoon) move. Only copy and delete
|
||||||
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
||||||
|
Bucket: bucketName,
|
||||||
|
Object: dest,
|
||||||
|
}, minio.CopySrcOptions{
|
||||||
|
Bucket: bucketName,
|
||||||
|
Object: src,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = deleteMutation(ctx, src)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &File{
|
||||||
|
ID: info.Key,
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,9 +10,9 @@ import (
|
|||||||
// graphqlSchema generate the schema with its root query and mutation
|
// graphqlSchema generate the schema with its root query and mutation
|
||||||
func graphqlSchema() (graphql.Schema, error) {
|
func graphqlSchema() (graphql.Schema, error) {
|
||||||
|
|
||||||
fields := graphql.Fields{
|
queryFields := graphql.Fields{
|
||||||
"files": &graphql.Field{
|
"files": &graphql.Field{
|
||||||
Type: graphql.NewNonNull(graphql.NewList(graphqlFileType)),
|
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlFileType))),
|
||||||
Args: graphql.FieldConfigArgument{
|
Args: graphql.FieldConfigArgument{
|
||||||
"path": &graphql.ArgumentConfig{
|
"path": &graphql.ArgumentConfig{
|
||||||
Type: graphql.NewNonNull(graphql.String),
|
Type: graphql.NewNonNull(graphql.String),
|
||||||
@@ -30,7 +30,7 @@ func graphqlSchema() (graphql.Schema, error) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"directorys": &graphql.Field{
|
"directorys": &graphql.Field{
|
||||||
Type: graphql.NewNonNull(graphql.NewList(graphqlDirType)),
|
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlDirType))),
|
||||||
Args: graphql.FieldConfigArgument{
|
Args: graphql.FieldConfigArgument{
|
||||||
"path": &graphql.ArgumentConfig{
|
"path": &graphql.ArgumentConfig{
|
||||||
Type: graphql.NewNonNull(graphql.String),
|
Type: graphql.NewNonNull(graphql.String),
|
||||||
@@ -67,13 +67,84 @@ func graphqlSchema() (graphql.Schema, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mutationFields := graphql.Fields{
|
||||||
|
"delete": &graphql.Field{
|
||||||
|
Type: graphql.String,
|
||||||
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"id": &graphql.ArgumentConfig{
|
||||||
|
Type: graphql.NewNonNull(graphql.ID),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
|
id, ok := p.Args["id"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to parse args")
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, deleteMutation(p.Context, id)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"copy": &graphql.Field{
|
||||||
|
Type: graphqlFileType,
|
||||||
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"src": &graphql.ArgumentConfig{
|
||||||
|
Type: graphql.NewNonNull(graphql.ID),
|
||||||
|
},
|
||||||
|
"dest": &graphql.ArgumentConfig{
|
||||||
|
Type: graphql.NewNonNull(graphql.ID),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
|
src, ok := p.Args["src"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to parse args")
|
||||||
|
}
|
||||||
|
dest, ok := p.Args["dest"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to parse args")
|
||||||
|
}
|
||||||
|
|
||||||
|
return copyMutation(p.Context, src, dest)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"move": &graphql.Field{
|
||||||
|
Type: graphqlFileType,
|
||||||
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"src": &graphql.ArgumentConfig{
|
||||||
|
Type: graphql.NewNonNull(graphql.ID),
|
||||||
|
},
|
||||||
|
"dest": &graphql.ArgumentConfig{
|
||||||
|
Type: graphql.NewNonNull(graphql.ID),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
|
src, ok := p.Args["src"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to parse args")
|
||||||
|
}
|
||||||
|
dest, ok := p.Args["dest"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Failed to parse args")
|
||||||
|
}
|
||||||
|
|
||||||
|
return moveMutation(p.Context, src, dest)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
rootQuery := graphql.ObjectConfig{
|
rootQuery := graphql.ObjectConfig{
|
||||||
Name: "RootQuery",
|
Name: "RootQuery",
|
||||||
Fields: fields,
|
Fields: queryFields,
|
||||||
|
}
|
||||||
|
|
||||||
|
rootMutation := graphql.ObjectConfig{
|
||||||
|
Name: "RootMutation",
|
||||||
|
Fields: mutationFields,
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaConfig := graphql.SchemaConfig{
|
schemaConfig := graphql.SchemaConfig{
|
||||||
Query: graphql.NewObject(rootQuery),
|
Query: graphql.NewObject(rootQuery),
|
||||||
|
Mutation: graphql.NewObject(rootMutation),
|
||||||
}
|
}
|
||||||
|
|
||||||
return graphql.NewSchema(schemaConfig)
|
return graphql.NewSchema(schemaConfig)
|
||||||
|
|||||||
Reference in New Issue
Block a user