s3browser-backend/internal/gql/schema.go

320 lines
8.1 KiB
Go
Raw Permalink Normal View History

2021-09-24 13:39:23 +00:00
package gql
2021-07-26 12:52:36 +00:00
import (
"fmt"
"github.com/graphql-go/graphql"
2021-08-12 15:48:28 +00:00
2021-09-24 13:49:51 +00:00
s3errors "git.kapelle.org/niklas/s3browser/internal/errors"
2021-09-24 13:39:23 +00:00
helper "git.kapelle.org/niklas/s3browser/internal/helper"
2021-10-14 17:00:11 +00:00
"git.kapelle.org/niklas/s3browser/internal/loader"
2021-09-24 13:39:23 +00:00
types "git.kapelle.org/niklas/s3browser/internal/types"
2021-08-12 15:48:28 +00:00
log "github.com/sirupsen/logrus"
2021-07-26 12:52:36 +00:00
)
2021-09-24 13:39:23 +00:00
//GraphqlSchema generate the schema with its root query and mutation
func GraphqlSchema() (graphql.Schema, error) {
2021-07-26 12:52:36 +00:00
2022-02-07 14:53:14 +00:00
if !typesInit {
GraphqlTypes()
}
2021-08-06 23:19:36 +00:00
queryFields := graphql.Fields{
2021-07-26 12:52:36 +00:00
"files": &graphql.Field{
2021-08-10 09:22:10 +00:00
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlFileType))),
2021-07-26 12:52:36 +00:00
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-07-26 12:52:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
path, ok := p.Args["path"].(*types.ID)
2021-07-26 12:52:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
2021-07-26 12:52:36 +00:00
}
2021-08-12 15:48:28 +00:00
log.Debug("querry 'files': ", path)
2021-10-14 17:00:11 +00:00
loader := p.Context.Value("loader").(*loader.Loader)
return loader.GetFiles(p.Context, *path)
2021-07-26 12:52:36 +00:00
},
},
2021-09-27 17:48:57 +00:00
"directories": &graphql.Field{
2021-08-10 09:22:10 +00:00
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphqlDirType))),
2021-07-26 12:52:36 +00:00
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-07-26 12:52:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
path, ok := p.Args["path"].(*types.ID)
2021-07-26 12:52:36 +00:00
if !ok {
return nil, nil
}
2021-08-12 15:48:28 +00:00
log.Debug("querry 'directorys': ", path)
2021-10-14 17:00:11 +00:00
loader := p.Context.Value("loader").(*loader.Loader)
return loader.GetDirs(p.Context, *path)
2021-07-26 12:52:36 +00:00
},
},
"file": &graphql.Field{
Type: graphqlFileType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-07-26 12:52:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
id, ok := p.Args["id"].(*types.ID)
2021-07-26 12:52:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
2021-08-12 15:48:28 +00:00
log.Debug("querry 'file': ", id)
2021-09-24 13:39:23 +00:00
return types.File{
ID: *id,
2021-07-26 12:52:36 +00:00
}, 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) {
2021-09-24 13:49:51 +00:00
auth := helper.IsAuthenticated(p.Context)
return auth, nil
},
},
2021-09-29 19:41:50 +00:00
"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
}
2021-10-14 17:00:11 +00:00
loader := p.Context.Value("loader").(*loader.Loader)
return loader.GetBuckets(p.Context)
2021-09-29 19:41:50 +00:00
},
},
2021-07-26 12:52:36 +00:00
}
2021-08-06 23:19:36 +00:00
mutationFields := graphql.Fields{
"delete": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-06 23:19:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
id, ok := p.Args["id"].(*types.ID)
2021-08-06 23:19:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
2021-08-12 15:48:28 +00:00
log.Debug("mutation 'delete': ", id)
return id, deleteMutation(p.Context, *id)
2021-08-06 23:19:36 +00:00
},
},
"copy": &graphql.Field{
Type: graphqlFileType,
Args: graphql.FieldConfigArgument{
"src": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-06 23:19:36 +00:00
},
"dest": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-06 23:19:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
src, ok := p.Args["src"].(*types.ID)
2021-08-06 23:19:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
dest, ok := p.Args["dest"].(*types.ID)
2021-08-06 23:19:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
2021-08-12 15:48:28 +00:00
log.Debug("mutation 'copy': ", src, "-->", dest)
return copyMutation(p.Context, *src, *dest)
2021-08-06 23:19:36 +00:00
},
},
"move": &graphql.Field{
2021-11-01 23:33:55 +00:00
Type: graphql.NewNonNull(graphqlFileType),
2021-08-06 23:19:36 +00:00
Args: graphql.FieldConfigArgument{
"src": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-06 23:19:36 +00:00
},
"dest": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-06 23:19:36 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
src, ok := p.Args["src"].(*types.ID)
2021-08-06 23:19:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
dest, ok := p.Args["dest"].(*types.ID)
2021-08-06 23:19:36 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
2021-08-12 15:48:28 +00:00
log.Debug("mutation 'move': ", src, "-->", dest)
2021-11-01 23:33:55 +00:00
return moveFileMutation(p.Context, *src, *dest)
},
},
"moveDir": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(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 'moveDir': ", src, "-->", dest)
return moveDirMutation(p.Context, *src, *dest)
2021-08-06 23:19:36 +00:00
},
},
2021-08-16 17:57:48 +00:00
"createDir": &graphql.Field{
Type: graphql.NewNonNull(graphqlDirType),
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-16 17:57:48 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
path, ok := p.Args["path"].(*types.ID)
2021-08-16 17:57:48 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
log.Debug("mutation 'createDir': ", path)
return createDirectory(p.Context, *path)
2021-08-16 17:57:48 +00:00
},
},
2021-08-20 19:38:22 +00:00
"deleteDir": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(objIDType),
2021-08-20 19:38:22 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2021-09-24 15:03:06 +00:00
if !helper.IsAuthenticated(p.Context) {
2021-09-24 13:49:51 +00:00
return nil, s3errors.ErrNotAuthenticated
2021-09-14 14:51:01 +00:00
}
path, ok := p.Args["path"].(*types.ID)
2021-08-20 19:38:22 +00:00
if !ok {
return nil, fmt.Errorf("Failed to parse args")
}
log.Debug("mutation 'deleteDir': ", path)
return path, deleteDirectory(p.Context, *path)
2021-08-20 19:38:22 +00:00
},
},
2021-09-14 13:16:37 +00:00
"login": &graphql.Field{
2021-09-14 14:22:32 +00:00
Type: graphql.NewNonNull(graphqlLoginResultType),
2021-09-14 13:16:37 +00:00
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)
},
},
2021-08-06 23:19:36 +00:00
}
2021-07-26 12:52:36 +00:00
rootQuery := graphql.ObjectConfig{
Name: "RootQuery",
2021-08-06 23:19:36 +00:00
Fields: queryFields,
}
rootMutation := graphql.ObjectConfig{
Name: "RootMutation",
Fields: mutationFields,
2021-07-26 12:52:36 +00:00
}
schemaConfig := graphql.SchemaConfig{
2021-08-06 23:19:36 +00:00
Query: graphql.NewObject(rootQuery),
Mutation: graphql.NewObject(rootMutation),
2021-07-26 12:52:36 +00:00
}
return graphql.NewSchema(schemaConfig)
}