Simplified gql node/interface creation with helper functions

gql_cataclysm
noah metz 2023-07-26 23:57:50 -06:00
parent 34082630b2
commit 494d212051
4 changed files with 139 additions and 88 deletions

@ -123,30 +123,49 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return nil, err return nil, err
} }
err = ctx.RegisterExtension(ThreadExtType, LoadThreadExt, NewThreadExtContext()) err = ctx.RegisterExtension(ECDHExtType, LoadECDHExt, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ctx.RegisterExtension(ECDHExtType, LoadECDHExt, nil) err = ctx.RegisterExtension(GroupExtType, LoadGroupExt, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ctx.RegisterExtension(GroupExtType, LoadGroupExt, nil) gql_ctx := NewGQLExtContext()
err = ctx.RegisterExtension(GQLExtType, LoadGQLExt, gql_ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ctx.RegisterExtension(GQLExtType, LoadGQLExt, NewGQLExtContext()) thread_ctx := NewThreadExtContext()
err = ctx.RegisterExtension(ThreadExtType, LoadThreadExt, thread_ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = RegisterGQLThread(ctx) err = thread_ctx.RegisterThreadType(GQLThreadType, gql_actions, gql_handlers)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ctx.RegisterNodeType(GQLNodeType, []ExtType{ACLExtType, ACLPolicyExtType, GroupExtType, GQLExtType, ThreadExtType, LockableExtType})
if err != nil {
return nil, err
}
err = gql_ctx.RegisterNodeType(GQLNodeType, GQLTypeGQLNode.Type)
if err != nil {
return nil, err
}
schema, err := BuildSchema(gql_ctx)
if err != nil {
return nil, err
}
gql_ctx.Schema = schema
return ctx, nil return ctx, nil
} }

@ -31,19 +31,7 @@ import (
) )
const GQLThreadType = ThreadType("GQL") const GQLThreadType = ThreadType("GQL")
func RegisterGQLThread(ctx *Context) error { const GQLNodeType = NodeType("GQL")
thread_ctx, err := GetCtx[*ThreadExt, *ThreadExtContext](ctx)
if err != nil {
return err
}
err = thread_ctx.RegisterThreadType(GQLThreadType, gql_actions, gql_handlers)
if err != nil {
return err
}
return nil
}
type AuthReqJSON struct { type AuthReqJSON struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
@ -652,6 +640,31 @@ type GQLInterface struct {
Extensions []ExtType Extensions []ExtType
} }
type GQLType struct {
Type *graphql.Object
List *graphql.List
}
func NewGQLNodeType(node_type NodeType, interfaces []*graphql.Interface, init func(*GQLType)) *GQLType {
var gql GQLType
gql.Type = graphql.NewObject(graphql.ObjectConfig{
Name: string(node_type),
Interfaces: interfaces,
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
node, ok := p.Value.(*Node)
if ok == false {
return false
}
return node.Type == node_type
},
Fields: graphql.Fields{},
})
gql.List = graphql.NewList(gql.Type)
init(&gql)
return &gql
}
func NewGQLInterface(if_name string, default_name string, interfaces []*graphql.Interface, extensions []ExtType, init_1 func(*GQLInterface), init_2 func(*GQLInterface)) *GQLInterface { func NewGQLInterface(if_name string, default_name string, interfaces []*graphql.Interface, extensions []ExtType, init_1 func(*GQLInterface), init_2 func(*GQLInterface)) *GQLInterface {
var gql GQLInterface var gql GQLInterface
gql.Extensions = extensions gql.Extensions = extensions

@ -35,23 +35,11 @@ func AddNodeInterfaceFields(gql *GQLInterface) {
}) })
} }
func AddNodeFields(gql *GQLInterface) { func AddLockableInterfaceFields(gql *GQLInterface) {
gql.Default.AddFieldConfig("ID", &graphql.Field{ addLockableInterfaceFields(gql, GQLInterfaceLockable)
Type: graphql.String,
Resolve: GQLNodeID,
})
gql.Default.AddFieldConfig("TypeHash", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeTypeHash,
})
} }
func LockableInterfaceFields(gql *GQLInterface) { func addLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) {
AddLockableInterfaceFields(gql, gql)
}
func AddLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) {
AddNodeInterfaceFields(gql) AddNodeInterfaceFields(gql)
gql.Interface.AddFieldConfig("Requirements", &graphql.Field{ gql.Interface.AddFieldConfig("Requirements", &graphql.Field{
@ -67,35 +55,12 @@ func AddLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) {
}) })
} }
func LockableFields(gql *GQLInterface) { func AddThreadInterfaceFields(gql *GQLInterface) {
AddLockableFields(gql, gql) addThreadInterfaceFields(gql, GQLInterfaceThread)
} }
func AddLockableFields(gql *GQLInterface, gql_lockable *GQLInterface) { func addThreadInterfaceFields(gql *GQLInterface, gql_thread *GQLInterface) {
AddNodeFields(gql) AddLockableInterfaceFields(gql)
gql.Default.AddFieldConfig("Requirements", &graphql.Field{
Type: gql_lockable.List,
Resolve: GQLLockableRequirements,
})
gql.Default.AddFieldConfig("Owner", &graphql.Field{
Type: gql_lockable.Interface,
Resolve: GQLLockableOwner,
})
gql.Default.AddFieldConfig("Dependencies", &graphql.Field{
Type: gql_lockable.List,
Resolve: GQLLockableDependencies,
})
}
func ThreadInterfaceFields(gql *GQLInterface) {
AddThreadInterfaceFields(gql, GQLInterfaceLockable, gql)
}
func AddThreadInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface, gql_thread *GQLInterface) {
AddLockableInterfaceFields(gql, gql_lockable)
gql.Interface.AddFieldConfig("Children", &graphql.Field{ gql.Interface.AddFieldConfig("Children", &graphql.Field{
Type: gql_thread.List, Type: gql_thread.List,
@ -106,29 +71,6 @@ func AddThreadInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface, gql
}) })
} }
func ThreadFields(gql *GQLInterface) {
AddThreadFields(gql, GQLInterfaceLockable, gql)
}
func AddThreadFields(gql *GQLInterface, gql_lockable *GQLInterface, gql_thread *GQLInterface) {
AddLockableFields(gql, gql_lockable)
gql.Default.AddFieldConfig("State", &graphql.Field{
Type: graphql.String,
Resolve: GQLThreadState,
})
gql.Default.AddFieldConfig("Children", &graphql.Field{
Type: gql_thread.List,
Resolve: GQLThreadChildren,
})
gql.Default.AddFieldConfig("Parent", &graphql.Field{
Type: gql_thread.Interface,
Resolve: GQLThreadParent,
})
}
func NodeHasExtensions(node *Node, extensions []ExtType) bool { func NodeHasExtensions(node *Node, extensions []ExtType) bool {
if node == nil { if node == nil {
return false return false
@ -182,8 +124,20 @@ func NodeResolver(required_extensions []ExtType, default_type **graphql.Object)f
} }
} }
var GQLInterfaceNode = NewGQLInterface("Node", "DefaultNode", []*graphql.Interface{}, []ExtType{}, AddNodeInterfaceFields, AddNodeFields) var GQLInterfaceNode = NewGQLInterface("Node", "DefaultNode", []*graphql.Interface{}, []ExtType{}, func(gql *GQLInterface) {
AddNodeInterfaceFields(gql)
}, func(gql *GQLInterface) {
AddNodeFields(gql.Default)
})
var GQLInterfaceLockable = NewGQLInterface("Lockable", "DefaultLockable", []*graphql.Interface{GQLInterfaceNode.Interface}, []ExtType{LockableExtType}, LockableInterfaceFields, LockableFields) var GQLInterfaceLockable = NewGQLInterface("Lockable", "DefaultLockable", []*graphql.Interface{GQLInterfaceNode.Interface}, []ExtType{LockableExtType}, func(gql *GQLInterface) {
addLockableInterfaceFields(gql, gql)
}, func(gql *GQLInterface) {
addLockableFields(gql.Default, gql.Interface, gql.List)
})
var GQLInterfaceThread = NewGQLInterface("Thread", "DefaultThread", []*graphql.Interface{GQLInterfaceNode.Interface, }, []ExtType{ThreadExtType, LockableExtType}, ThreadInterfaceFields, ThreadFields) var GQLInterfaceThread = NewGQLInterface("Thread", "DefaultThread", []*graphql.Interface{GQLInterfaceNode.Interface, }, []ExtType{ThreadExtType, LockableExtType}, func(gql *GQLInterface){
addThreadInterfaceFields(gql, gql)
}, func(gql *GQLInterface) {
addThreadFields(gql.Default, gql.Interface, gql.List)
})

@ -4,6 +4,71 @@ import (
"github.com/graphql-go/graphql" "github.com/graphql-go/graphql"
) )
func AddNodeFields(object *graphql.Object) {
object.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeID,
})
object.AddFieldConfig("TypeHash", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeTypeHash,
})
}
func AddLockableFields(object *graphql.Object) {
addLockableFields(object, GQLInterfaceLockable.Interface, GQLInterfaceLockable.List)
}
func addLockableFields(object *graphql.Object, lockable_interface *graphql.Interface, lockable_list *graphql.List) {
AddNodeFields(object)
object.AddFieldConfig("Requirements", &graphql.Field{
Type: lockable_list,
Resolve: GQLLockableRequirements,
})
object.AddFieldConfig("Owner", &graphql.Field{
Type: lockable_interface,
Resolve: GQLLockableOwner,
})
object.AddFieldConfig("Dependencies", &graphql.Field{
Type: lockable_list,
Resolve: GQLLockableDependencies,
})
}
func AddThreadFields(object *graphql.Object) {
addThreadFields(object, GQLInterfaceThread.Interface, GQLInterfaceThread.List)
}
func addThreadFields(object *graphql.Object, thread_interface *graphql.Interface, thread_list *graphql.List) {
AddLockableFields(object)
object.AddFieldConfig("State", &graphql.Field{
Type: graphql.String,
Resolve: GQLThreadState,
})
object.AddFieldConfig("Children", &graphql.Field{
Type: thread_list,
Resolve: GQLThreadChildren,
})
object.AddFieldConfig("Parent", &graphql.Field{
Type: thread_interface,
Resolve: GQLThreadParent,
})
}
var GQLNodeInterfaces = []*graphql.Interface{GQLInterfaceNode.Interface}
var GQLLockableInterfaces = append(GQLNodeInterfaces, GQLInterfaceLockable.Interface)
var GQLThreadInterfaces = append(GQLLockableInterfaces, GQLInterfaceThread.Interface)
var GQLTypeGQLNode = NewGQLNodeType(GQLNodeType, GQLThreadInterfaces, func(gql *GQLType) {
AddThreadFields(gql.Type)
})
var GQLTypeSignal = NewSingleton(func() *graphql.Object { var GQLTypeSignal = NewSingleton(func() *graphql.Object {
gql_type_signal := graphql.NewObject(graphql.ObjectConfig{ gql_type_signal := graphql.NewObject(graphql.ObjectConfig{
Name: "Signal", Name: "Signal",