graphvent/gql_interfaces.go

173 lines
3.8 KiB
Go

package graphvent
import (
"github.com/graphql-go/graphql"
"reflect"
2023-07-26 00:18:11 -06:00
"fmt"
)
func NewField(init func()*graphql.Field) *graphql.Field {
return init()
}
type Singleton[K graphql.Type] struct {
Type K
List *graphql.List
}
func NewSingleton[K graphql.Type](init func() K, post_init func(K, *graphql.List)) *Singleton[K] {
val := init()
list := graphql.NewList(val)
if post_init != nil {
post_init(val, list)
}
return &Singleton[K]{
Type: val,
List: list,
}
}
2023-07-26 00:18:11 -06:00
func AddNodeInterfaceFields(i *graphql.Interface) {
i.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
})
2023-07-26 00:18:11 -06:00
i.AddFieldConfig("TypeHash", &graphql.Field{
Type: graphql.String,
})
}
func PrepTypeResolve(p graphql.ResolveTypeParams) (*ResolveContext, error) {
resolve_context, ok := p.Context.Value("resolve").(*ResolveContext)
if ok == false {
return nil, fmt.Errorf("Bad resolve in params context")
}
return resolve_context, nil
}
var GQLInterfaceNode = NewSingleton(func() *graphql.Interface {
i := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Node",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
2023-07-26 00:18:11 -06:00
ctx, err := PrepTypeResolve(p)
if err != nil {
return nil
}
2023-07-26 00:18:11 -06:00
valid_nodes := ctx.GQLContext.ValidNodes
p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_nodes) {
if p_type == key {
return value
}
}
2023-07-26 00:18:11 -06:00
_, ok := p.Value.(Node)
2023-07-21 18:18:26 -06:00
if ok == true {
2023-07-26 00:18:11 -06:00
return ctx.GQLContext.BaseNodeType
}
return nil
},
Fields: graphql.Fields{},
})
2023-07-26 00:18:11 -06:00
AddNodeInterfaceFields(i)
return i
}, nil)
var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface {
gql_interface_lockable := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Lockable",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
2023-07-26 00:18:11 -06:00
ctx, err := PrepTypeResolve(p)
if err != nil {
return nil
}
2023-07-26 00:18:11 -06:00
valid_lockables := ctx.GQLContext.ValidLockables
p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_lockables) {
if p_type == key {
return value
}
}
2023-07-26 00:18:11 -06:00
_, ok := p.Value.(*Node)
if ok == false {
return ctx.GQLContext.BaseLockableType
}
return nil
},
Fields: graphql.Fields{},
})
return gql_interface_lockable
}, func(lockable *graphql.Interface, lockable_list *graphql.List) {
2023-07-26 00:18:11 -06:00
lockable.AddFieldConfig("Requirements", &graphql.Field{
Type: lockable_list,
})
2023-07-26 00:18:11 -06:00
lockable.AddFieldConfig("Dependencies", &graphql.Field{
Type: lockable_list,
})
2023-07-26 00:18:11 -06:00
lockable.AddFieldConfig("Owner", &graphql.Field{
Type: lockable,
})
2023-07-26 00:18:11 -06:00
AddNodeInterfaceFields(lockable)
})
var GQLInterfaceThread = NewSingleton(func() *graphql.Interface {
gql_interface_thread := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Thread",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
2023-07-26 00:18:11 -06:00
ctx, err := PrepTypeResolve(p)
if err != nil {
return nil
}
2023-07-26 00:18:11 -06:00
valid_threads := ctx.GQLContext.ValidThreads
p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_threads) {
if p_type == key {
return value
}
}
2023-07-26 00:18:11 -06:00
node, ok := p.Value.(*Node)
if ok == false {
return nil
}
_, err = GetExt[*ThreadExt](node)
if err == nil {
return ctx.GQLContext.BaseThreadType
}
return nil
},
Fields: graphql.Fields{},
})
return gql_interface_thread
}, func(thread *graphql.Interface, thread_list *graphql.List) {
2023-07-26 00:18:11 -06:00
thread.AddFieldConfig("Children", &graphql.Field{
Type: thread_list,
})
thread.AddFieldConfig("Parent", &graphql.Field{
Type: thread,
})
thread.AddFieldConfig("State", &graphql.Field{
Type: graphql.String,
})
AddNodeInterfaceFields(thread)
})