291 lines
7.5 KiB
Go
291 lines
7.5 KiB
Go
package gql
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/graph-gophers/dataloader"
|
|
"github.com/graphql-go/graphql"
|
|
|
|
s3errors "git.kapelle.org/niklas/s3browser/internal/errors"
|
|
helper "git.kapelle.org/niklas/s3browser/internal/helper"
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
//GraphqlSchema generate the schema with its root query and mutation
|
|
func GraphqlSchema() (graphql.Schema, error) {
|
|
|
|
queryFields := graphql.Fields{
|
|
"files": &graphql.Field{
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlFileType))),
|
|
Args: graphql.FieldConfigArgument{
|
|
"path": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
path, ok := p.Args["path"].(*types.ID)
|
|
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("querry 'files': ", path)
|
|
|
|
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
|
thunk := loader["getFiles"].Load(p.Context, path)
|
|
return thunk()
|
|
},
|
|
},
|
|
"directories": &graphql.Field{
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlDirType))),
|
|
Args: graphql.FieldConfigArgument{
|
|
"path": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
path, ok := p.Args["path"].(*types.ID)
|
|
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
|
|
log.Debug("querry 'directorys': ", path)
|
|
|
|
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
|
thunk := loader["getDirs"].Load(p.Context, path)
|
|
return thunk()
|
|
},
|
|
},
|
|
"file": &graphql.Field{
|
|
Type: graphqlFileType,
|
|
Args: graphql.FieldConfigArgument{
|
|
"id": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
id, ok := p.Args["id"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("querry 'file': ", id)
|
|
|
|
return types.File{
|
|
ID: *id,
|
|
}, nil
|
|
},
|
|
},
|
|
"authorized": &graphql.Field{
|
|
Name: "authorized",
|
|
Type: graphql.NewNonNull(graphql.Boolean),
|
|
Description: "True if the user is authorized",
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
auth := helper.IsAuthenticated(p.Context)
|
|
|
|
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{
|
|
"delete": &graphql.Field{
|
|
Type: graphql.String,
|
|
Args: graphql.FieldConfigArgument{
|
|
"id": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
id, ok := p.Args["id"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("mutation 'delete': ", id)
|
|
|
|
return id, deleteMutation(p.Context, *id)
|
|
},
|
|
},
|
|
"copy": &graphql.Field{
|
|
Type: graphqlFileType,
|
|
Args: graphql.FieldConfigArgument{
|
|
"src": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
"dest": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
src, ok := p.Args["src"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
dest, ok := p.Args["dest"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("mutation 'copy': ", src, "-->", dest)
|
|
|
|
return copyMutation(p.Context, *src, *dest)
|
|
},
|
|
},
|
|
"move": &graphql.Field{
|
|
Type: graphqlFileType,
|
|
Args: graphql.FieldConfigArgument{
|
|
"src": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
"dest": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
src, ok := p.Args["src"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
dest, ok := p.Args["dest"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("mutation 'move': ", src, "-->", dest)
|
|
|
|
return moveMutation(p.Context, *src, *dest)
|
|
},
|
|
},
|
|
"createDir": &graphql.Field{
|
|
Type: graphql.NewNonNull(graphqlDirType),
|
|
Args: graphql.FieldConfigArgument{
|
|
"path": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
path, ok := p.Args["path"].(*types.ID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("mutation 'createDir': ", path)
|
|
|
|
return createDirectory(p.Context, *path)
|
|
},
|
|
},
|
|
"deleteDir": &graphql.Field{
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
Args: graphql.FieldConfigArgument{
|
|
"path": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(objIDType),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
if !helper.IsAuthenticated(p.Context) {
|
|
return nil, s3errors.ErrNotAuthenticated
|
|
}
|
|
|
|
path, ok := p.Args["path"].(*types.ID)
|
|
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
log.Debug("mutation 'deleteDir': ", path)
|
|
|
|
return path, deleteDirectory(p.Context, *path)
|
|
},
|
|
},
|
|
"login": &graphql.Field{
|
|
Type: graphql.NewNonNull(graphqlLoginResultType),
|
|
Args: graphql.FieldConfigArgument{
|
|
"username": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
},
|
|
"password": &graphql.ArgumentConfig{
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
username, ok := p.Args["username"].(string)
|
|
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
password, ok := p.Args["password"].(string)
|
|
|
|
if !ok {
|
|
return nil, fmt.Errorf("Failed to parse args")
|
|
}
|
|
|
|
return login(p.Context, username, password)
|
|
},
|
|
},
|
|
}
|
|
|
|
rootQuery := graphql.ObjectConfig{
|
|
Name: "RootQuery",
|
|
Fields: queryFields,
|
|
}
|
|
|
|
rootMutation := graphql.ObjectConfig{
|
|
Name: "RootMutation",
|
|
Fields: mutationFields,
|
|
}
|
|
|
|
schemaConfig := graphql.SchemaConfig{
|
|
Query: graphql.NewObject(rootQuery),
|
|
Mutation: graphql.NewObject(rootMutation),
|
|
}
|
|
|
|
return graphql.NewSchema(schemaConfig)
|
|
}
|