Removed types from context

graph-rework-2
noah metz 2023-07-21 18:18:26 -06:00
parent e59b4f57fd
commit 6d6effadec
3 changed files with 22 additions and 60 deletions

@ -85,13 +85,17 @@ func (ctx * Context) RegisterNodeType(def NodeDef) error {
ctx.Types[type_hash] = def ctx.Types[type_hash] = def
if def.Reflect.Implements(ctx.GQL.NodeType) { node_type := reflect.TypeOf((*Node)(nil)).Elem()
lockable_type := reflect.TypeOf((*Lockable)(nil)).Elem()
thread_type := reflect.TypeOf((*Thread)(nil)).Elem()
if def.Reflect.Implements(node_type) {
ctx.GQL.ValidNodes[def.Reflect] = def.GQLType ctx.GQL.ValidNodes[def.Reflect] = def.GQLType
} }
if def.Reflect.Implements(ctx.GQL.LockableType) { if def.Reflect.Implements(lockable_type) {
ctx.GQL.ValidLockables[def.Reflect] = def.GQLType ctx.GQL.ValidLockables[def.Reflect] = def.GQLType
} }
if def.Reflect.Implements(ctx.GQL.ThreadType) { if def.Reflect.Implements(thread_type) {
ctx.GQL.ValidThreads[def.Reflect] = def.GQLType ctx.GQL.ValidThreads[def.Reflect] = def.GQLType
} }
ctx.GQL.TypeList = append(ctx.GQL.TypeList, def.GQLType) ctx.GQL.TypeList = append(ctx.GQL.TypeList, def.GQLType)
@ -107,11 +111,6 @@ type GQLContext struct {
// Generated GQL schema // Generated GQL schema
Schema graphql.Schema Schema graphql.Schema
// Interface types to compare against
NodeType reflect.Type
LockableType reflect.Type
ThreadType reflect.Type
// List of GQL types // List of GQL types
TypeList []graphql.Type TypeList []graphql.Type
@ -150,15 +149,14 @@ func NewGQLContext() GQLContext {
Schema: graphql.Schema{}, Schema: graphql.Schema{},
TypeList: []graphql.Type{}, TypeList: []graphql.Type{},
ValidNodes: ObjTypeMap{}, ValidNodes: ObjTypeMap{},
NodeType: reflect.TypeOf((*Node)(nil)).Elem(),
ValidThreads: ObjTypeMap{}, ValidThreads: ObjTypeMap{},
ThreadType: reflect.TypeOf((*Thread)(nil)).Elem(),
ValidLockables: ObjTypeMap{}, ValidLockables: ObjTypeMap{},
LockableType: reflect.TypeOf((*Lockable)(nil)).Elem(),
Query: query, Query: query,
Mutation: mutation, Mutation: mutation,
Subscription: subscription, Subscription: subscription,
BaseNodeType: GQLTypeGraphNode.Type, BaseNodeType: GQLTypeGraphNode.Type,
BaseLockableType: GQLTypeSimpleLockable.Type,
BaseThreadType: GQLTypeSimpleThread.Type,
} }
return ctx return ctx

@ -42,7 +42,6 @@ var GQLInterfaceNode = NewSingleton(func() *graphql.Interface {
} }
valid_nodes := ctx.GQL.ValidNodes valid_nodes := ctx.GQL.ValidNodes
node_type := ctx.GQL.NodeType
p_type := reflect.TypeOf(p.Value) p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_nodes) { for key, value := range(valid_nodes) {
@ -51,7 +50,8 @@ var GQLInterfaceNode = NewSingleton(func() *graphql.Interface {
} }
} }
if p_type.Implements(node_type) { _, ok = p.Value.(Node)
if ok == true {
return ctx.GQL.BaseNodeType return ctx.GQL.BaseNodeType
} }
@ -95,7 +95,6 @@ var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface {
} }
valid_lockables := ctx.GQL.ValidLockables valid_lockables := ctx.GQL.ValidLockables
lockable_type := ctx.GQL.LockableType
p_type := reflect.TypeOf(p.Value) p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_lockables) { for key, value := range(valid_lockables) {
@ -104,8 +103,9 @@ var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface {
} }
} }
if p_type.Implements(lockable_type) { _, ok = p.Value.(Lockable)
return ctx.GQL.BaseThreadType if ok == true {
return ctx.GQL.BaseLockableType
} }
return nil return nil
}, },
@ -139,7 +139,6 @@ var GQLInterfaceThread = NewSingleton(func() *graphql.Interface {
} }
valid_threads := ctx.GQL.ValidThreads valid_threads := ctx.GQL.ValidThreads
thread_type := ctx.GQL.ThreadType
p_type := reflect.TypeOf(p.Value) p_type := reflect.TypeOf(p.Value)
for key, value := range(valid_threads) { for key, value := range(valid_threads) {
@ -148,11 +147,11 @@ var GQLInterfaceThread = NewSingleton(func() *graphql.Interface {
} }
} }
if p_type.Implements(thread_type) { _, ok = p.Value.(Thread)
if ok == true {
return ctx.GQL.BaseThreadType return ctx.GQL.BaseThreadType
} }
ctx.Log.Logf("gql", "Found no type that matches %+v: %+v", p_type, p_type.Implements(thread_type))
return nil return nil
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},

@ -2,7 +2,6 @@ package graphvent
import ( import (
"github.com/graphql-go/graphql" "github.com/graphql-go/graphql"
"reflect"
) )
func AddNodeFields(obj *graphql.Object) { func AddNodeFields(obj *graphql.Object) {
@ -113,20 +112,8 @@ var GQLTypeSimpleThread = NewSingleton(func() *graphql.Object {
GQLInterfaceLockable.Type, GQLInterfaceLockable.Type,
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
ctx, ok := p.Context.Value("graph_context").(*Context) _, ok := p.Value.(Thread)
if ok == false { return ok
return false
}
thread_type := ctx.GQL.ThreadType
value_type := reflect.TypeOf(p.Value)
if value_type.Implements(thread_type) {
return true
}
return false
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
@ -144,19 +131,8 @@ var GQLTypeSimpleLockable = NewSingleton(func() *graphql.Object {
GQLInterfaceLockable.Type, GQLInterfaceLockable.Type,
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
ctx, ok := p.Context.Value("graph_context").(*Context) _, ok := p.Value.(Lockable)
if ok == false { return ok
return false
}
lockable_type := ctx.GQL.LockableType
value_type := reflect.TypeOf(p.Value)
if value_type.Implements(lockable_type) {
return true
}
return false
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
@ -173,19 +149,8 @@ var GQLTypeGraphNode = NewSingleton(func() *graphql.Object {
GQLInterfaceNode.Type, GQLInterfaceNode.Type,
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
ctx, ok := p.Context.Value("graph_context").(*Context) _, ok := p.Value.(Node)
if ok == false { return ok
return false
}
node_type := ctx.GQL.NodeType
value_type := reflect.TypeOf(p.Value)
if value_type.Implements(node_type) {
return true
}
return false
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })