Moved common graphql fields to functions instead of redefining every definition

graph-rework-2
noah metz 2023-07-21 18:06:53 -06:00
parent 3b35885c90
commit f09a1db2d4
2 changed files with 201 additions and 281 deletions

@ -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)
})

@ -5,174 +5,62 @@ import (
"reflect" "reflect"
) )
func NewField(init func()*graphql.Field) *graphql.Field { func AddNodeFields(obj *graphql.Object) {
return init() obj.AddFieldConfig("ID", &graphql.Field{
}
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, Type: graphql.String,
Resolve: GQLNodeID,
}) })
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)) func AddLockableFields(obj *graphql.Object) {
return nil AddNodeFields(obj)
},
Fields: graphql.Fields{},
})
gql_interface_thread.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
})
gql_interface_thread.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
})
gql_interface_thread.AddFieldConfig("State", &graphql.Field{ obj.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLLockableName,
}) })
gql_interface_thread.AddFieldConfig("Requirements", &graphql.Field{ obj.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLInterfaceLockable.List,
})
gql_interface_thread.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLInterfaceLockable.List, Type: GQLInterfaceLockable.List,
Resolve: GQLLockableRequirements,
}) })
gql_interface_thread.AddFieldConfig("Owner", &graphql.Field{ obj.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable.Type, Type: GQLInterfaceLockable.Type,
Resolve: GQLLockableOwner,
}) })
return gql_interface_thread obj.AddFieldConfig("Dependencies", &graphql.Field{
}, func(thread *graphql.Interface, thread_list *graphql.List) { Type: GQLInterfaceLockable.List,
thread.AddFieldConfig("Children", &graphql.Field{ Resolve: GQLLockableDependencies,
Type: thread_list,
})
thread.AddFieldConfig("Parent", &graphql.Field{
Type: thread,
})
}) })
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) { func AddThreadFields(obj *graphql.Object) {
return ctx.GQL.BaseThreadType AddLockableFields(obj)
}
return nil
},
Fields: graphql.Fields{},
})
gql_interface_lockable.AddFieldConfig("ID", &graphql.Field{ obj.AddFieldConfig("State", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLThreadState,
}) })
gql_interface_lockable.AddFieldConfig("Name", &graphql.Field{ obj.AddFieldConfig("Children", &graphql.Field{
Type: graphql.String, Type: GQLInterfaceThread.List,
}) Resolve: GQLThreadChildren,
return gql_interface_lockable
}, func(lockable *graphql.Interface, lockable_list *graphql.List) {
lockable.AddFieldConfig("Requirements", &graphql.Field{
Type: lockable_list,
}) })
lockable.AddFieldConfig("Dependencies", &graphql.Field{ obj.AddFieldConfig("Parent", &graphql.Field{
Type: lockable_list, Type: GQLInterfaceThread.Type,
Resolve: GQLThreadParent,
}) })
lockable.AddFieldConfig("Owner", &graphql.Field{ obj.AddFieldConfig("Listen", &graphql.Field{
Type: lockable, Type: graphql.String,
}) Resolve: GQLThreadListen,
}) })
}
var GQLTypeUser = NewSingleton(func() *graphql.Object { var GQLTypeUser = NewSingleton(func() *graphql.Object {
gql_type_user := graphql.NewObject(graphql.ObjectConfig{ gql_type_user := graphql.NewObject(graphql.ObjectConfig{
Name: "User", Name: "User",
@ -198,30 +86,8 @@ var GQLTypeUser = NewSingleton(func() *graphql.Object {
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
gql_type_user.AddFieldConfig("ID", &graphql.Field{ AddLockableFields(gql_type_user)
Type: graphql.String,
Resolve: GQLNodeID,
})
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 return gql_type_user
}, nil) }, nil)
@ -240,55 +106,13 @@ var GQLTypeGQLThread = NewSingleton(func() *graphql.Object {
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
gql_type_gql_thread.AddFieldConfig("ID", &graphql.Field{ AddThreadFields(gql_type_gql_thread)
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,
})
gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{
Type: GQLTypeUser.List, Type: GQLTypeUser.List,
Resolve: GQLThreadUsers, Resolve: GQLThreadUsers,
}) })
return gql_type_gql_thread return gql_type_gql_thread
}, nil) }, nil)
@ -318,45 +142,8 @@ var GQLTypeSimpleThread = NewSingleton(func() *graphql.Object {
}, },
Fields: graphql.Fields{}, 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{ AddThreadFields(gql_type_simple_thread)
Type: GQLInterfaceLockable.List,
Resolve: GQLLockableDependencies,
})
return gql_type_simple_thread return gql_type_simple_thread
}, nil) }, nil)
@ -386,30 +173,7 @@ var GQLTypeSimpleLockable = NewSingleton(func() *graphql.Object {
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
gql_type_simple_lockable.AddFieldConfig("ID", &graphql.Field{ AddLockableFields(gql_type_simple_lockable)
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,
})
return gql_type_simple_lockable return gql_type_simple_lockable
}, nil) }, nil)
@ -438,15 +202,7 @@ var GQLTypeGraphNode = NewSingleton(func() *graphql.Object {
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
object.AddFieldConfig("ID", &graphql.Field{ AddNodeFields(object)
Type: graphql.String,
Resolve: GQLNodeID,
})
object.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLLockableName,
})
return object return object
}, nil) }, nil)