graphvent/context.go

194 lines
5.1 KiB
Go

2023-07-09 14:30:30 -06:00
package graphvent
import (
"github.com/graphql-go/graphql"
badger "github.com/dgraph-io/badger/v3"
"reflect"
"fmt"
)
// For persistance, each node needs the following functions(* is a placeholder for the node/state type):
// Load*State - StateLoadFunc that returns the NodeState interface to attach to the node
// Load* - NodeLoadFunc that returns the GraphNode restored from it's loaded state
// For convenience, the following functions are a good idea to define for composability:
// Restore*State - takes in the nodes serialized data to allow for easier nesting of inherited Load*State functions
// Save*State - serialize the node into it's json counterpart to be included as part of a larger json
type NodeLoadFunc func(*Context, NodeID, []byte, NodeMap)(Node, error)
type NodeDef struct {
Load NodeLoadFunc
Type NodeType
2023-07-10 21:15:01 -06:00
GQLType *graphql.Object
Reflect reflect.Type
2023-07-09 14:30:30 -06:00
}
2023-07-10 21:15:01 -06:00
func NewNodeDef(type_name string, reflect reflect.Type, load_func NodeLoadFunc, gql_type *graphql.Object) NodeDef {
2023-07-09 14:30:30 -06:00
return NodeDef{
Type: NodeType(type_name),
Load: load_func,
2023-07-10 21:15:01 -06:00
GQLType: gql_type,
Reflect: reflect,
2023-07-09 14:30:30 -06:00
}
}
type Context struct {
DB * badger.DB
Log Logger
Types map[uint64]NodeDef
2023-07-10 21:15:01 -06:00
GQL GQLContext
2023-07-09 14:30:30 -06:00
}
2023-07-10 21:15:01 -06:00
func (ctx * Context) RebuildSchema() error {
schemaConfig := graphql.SchemaConfig{
Types: ctx.GQL.TypeList,
Query: ctx.GQL.Query,
Mutation: ctx.GQL.Mutation,
Subscription: ctx.GQL.Subscription,
2023-07-09 14:30:30 -06:00
}
2023-07-10 21:15:01 -06:00
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
return err
}
ctx.GQL.Schema = schema
return nil
}
func (ctx * Context) AddGQLType(gql_type graphql.Type) {
ctx.GQL.TypeList = append(ctx.GQL.TypeList, gql_type)
}
func (ctx * Context) RegisterNodeType(def NodeDef) error {
if def.Load == nil {
return fmt.Errorf("Cannot register a node without a load function: %s", def.Type)
}
if def.Reflect == nil {
return fmt.Errorf("Cannot register a node without a reflect type: %s", def.Type)
}
if def.GQLType == nil {
return fmt.Errorf("Cannot register a node without a gql type: %s", def.Type)
2023-07-09 14:30:30 -06:00
}
type_hash := def.Type.Hash()
_, exists := ctx.Types[type_hash]
if exists == true {
2023-07-10 21:15:01 -06:00
return fmt.Errorf("Cannot register node of type %s, type already exists in context", def.Type)
2023-07-09 14:30:30 -06:00
}
ctx.Types[type_hash] = def
2023-07-10 21:15:01 -06:00
if def.Reflect.Implements(ctx.GQL.NodeType) {
ctx.GQL.ValidNodes[def.Reflect] = def.GQLType
}
if def.Reflect.Implements(ctx.GQL.LockableType) {
ctx.GQL.ValidLockables[def.Reflect] = def.GQLType
}
if def.Reflect.Implements(ctx.GQL.ThreadType) {
ctx.GQL.ValidThreads[def.Reflect] = def.GQLType
}
ctx.GQL.TypeList = append(ctx.GQL.TypeList, def.GQLType)
2023-07-09 14:30:30 -06:00
return nil
}
type TypeList []graphql.Type
type ObjTypeMap map[reflect.Type]*graphql.Object
type FieldMap map[string]*graphql.Field
type GQLContext struct {
Schema graphql.Schema
2023-07-10 21:15:01 -06:00
2023-07-09 14:30:30 -06:00
NodeType reflect.Type
LockableType reflect.Type
ThreadType reflect.Type
2023-07-10 21:15:01 -06:00
TypeList TypeList
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
ValidNodes ObjTypeMap
ValidLockables ObjTypeMap
ValidThreads ObjTypeMap
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
Query *graphql.Object
Mutation *graphql.Object
Subscription *graphql.Object
}
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
func NewGQLContext() GQLContext {
query := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{},
})
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
mutation := graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{},
})
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
subscription := graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: graphql.Fields{},
})
2023-07-09 14:30:30 -06:00
ctx := GQLContext{
2023-07-10 21:15:01 -06:00
Schema: graphql.Schema{},
TypeList: TypeList{},
ValidNodes: ObjTypeMap{},
NodeType: reflect.TypeOf((*Node)(nil)).Elem(),
ValidThreads: ObjTypeMap{},
ThreadType: reflect.TypeOf((*Thread)(nil)).Elem(),
ValidLockables: ObjTypeMap{},
LockableType: reflect.TypeOf((*Lockable)(nil)).Elem(),
Query: query,
Mutation: mutation,
Subscription: subscription,
2023-07-09 14:30:30 -06:00
}
2023-07-10 21:15:01 -06:00
return ctx
}
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
func NewContext(db * badger.DB, log Logger) * Context {
2023-07-09 14:30:30 -06:00
ctx := &Context{
2023-07-10 21:15:01 -06:00
GQL: NewGQLContext(),
2023-07-09 14:30:30 -06:00
DB: db,
Log: log,
Types: map[uint64]NodeDef{},
}
2023-07-10 21:15:01 -06:00
err := ctx.RegisterNodeType(NewNodeDef("graph_node", reflect.TypeOf((*GraphNode)(nil)), LoadGraphNode, GQLTypeGraphNode()))
2023-07-09 14:30:30 -06:00
if err != nil {
panic(err)
}
2023-07-10 21:15:01 -06:00
err = ctx.RegisterNodeType(NewNodeDef("simple_lockable", reflect.TypeOf((*SimpleLockable)(nil)), LoadSimpleLockable, GQLTypeSimpleLockable()))
2023-07-09 14:30:30 -06:00
if err != nil {
panic(err)
}
2023-07-10 21:15:01 -06:00
err = ctx.RegisterNodeType(NewNodeDef("simple_thread", reflect.TypeOf((*SimpleThread)(nil)), LoadSimpleThread, GQLTypeSimpleThread()))
2023-07-09 14:30:30 -06:00
if err != nil {
panic(err)
}
2023-07-10 21:15:01 -06:00
err = ctx.RegisterNodeType(NewNodeDef("gql_thread", reflect.TypeOf((*GQLThread)(nil)), LoadGQLThread, GQLTypeGQLThread()))
2023-07-09 14:30:30 -06:00
if err != nil {
panic(err)
2023-07-09 20:30:19 -06:00
}
2023-07-09 14:30:30 -06:00
2023-07-10 21:15:01 -06:00
ctx.AddGQLType(GQLTypeSignal())
ctx.GQL.Query.AddFieldConfig("Self", GQLQuerySelf())
ctx.GQL.Subscription.AddFieldConfig("Update", GQLSubscriptionUpdate())
ctx.GQL.Subscription.AddFieldConfig("Self", GQLSubscriptionSelf())
ctx.GQL.Mutation.AddFieldConfig("SendUpdate", GQLMutationSendUpdate())
err = ctx.RebuildSchema()
if err != nil {
panic(err)
2023-07-09 14:30:30 -06:00
}
return ctx
}