diff --git a/context.go b/context.go index 41481c2..8a1cd96 100644 --- a/context.go +++ b/context.go @@ -123,30 +123,49 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) { return nil, err } - err = ctx.RegisterExtension(ThreadExtType, LoadThreadExt, NewThreadExtContext()) + err = ctx.RegisterExtension(ECDHExtType, LoadECDHExt, nil) if err != nil { return nil, err } - err = ctx.RegisterExtension(ECDHExtType, LoadECDHExt, nil) + err = ctx.RegisterExtension(GroupExtType, LoadGroupExt, nil) if err != nil { return nil, err } - err = ctx.RegisterExtension(GroupExtType, LoadGroupExt, nil) + gql_ctx := NewGQLExtContext() + err = ctx.RegisterExtension(GQLExtType, LoadGQLExt, gql_ctx) if err != nil { return nil, err } - err = ctx.RegisterExtension(GQLExtType, LoadGQLExt, NewGQLExtContext()) + thread_ctx := NewThreadExtContext() + err = ctx.RegisterExtension(ThreadExtType, LoadThreadExt, thread_ctx) if err != nil { return nil, err } - err = RegisterGQLThread(ctx) + err = thread_ctx.RegisterThreadType(GQLThreadType, gql_actions, gql_handlers) if err != nil { 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 } diff --git a/gql.go b/gql.go index 30dee29..1b53fda 100644 --- a/gql.go +++ b/gql.go @@ -31,19 +31,7 @@ import ( ) const GQLThreadType = ThreadType("GQL") -func RegisterGQLThread(ctx *Context) error { - 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 -} +const GQLNodeType = NodeType("GQL") type AuthReqJSON struct { Time time.Time `json:"time"` @@ -652,6 +640,31 @@ type GQLInterface struct { 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 { var gql GQLInterface gql.Extensions = extensions diff --git a/gql_interfaces.go b/gql_interfaces.go index 54d28c3..fe84d85 100644 --- a/gql_interfaces.go +++ b/gql_interfaces.go @@ -35,23 +35,11 @@ func AddNodeInterfaceFields(gql *GQLInterface) { }) } -func AddNodeFields(gql *GQLInterface) { - gql.Default.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) - - gql.Default.AddFieldConfig("TypeHash", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeTypeHash, - }) +func AddLockableInterfaceFields(gql *GQLInterface) { + addLockableInterfaceFields(gql, GQLInterfaceLockable) } -func LockableInterfaceFields(gql *GQLInterface) { - AddLockableInterfaceFields(gql, gql) -} - -func AddLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) { +func addLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) { AddNodeInterfaceFields(gql) gql.Interface.AddFieldConfig("Requirements", &graphql.Field{ @@ -67,35 +55,12 @@ func AddLockableInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface) { }) } -func LockableFields(gql *GQLInterface) { - AddLockableFields(gql, gql) -} - -func AddLockableFields(gql *GQLInterface, gql_lockable *GQLInterface) { - AddNodeFields(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) { + addThreadInterfaceFields(gql, GQLInterfaceThread) } -func AddThreadInterfaceFields(gql *GQLInterface, gql_lockable *GQLInterface, gql_thread *GQLInterface) { - AddLockableInterfaceFields(gql, gql_lockable) +func addThreadInterfaceFields(gql *GQLInterface, gql_thread *GQLInterface) { + AddLockableInterfaceFields(gql) gql.Interface.AddFieldConfig("Children", &graphql.Field{ 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 { if node == nil { 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 GQLInterfaceLockable = NewGQLInterface("Lockable", "DefaultLockable", []*graphql.Interface{GQLInterfaceNode.Interface}, []ExtType{LockableExtType}, LockableInterfaceFields, LockableFields) - -var GQLInterfaceThread = NewGQLInterface("Thread", "DefaultThread", []*graphql.Interface{GQLInterfaceNode.Interface, }, []ExtType{ThreadExtType, LockableExtType}, ThreadInterfaceFields, ThreadFields) +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}, 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}, func(gql *GQLInterface){ + addThreadInterfaceFields(gql, gql) +}, func(gql *GQLInterface) { + addThreadFields(gql.Default, gql.Interface, gql.List) +}) diff --git a/gql_types.go b/gql_types.go index b1caeba..818d683 100644 --- a/gql_types.go +++ b/gql_types.go @@ -4,6 +4,71 @@ import ( "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 { gql_type_signal := graphql.NewObject(graphql.ObjectConfig{ Name: "Signal",