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) { fields := graphql.Fields{ "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 }, }, } rootQuery := graphql.ObjectConfig{ Name: "RootQuery", Fields: fields, } schemaConfig := graphql.SchemaConfig{ Query: graphql.NewObject(rootQuery), } return graphql.NewSchema(schemaConfig) }