84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
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
|
|
}
|