s3browser-backend/internal/schema.go

152 lines
3.7 KiB
Go
Raw Normal View History

2021-07-26 12:52:36 +00:00
package s3browser
import (
"fmt"
"github.com/graph-gophers/dataloader"
"github.com/graphql-go/graphql"
)
// graphqlSchema generate the schema with its root query and mutation
func graphqlSchema() (graphql.Schema, error) {
2021-08-06 23:19:36 +00:00
queryFields := graphql.Fields{
2021-07-26 12:52:36 +00:00
"files": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphqlFileType)),
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
path, ok := p.Args["path"].(string)
if !ok {
return nil, nil
}
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
thunk := loader["getFiles"].Load(p.Context, dataloader.StringKey(path))
return thunk()
},
},
"directorys": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphqlDirType)),
Args: graphql.FieldConfigArgument{
"path": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
path, ok := p.Args["path"].(string)
if !ok {
return nil, nil
}
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
thunk := loader["getDirs"].Load(p.Context, dataloader.StringKey(path))
return thunk()
},
},
"file": &graphql.Field{
Type: graphqlFileType,
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 File{
ID: id,
}, nil
},
},
}
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(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)
},
},
}
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)
}