From 6d6effadeccea5df0e22605a1183d2f498a365ca Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 21 Jul 2023 18:18:26 -0600 Subject: [PATCH] Removed types from context --- context.go | 20 +++++++++----------- gql_interfaces.go | 15 +++++++-------- gql_types.go | 47 ++++++----------------------------------------- 3 files changed, 22 insertions(+), 60 deletions(-) diff --git a/context.go b/context.go index 8d3029b..2a130b3 100644 --- a/context.go +++ b/context.go @@ -85,13 +85,17 @@ func (ctx * Context) RegisterNodeType(def NodeDef) error { 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 } - if def.Reflect.Implements(ctx.GQL.LockableType) { + if def.Reflect.Implements(lockable_type) { 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.TypeList = append(ctx.GQL.TypeList, def.GQLType) @@ -107,11 +111,6 @@ type GQLContext struct { // Generated GQL schema Schema graphql.Schema - // Interface types to compare against - NodeType reflect.Type - LockableType reflect.Type - ThreadType reflect.Type - // List of GQL types TypeList []graphql.Type @@ -150,15 +149,14 @@ func NewGQLContext() GQLContext { Schema: graphql.Schema{}, TypeList: []graphql.Type{}, ValidNodes: ObjTypeMap{}, - NodeType: reflect.TypeOf((*Node)(nil)).Elem(), ValidThreads: ObjTypeMap{}, - ThreadType: reflect.TypeOf((*Thread)(nil)).Elem(), ValidLockables: ObjTypeMap{}, - LockableType: reflect.TypeOf((*Lockable)(nil)).Elem(), Query: query, Mutation: mutation, Subscription: subscription, BaseNodeType: GQLTypeGraphNode.Type, + BaseLockableType: GQLTypeSimpleLockable.Type, + BaseThreadType: GQLTypeSimpleThread.Type, } return ctx diff --git a/gql_interfaces.go b/gql_interfaces.go index 4c7e918..613df72 100644 --- a/gql_interfaces.go +++ b/gql_interfaces.go @@ -42,7 +42,6 @@ var GQLInterfaceNode = NewSingleton(func() *graphql.Interface { } valid_nodes := ctx.GQL.ValidNodes - node_type := ctx.GQL.NodeType p_type := reflect.TypeOf(p.Value) 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 } @@ -95,7 +95,6 @@ var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface { } valid_lockables := ctx.GQL.ValidLockables - lockable_type := ctx.GQL.LockableType p_type := reflect.TypeOf(p.Value) for key, value := range(valid_lockables) { @@ -104,8 +103,9 @@ var GQLInterfaceLockable = NewSingleton(func() *graphql.Interface { } } - if p_type.Implements(lockable_type) { - return ctx.GQL.BaseThreadType + _, ok = p.Value.(Lockable) + if ok == true { + return ctx.GQL.BaseLockableType } return nil }, @@ -139,7 +139,6 @@ var GQLInterfaceThread = NewSingleton(func() *graphql.Interface { } valid_threads := ctx.GQL.ValidThreads - thread_type := ctx.GQL.ThreadType p_type := reflect.TypeOf(p.Value) 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 } - ctx.Log.Logf("gql", "Found no type that matches %+v: %+v", p_type, p_type.Implements(thread_type)) return nil }, Fields: graphql.Fields{}, diff --git a/gql_types.go b/gql_types.go index b16054a..f8bb852 100644 --- a/gql_types.go +++ b/gql_types.go @@ -2,7 +2,6 @@ package graphvent import ( "github.com/graphql-go/graphql" - "reflect" ) func AddNodeFields(obj *graphql.Object) { @@ -113,20 +112,8 @@ var GQLTypeSimpleThread = NewSingleton(func() *graphql.Object { GQLInterfaceLockable.Type, }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return false - } - - thread_type := ctx.GQL.ThreadType - - value_type := reflect.TypeOf(p.Value) - - if value_type.Implements(thread_type) { - return true - } - - return false + _, ok := p.Value.(Thread) + return ok }, Fields: graphql.Fields{}, }) @@ -144,19 +131,8 @@ var GQLTypeSimpleLockable = NewSingleton(func() *graphql.Object { GQLInterfaceLockable.Type, }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return false - } - - lockable_type := ctx.GQL.LockableType - value_type := reflect.TypeOf(p.Value) - - if value_type.Implements(lockable_type) { - return true - } - - return false + _, ok := p.Value.(Lockable) + return ok }, Fields: graphql.Fields{}, }) @@ -173,19 +149,8 @@ var GQLTypeGraphNode = NewSingleton(func() *graphql.Object { GQLInterfaceNode.Type, }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { - ctx, ok := p.Context.Value("graph_context").(*Context) - if ok == false { - return false - } - - node_type := ctx.GQL.NodeType - value_type := reflect.TypeOf(p.Value) - - if value_type.Implements(node_type) { - return true - } - - return false + _, ok := p.Value.(Node) + return ok }, Fields: graphql.Fields{}, })