From f09a1db2d4730b331942ac444ca2d61b12758166 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 21 Jul 2023 18:06:53 -0600 Subject: [PATCH] Moved common graphql fields to functions instead of redefining every definition --- gql_interfaces.go | 164 ++++++++++++++++++++++++ gql_types.go | 318 ++++++---------------------------------------- 2 files changed, 201 insertions(+), 281 deletions(-) create mode 100644 gql_interfaces.go diff --git a/gql_interfaces.go b/gql_interfaces.go new file mode 100644 index 0000000..4c7e918 --- /dev/null +++ b/gql_interfaces.go @@ -0,0 +1,164 @@ +package graphvent + +import ( + "github.com/graphql-go/graphql" + "reflect" +) + +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, + } +} + +func addNodeInterfaceFields(i *graphql.Interface) { + i.AddFieldConfig("ID", &graphql.Field{ + Type: graphql.String, + }) +} + +var GQLInterfaceNode = NewSingleton(func() *graphql.Interface { + i := graphql.NewInterface(graphql.InterfaceConfig{ + Name: "Node", + ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { + ctx, ok := p.Context.Value("graph_context").(*Context) + if ok == false { + return nil + } + + valid_nodes := ctx.GQL.ValidNodes + node_type := ctx.GQL.NodeType + p_type := reflect.TypeOf(p.Value) + + for key, value := range(valid_nodes) { + if p_type == key { + return value + } + } + + if p_type.Implements(node_type) { + return ctx.GQL.BaseNodeType + } + + return nil + }, + Fields: graphql.Fields{}, + }) + + addNodeInterfaceFields(i) + + return i +}, nil) + +func addLockableInterfaceFields(i *graphql.Interface, lockable *graphql.Interface, list *graphql.List) { + addNodeInterfaceFields(i) + + i.AddFieldConfig("Name", &graphql.Field{ + Type: graphql.String, + }) + + i.AddFieldConfig("Requirements", &graphql.Field{ + Type: list, + }) + + i.AddFieldConfig("Dependencies", &graphql.Field{ + Type: list, + }) + + i.AddFieldConfig("Owner", &graphql.Field{ + Type: lockable, + }) +} + +var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface { + gql_interface_lockable := graphql.NewInterface(graphql.InterfaceConfig{ + Name: "Lockable", + ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { + ctx, ok := p.Context.Value("graph_context").(*Context) + if ok == false { + return nil + } + + valid_lockables := ctx.GQL.ValidLockables + lockable_type := ctx.GQL.LockableType + p_type := reflect.TypeOf(p.Value) + + for key, value := range(valid_lockables) { + if p_type == key { + return value + } + } + + if p_type.Implements(lockable_type) { + return ctx.GQL.BaseThreadType + } + return nil + }, + Fields: graphql.Fields{}, + }) + + return gql_interface_lockable +}, func(lockable *graphql.Interface, lockable_list *graphql.List) { + addLockableInterfaceFields(lockable, lockable, lockable_list) +}) + +func addThreadInterfaceFields(i *graphql.Interface, thread *graphql.Interface, list *graphql.List) { + addLockableInterfaceFields(i, GQLInterfaceLockable.Type, GQLInterfaceLockable.List) + + i.AddFieldConfig("Children", &graphql.Field{ + Type: list, + }) + + i.AddFieldConfig("Parent", &graphql.Field{ + Type: thread, + }) +} + +var GQLInterfaceThread = NewSingleton(func() *graphql.Interface { + gql_interface_thread := graphql.NewInterface(graphql.InterfaceConfig{ + Name: "Thread", + ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { + ctx, ok := p.Context.Value("graph_context").(*Context) + if ok == false { + return nil + } + + valid_threads := ctx.GQL.ValidThreads + thread_type := ctx.GQL.ThreadType + p_type := reflect.TypeOf(p.Value) + + for key, value := range(valid_threads) { + if p_type == key { + return value + } + } + + if p_type.Implements(thread_type) { + return ctx.GQL.BaseThreadType + } + + ctx.Log.Logf("gql", "Found no type that matches %+v: %+v", p_type, p_type.Implements(thread_type)) + return nil + }, + Fields: graphql.Fields{}, + }) + + return gql_interface_thread +}, func(thread *graphql.Interface, thread_list *graphql.List) { + addThreadInterfaceFields(thread, thread, thread_list) +}) diff --git a/gql_types.go b/gql_types.go index d27a9d3..7f78172 100644 --- a/gql_types.go +++ b/gql_types.go @@ -5,173 +5,61 @@ import ( "reflect" ) -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, - } -} - -var GQLInterfaceNode = NewSingleton(func() *graphql.Interface { - i := graphql.NewInterface(graphql.InterfaceConfig{ - Name: "Node", - ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return nil - } - - valid_nodes := ctx.GQL.ValidNodes - node_type := ctx.GQL.NodeType - p_type := reflect.TypeOf(p.Value) - - for key, value := range(valid_nodes) { - if p_type == key { - return value - } - } - - if p_type.Implements(node_type) { - return ctx.GQL.BaseNodeType - } - - return nil - }, - Fields: graphql.Fields{}, - }) - i.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - }) - return i -}, nil) - -var GQLInterfaceThread = NewSingleton(func() *graphql.Interface { - gql_interface_thread := graphql.NewInterface(graphql.InterfaceConfig{ - Name: "Thread", - ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return nil - } - - valid_threads := ctx.GQL.ValidThreads - thread_type := ctx.GQL.ThreadType - p_type := reflect.TypeOf(p.Value) - - for key, value := range(valid_threads) { - if p_type == key { - return value - } - } - - if p_type.Implements(thread_type) { - return ctx.GQL.BaseThreadType - } - - ctx.Log.Logf("gql", "Found no type that matches %+v: %+v", p_type, p_type.Implements(thread_type)) - return nil - }, - Fields: graphql.Fields{}, - }) - - gql_interface_thread.AddFieldConfig("ID", &graphql.Field{ +func AddNodeFields(obj *graphql.Object) { + obj.AddFieldConfig("ID", &graphql.Field{ Type: graphql.String, + Resolve: GQLNodeID, }) +} - gql_interface_thread.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - }) +func AddLockableFields(obj *graphql.Object) { + AddNodeFields(obj) - gql_interface_thread.AddFieldConfig("State", &graphql.Field{ + obj.AddFieldConfig("Name", &graphql.Field{ Type: graphql.String, + Resolve: GQLLockableName, }) - gql_interface_thread.AddFieldConfig("Requirements", &graphql.Field{ - Type: GQLInterfaceLockable.List, - }) - - gql_interface_thread.AddFieldConfig("Dependencies", &graphql.Field{ + obj.AddFieldConfig("Requirements", &graphql.Field{ Type: GQLInterfaceLockable.List, + Resolve: GQLLockableRequirements, }) - gql_interface_thread.AddFieldConfig("Owner", &graphql.Field{ + obj.AddFieldConfig("Owner", &graphql.Field{ Type: GQLInterfaceLockable.Type, + Resolve: GQLLockableOwner, }) - return gql_interface_thread -}, func(thread *graphql.Interface, thread_list *graphql.List) { - thread.AddFieldConfig("Children", &graphql.Field{ - Type: thread_list, - }) - - thread.AddFieldConfig("Parent", &graphql.Field{ - Type: thread, + obj.AddFieldConfig("Dependencies", &graphql.Field{ + Type: GQLInterfaceLockable.List, + Resolve: GQLLockableDependencies, }) +} -}) - -var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface { - gql_interface_lockable := graphql.NewInterface(graphql.InterfaceConfig{ - Name: "Lockable", - ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return nil - } - - valid_lockables := ctx.GQL.ValidLockables - lockable_type := ctx.GQL.LockableType - p_type := reflect.TypeOf(p.Value) - - for key, value := range(valid_lockables) { - if p_type == key { - return value - } - } - - if p_type.Implements(lockable_type) { - return ctx.GQL.BaseThreadType - } - return nil - }, - Fields: graphql.Fields{}, - }) +func AddThreadFields(obj *graphql.Object) { + AddLockableFields(obj) - gql_interface_lockable.AddFieldConfig("ID", &graphql.Field{ + obj.AddFieldConfig("State", &graphql.Field{ Type: graphql.String, + Resolve: GQLThreadState, }) - gql_interface_lockable.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - }) - return gql_interface_lockable -}, func(lockable *graphql.Interface, lockable_list *graphql.List) { - lockable.AddFieldConfig("Requirements", &graphql.Field{ - Type: lockable_list, + obj.AddFieldConfig("Children", &graphql.Field{ + Type: GQLInterfaceThread.List, + Resolve: GQLThreadChildren, }) - lockable.AddFieldConfig("Dependencies", &graphql.Field{ - Type: lockable_list, + obj.AddFieldConfig("Parent", &graphql.Field{ + Type: GQLInterfaceThread.Type, + Resolve: GQLThreadParent, }) - lockable.AddFieldConfig("Owner", &graphql.Field{ - Type: lockable, + obj.AddFieldConfig("Listen", &graphql.Field{ + Type: graphql.String, + Resolve: GQLThreadListen, }) -}) + +} var GQLTypeUser = NewSingleton(func() *graphql.Object { gql_type_user := graphql.NewObject(graphql.ObjectConfig{ @@ -198,30 +86,8 @@ var GQLTypeUser = NewSingleton(func() *graphql.Object { Fields: graphql.Fields{}, }) - gql_type_user.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) + AddLockableFields(gql_type_user) - gql_type_user.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - Resolve: GQLLockableName, - }) - - gql_type_user.AddFieldConfig("Requirements", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableRequirements, - }) - - gql_type_user.AddFieldConfig("Owner", &graphql.Field{ - Type: GQLInterfaceLockable.Type, - Resolve: GQLLockableOwner, - }) - - gql_type_user.AddFieldConfig("Dependencies", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableDependencies, - }) return gql_type_user }, nil) @@ -240,55 +106,13 @@ var GQLTypeGQLThread = NewSingleton(func() *graphql.Object { Fields: graphql.Fields{}, }) - gql_type_gql_thread.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) - - gql_type_gql_thread.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - Resolve: GQLLockableName, - }) - - gql_type_gql_thread.AddFieldConfig("State", &graphql.Field{ - Type: graphql.String, - Resolve: GQLThreadState, - }) - - gql_type_gql_thread.AddFieldConfig("Children", &graphql.Field{ - Type: GQLInterfaceThread.List, - Resolve: GQLThreadChildren, - }) - - gql_type_gql_thread.AddFieldConfig("Parent", &graphql.Field{ - Type: GQLInterfaceThread.Type, - Resolve: GQLThreadParent, - }) - - gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{ - Type: graphql.String, - Resolve: GQLThreadListen, - }) - - gql_type_gql_thread.AddFieldConfig("Requirements", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableRequirements, - }) - - gql_type_gql_thread.AddFieldConfig("Owner", &graphql.Field{ - Type: GQLInterfaceLockable.Type, - Resolve: GQLLockableOwner, - }) - - gql_type_gql_thread.AddFieldConfig("Dependencies", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableDependencies, - }) + AddThreadFields(gql_type_gql_thread) gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ Type: GQLTypeUser.List, Resolve: GQLThreadUsers, }) + return gql_type_gql_thread }, nil) @@ -318,45 +142,8 @@ var GQLTypeSimpleThread = NewSingleton(func() *graphql.Object { }, Fields: graphql.Fields{}, }) - gql_type_simple_thread.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) - gql_type_simple_thread.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - Resolve: GQLLockableName, - }) - - gql_type_simple_thread.AddFieldConfig("State", &graphql.Field{ - Type: graphql.String, - Resolve: GQLThreadState, - }) - - gql_type_simple_thread.AddFieldConfig("Children", &graphql.Field{ - Type: GQLInterfaceThread.List, - Resolve: GQLThreadChildren, - }) - - gql_type_simple_thread.AddFieldConfig("Parent", &graphql.Field{ - Type: GQLInterfaceThread.Type, - Resolve: GQLThreadParent, - }) - - gql_type_simple_thread.AddFieldConfig("Requirements", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableRequirements, - }) - - gql_type_simple_thread.AddFieldConfig("Owner", &graphql.Field{ - Type: GQLInterfaceLockable.Type, - Resolve: GQLLockableOwner, - }) - - gql_type_simple_thread.AddFieldConfig("Dependencies", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableDependencies, - }) + AddThreadFields(gql_type_simple_thread) return gql_type_simple_thread }, nil) @@ -386,30 +173,7 @@ var GQLTypeSimpleLockable = NewSingleton(func() *graphql.Object { Fields: graphql.Fields{}, }) - gql_type_simple_lockable.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) - - gql_type_simple_lockable.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - Resolve: GQLLockableName, - }) - - gql_type_simple_lockable.AddFieldConfig("Requirements", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableRequirements, - }) - - gql_type_simple_lockable.AddFieldConfig("Owner", &graphql.Field{ - Type: GQLInterfaceLockable.Type, - Resolve: GQLLockableOwner, - }) - - gql_type_simple_lockable.AddFieldConfig("Dependencies", &graphql.Field{ - Type: GQLInterfaceLockable.List, - Resolve: GQLLockableDependencies, - }) + AddLockableFields(gql_type_simple_lockable) return gql_type_simple_lockable }, nil) @@ -438,15 +202,7 @@ var GQLTypeGraphNode = NewSingleton(func() *graphql.Object { Fields: graphql.Fields{}, }) - object.AddFieldConfig("ID", &graphql.Field{ - Type: graphql.String, - Resolve: GQLNodeID, - }) - - object.AddFieldConfig("Name", &graphql.Field{ - Type: graphql.String, - Resolve: GQLLockableName, - }) + AddNodeFields(object) return object }, nil)