2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
2023-07-29 00:28:44 -06:00
|
|
|
func PrepResolve(p graphql.ResolveParams) (*ResolveContext, error) {
|
2023-07-21 18:51:42 -06:00
|
|
|
resolve_context, ok := p.Context.Value("resolve").(*ResolveContext)
|
2023-07-21 15:16:35 -06:00
|
|
|
if ok == false {
|
2023-07-29 00:28:44 -06:00
|
|
|
return nil, fmt.Errorf("Bad resolve in params context")
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
2023-07-26 00:18:11 -06:00
|
|
|
|
2023-07-29 00:28:44 -06:00
|
|
|
return resolve_context, nil
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 20:27:37 -06:00
|
|
|
// TODO: Make composabe by checkinf if K is a slice, then recursing in the same way that ExtractList does
|
2023-07-21 15:16:35 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 20:27:37 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
func ResolveNodeResult(p graphql.ResolveParams, resolve_fn func(NodeResult)(interface{}, error)) (interface{}, error) {
|
|
|
|
node, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("p.Value is not NodeResult")
|
|
|
|
}
|
2023-07-21 15:16:35 -06:00
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
return resolve_fn(node)
|
2023-07-26 00:18:11 -06:00
|
|
|
}
|
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
func ResolveNodeID(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return ResolveNodeResult(p, func(node NodeResult) (interface{}, error) {
|
|
|
|
return node.ID, nil
|
|
|
|
})
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
func ResolveNodeTypeHash(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return ResolveNodeResult(p, func(node NodeResult) (interface{}, error) {
|
|
|
|
return Hash(node.Result.NodeType), nil
|
|
|
|
})
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func GQLSignalFn(p graphql.ResolveParams, fn func(Signal, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
|
|
|
|
if signal, ok := p.Source.(Signal); ok {
|
2023-07-21 15:16:35 -06:00
|
|
|
return fn(signal, p)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to event")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalType(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:08:14 -06:00
|
|
|
return GQLSignalFn(p, func(signal Signal, p graphql.ResolveParams)(interface{}, error){
|
2023-07-21 15:16:35 -06:00
|
|
|
return signal.Type(), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalDirection(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:08:14 -06:00
|
|
|
return GQLSignalFn(p, func(signal Signal, p graphql.ResolveParams)(interface{}, error){
|
2023-07-21 15:16:35 -06:00
|
|
|
direction := signal.Direction()
|
|
|
|
if direction == Up {
|
|
|
|
return "up", nil
|
|
|
|
} else if direction == Down {
|
|
|
|
return "down", nil
|
|
|
|
} else if direction == Direct {
|
|
|
|
return "direct", nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Invalid direction: %+v", direction)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalString(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:08:14 -06:00
|
|
|
return GQLSignalFn(p, func(signal Signal, p graphql.ResolveParams)(interface{}, error){
|
|
|
|
ser, err := signal.Serialize()
|
|
|
|
return string(ser), err
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
}
|