Reorganized GQL files, and moved from modifying the GQL library to using a NodeCache in ResolveContext
parent
21224e8837
commit
9ffa9d6cb2
@ -1,98 +0,0 @@
|
||||
package graphvent
|
||||
|
||||
import (
|
||||
"github.com/graphql-go/graphql"
|
||||
)
|
||||
|
||||
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 NodeInterfaceDefaultIsType(required_extensions []ExtType) func(graphql.IsTypeOfParams) bool {
|
||||
return func(p graphql.IsTypeOfParams) bool {
|
||||
ctx, ok := p.Context.Value("resolve").(*ResolveContext)
|
||||
if ok == false {
|
||||
return false
|
||||
}
|
||||
node, ok := p.Value.(NodeResult)
|
||||
if ok == false {
|
||||
return false
|
||||
}
|
||||
|
||||
node_type_def, exists := ctx.Context.Nodes[node.Result.NodeType]
|
||||
if exists == false {
|
||||
return false
|
||||
} else {
|
||||
for _, ext := range(required_extensions) {
|
||||
found := false
|
||||
for _, e := range(node_type_def.Extensions) {
|
||||
if e == ext {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if found == false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func NodeInterfaceResolveType(required_extensions []ExtType, default_type **graphql.Object)func(graphql.ResolveTypeParams) *graphql.Object {
|
||||
return func(p graphql.ResolveTypeParams) *graphql.Object {
|
||||
ctx, ok := p.Context.Value("resolve").(*ResolveContext)
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
node, ok := p.Value.(NodeResult)
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
gql_type, exists := ctx.GQLContext.NodeTypes[node.Result.NodeType]
|
||||
ctx.Context.Log.Logf("gql", "GQL_INTERFACE_RESOLVE_TYPE(%+v): %+v - %t - %+v - %+v", node, gql_type, exists, required_extensions, *default_type)
|
||||
if exists == false {
|
||||
node_type_def, exists := ctx.Context.Nodes[node.Result.NodeType]
|
||||
if exists == false {
|
||||
return nil
|
||||
} else {
|
||||
for _, ext := range(required_extensions) {
|
||||
found := false
|
||||
for _, e := range(node_type_def.Extensions) {
|
||||
if e == ext {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if found == false {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return *default_type
|
||||
}
|
||||
|
||||
return gql_type
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package graphvent
|
||||
import (
|
||||
)
|
||||
|
@ -1,83 +0,0 @@
|
||||
package graphvent
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"github.com/graphql-go/graphql"
|
||||
)
|
||||
|
||||
func PrepResolve(p graphql.ResolveParams) (*ResolveContext, error) {
|
||||
resolve_context, ok := p.Context.Value("resolve").(*ResolveContext)
|
||||
if ok == false {
|
||||
return nil, fmt.Errorf("Bad resolve in params context")
|
||||
}
|
||||
|
||||
return resolve_context, nil
|
||||
}
|
||||
|
||||
// TODO: Make composabe by checkinf if K is a slice, then recursing in the same way that ExtractList does
|
||||
func ExtractParam[K interface{}](p graphql.ResolveParams, name string) (K, error) {
|
||||
var zero K
|
||||
arg_if, ok := p.Args[name]
|
||||
if ok == false {
|
||||
return zero, fmt.Errorf("No Arg of name %s", name)
|
||||
}
|
||||
|
||||
arg, ok := arg_if.(K)
|
||||
if ok == false {
|
||||
return zero, fmt.Errorf("Failed to cast arg %s(%+v) to %+v", name, arg_if, reflect.TypeOf(zero))
|
||||
}
|
||||
|
||||
return arg, nil
|
||||
}
|
||||
|
||||
func ExtractList[K interface{}](p graphql.ResolveParams, name string) ([]K, error) {
|
||||
var zero K
|
||||
|
||||
arg_list, err := ExtractParam[[]interface{}](p, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make([]K, len(arg_list))
|
||||
for i, val := range(arg_list) {
|
||||
val_conv, ok := arg_list[i].(K)
|
||||
if ok == false {
|
||||
return nil, fmt.Errorf("Failed to cast arg %s[%d](%+v) to %+v", name, i, val, reflect.TypeOf(zero))
|
||||
}
|
||||
ret[i] = val_conv
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func ExtractID(p graphql.ResolveParams, name string) (NodeID, error) {
|
||||
id_str, err := ExtractParam[string](p, name)
|
||||
if err != nil {
|
||||
return ZeroID, err
|
||||
}
|
||||
|
||||
id, err := ParseID(id_str)
|
||||
if err != nil {
|
||||
return ZeroID, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func ResolveNodeID(p graphql.ResolveParams) (interface{}, error) {
|
||||
node, ok := p.Source.(NodeResult)
|
||||
if ok == false {
|
||||
return nil, fmt.Errorf("Can't get NodeID from %+v", reflect.TypeOf(p.Source))
|
||||
}
|
||||
|
||||
return node.ID, nil
|
||||
}
|
||||
|
||||
func ResolveNodeTypeHash(p graphql.ResolveParams) (interface{}, error) {
|
||||
node, ok := p.Source.(NodeResult)
|
||||
if ok == false {
|
||||
return nil, fmt.Errorf("Can't get TypeHash from %+v", reflect.TypeOf(p.Source))
|
||||
}
|
||||
|
||||
return uint64(node.Result.NodeType), nil
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package graphvent
|
||||
import (
|
||||
)
|
||||
|
@ -1,37 +0,0 @@
|
||||
package graphvent
|
||||
|
||||
import (
|
||||
"github.com/graphql-go/graphql"
|
||||
)
|
||||
|
||||
func AddNodeFields(object *graphql.Object) {
|
||||
object.AddFieldConfig("ID", &graphql.Field{
|
||||
Type: graphql.String,
|
||||
Resolve: ResolveNodeID,
|
||||
})
|
||||
|
||||
object.AddFieldConfig("TypeHash", &graphql.Field{
|
||||
Type: graphql.String,
|
||||
Resolve: ResolveNodeTypeHash,
|
||||
})
|
||||
}
|
||||
|
||||
func NewGQLNodeType(gql_name string, node_type NodeType, interfaces []*graphql.Interface, init func(*Type)) *Type {
|
||||
var gql Type
|
||||
gql.Type = graphql.NewObject(graphql.ObjectConfig{
|
||||
Name: gql_name,
|
||||
Interfaces: interfaces,
|
||||
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
|
||||
node, ok := p.Value.(NodeResult)
|
||||
if ok == false {
|
||||
return false
|
||||
}
|
||||
return node.Result.NodeType == node_type
|
||||
},
|
||||
Fields: graphql.Fields{},
|
||||
})
|
||||
gql.List = graphql.NewList(gql.Type)
|
||||
|
||||
init(&gql)
|
||||
return &gql
|
||||
}
|
@ -1 +0,0 @@
|
||||
Subproject commit dd82b0abca1e9a52fba07e8935da2164e2c3f565
|
Loading…
Reference in New Issue