2023-07-09 14:30:30 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2024-03-04 17:30:42 -07:00
|
|
|
"crypto/ecdh"
|
2024-03-04 22:11:40 -07:00
|
|
|
"encoding"
|
2024-03-04 17:30:42 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2024-03-04 21:30:11 -07:00
|
|
|
"strconv"
|
2024-03-04 22:11:40 -07:00
|
|
|
"strings"
|
2024-03-04 17:30:42 -07:00
|
|
|
"sync"
|
2024-03-30 23:57:18 -06:00
|
|
|
"sync/atomic"
|
2024-03-04 22:11:40 -07:00
|
|
|
"time"
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-03-04 17:30:42 -07:00
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
"github.com/graphql-go/graphql/language/ast"
|
2023-07-27 23:15:58 -06:00
|
|
|
)
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
var (
|
|
|
|
NodeNotFoundError = errors.New("Node not found in DB")
|
2023-08-07 20:26:02 -06:00
|
|
|
ECDH = ecdh.X25519()
|
2023-07-28 00:04:18 -06:00
|
|
|
)
|
|
|
|
|
2024-03-28 21:23:22 -06:00
|
|
|
type SerializeFn func(ctx *Context, value reflect.Value, data []byte) (int, error)
|
2024-03-30 23:57:18 -06:00
|
|
|
type SerializedSizeFn func(ctx *Context, value reflect.Value) (int, error)
|
2024-03-08 00:22:51 -07:00
|
|
|
type DeserializeFn func(ctx *Context, data []byte) (reflect.Value, []byte, error)
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
type NodeFieldInfo struct {
|
|
|
|
Extension ExtType
|
|
|
|
Index []int
|
|
|
|
Type graphql.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
type StructFieldInfo struct {
|
2024-03-08 00:22:51 -07:00
|
|
|
Index []int
|
|
|
|
Type reflect.Type
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
type TypeInfo struct {
|
2024-03-08 00:22:51 -07:00
|
|
|
Serialized SerializedType
|
|
|
|
Reflect reflect.Type
|
2024-03-04 17:30:42 -07:00
|
|
|
Type graphql.Type
|
2024-03-08 00:22:51 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
Fields map[FieldTag]StructFieldInfo
|
2024-03-08 00:22:51 -07:00
|
|
|
PostDeserializeIndex int
|
|
|
|
|
|
|
|
Serialize SerializeFn
|
2024-03-30 23:57:18 -06:00
|
|
|
SerializedSize SerializedSizeFn
|
2024-03-08 00:22:51 -07:00
|
|
|
Deserialize DeserializeFn
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
2024-03-03 15:45:45 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
type ExtensionFieldInfo struct {
|
|
|
|
Index []int
|
|
|
|
Type reflect.Type
|
|
|
|
NodeTag string
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
type ExtensionInfo struct {
|
2024-03-08 00:22:51 -07:00
|
|
|
ExtType
|
2024-03-28 20:28:07 -06:00
|
|
|
Type reflect.Type
|
|
|
|
Fields map[string]ExtensionFieldInfo
|
2023-08-31 19:50:32 -06:00
|
|
|
Data interface{}
|
|
|
|
}
|
2023-08-01 20:55:15 -06:00
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
type NodeInfo struct {
|
2024-03-08 00:22:51 -07:00
|
|
|
NodeType
|
|
|
|
Type *graphql.Object
|
2024-03-28 20:28:07 -06:00
|
|
|
RequiredExtensions []ExtType
|
|
|
|
Fields map[string]NodeFieldInfo
|
|
|
|
ReverseFields map[ExtType]map[Tag]string
|
2023-08-01 20:55:15 -06:00
|
|
|
}
|
2023-07-29 00:28:44 -06:00
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
type InterfaceInfo struct {
|
2024-03-28 20:28:07 -06:00
|
|
|
Type *graphql.Interface
|
|
|
|
Fields map[string]graphql.Type
|
2024-03-10 15:41:13 -06:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
type ContextNode struct {
|
|
|
|
Node *Node
|
|
|
|
Status chan string
|
|
|
|
Command chan string
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// A Context stores all the data to run a graphvent process
|
2023-07-09 14:30:30 -06:00
|
|
|
type Context struct {
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2023-07-10 22:31:43 -06:00
|
|
|
// DB is the database connection used to load and write nodes
|
2024-03-30 15:42:06 -06:00
|
|
|
DB Database
|
2023-07-27 16:06:56 -06:00
|
|
|
// Logging interface
|
2023-07-09 14:30:30 -06:00
|
|
|
Log Logger
|
2024-03-04 17:30:42 -07:00
|
|
|
|
|
|
|
// Mapped types
|
2024-03-28 20:28:07 -06:00
|
|
|
Types map[reflect.Type]*TypeInfo
|
|
|
|
TypesReverse map[SerializedType]*TypeInfo
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Map between database extension hashes and the registered info
|
2024-03-28 20:28:07 -06:00
|
|
|
Extensions map[ExtType]ExtensionInfo
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
// Map between GQL interface name and the registered info
|
|
|
|
Interfaces map[string]InterfaceInfo
|
2024-03-10 15:41:13 -06:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
// Map between database node type hashes and the registered info
|
|
|
|
NodeTypes map[NodeType]NodeInfo
|
2024-03-03 15:45:45 -07:00
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Routing map to all the nodes local to this context
|
2024-03-28 20:28:07 -06:00
|
|
|
nodesLock sync.RWMutex
|
2024-03-30 23:57:18 -06:00
|
|
|
nodes map[NodeID]ContextNode
|
|
|
|
|
|
|
|
running atomic.Bool
|
2024-03-28 20:28:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gqltype(ctx *Context, t reflect.Type, node_type string) graphql.Type {
|
|
|
|
gql, err := ctx.GQLType(t, node_type)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
return gql
|
|
|
|
}
|
2023-07-10 21:15:01 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
func (ctx *Context) GQLType(t reflect.Type, node_type string) (graphql.Type, error) {
|
|
|
|
if t == reflect.TypeFor[NodeID]() {
|
2024-03-21 14:22:34 -06:00
|
|
|
if node_type == "" {
|
|
|
|
node_type = "Base"
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
|
|
|
interface_info, mapped := ctx.Interfaces[node_type]
|
2024-03-08 17:43:30 -07:00
|
|
|
if mapped == false {
|
2024-03-28 20:28:07 -06:00
|
|
|
type_info, mapped := ctx.NodeTypes[NodeTypeFor(node_type)]
|
|
|
|
if mapped {
|
|
|
|
return type_info.Type, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Cannot get GQL type for unregistered Node Type \"%s\"", node_type)
|
|
|
|
}
|
2024-03-08 17:43:30 -07:00
|
|
|
} else {
|
2024-03-28 20:28:07 -06:00
|
|
|
return interface_info.Type, nil
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
} else {
|
2024-03-28 20:28:07 -06:00
|
|
|
info, mapped := ctx.Types[t]
|
2024-03-08 17:43:30 -07:00
|
|
|
if mapped {
|
|
|
|
return info.Type, nil
|
|
|
|
} else {
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Array:
|
2024-03-28 20:28:07 -06:00
|
|
|
info, mapped := ctx.Types[t.Elem()]
|
2024-03-08 17:43:30 -07:00
|
|
|
if mapped {
|
|
|
|
return graphql.NewList(info.Type), nil
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
2024-03-08 17:43:30 -07:00
|
|
|
case reflect.Slice:
|
2024-03-28 20:28:07 -06:00
|
|
|
info, mapped := ctx.Types[t.Elem()]
|
2024-03-08 17:43:30 -07:00
|
|
|
if mapped {
|
|
|
|
return graphql.NewList(info.Type), nil
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
2024-03-28 20:28:07 -06:00
|
|
|
info, exists := ctx.Types[t]
|
2024-03-08 17:43:30 -07:00
|
|
|
if exists {
|
|
|
|
return info.Type, nil
|
|
|
|
} else {
|
|
|
|
err := RegisterMap(ctx, t, node_type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
map_type := ctx.Types[t].Type
|
2024-03-08 17:43:30 -07:00
|
|
|
return map_type, nil
|
|
|
|
}
|
|
|
|
case reflect.Pointer:
|
|
|
|
return ctx.GQLType(t.Elem(), node_type)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
2024-03-08 17:43:30 -07:00
|
|
|
return nil, fmt.Errorf("Can't convert %s to GQL type", t)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 22:09:40 -07:00
|
|
|
type Pair struct {
|
|
|
|
Key any
|
|
|
|
Val any
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
func RegisterMap(ctx *Context, reflect_type reflect.Type, node_type string) error {
|
2024-03-21 14:22:34 -06:00
|
|
|
var node_types []string
|
|
|
|
if node_type == "" {
|
|
|
|
node_types = []string{"", ""}
|
|
|
|
} else {
|
|
|
|
node_types = strings.SplitN(node_type, ":", 2)
|
2024-03-08 17:43:30 -07:00
|
|
|
|
2024-03-21 14:22:34 -06:00
|
|
|
if len(node_types) != 2 {
|
|
|
|
return fmt.Errorf("Invalid node tag for map type %s: \"%s\"", reflect_type, node_type)
|
|
|
|
}
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
key_type, err := ctx.GQLType(reflect_type.Key(), node_types[0])
|
2024-03-08 14:35:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
key_resolve := ctx.GQLResolve(reflect_type.Key(), node_types[0])
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
val_type, err := ctx.GQLType(reflect_type.Elem(), node_types[1])
|
2024-03-08 14:35:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
val_resolve := ctx.GQLResolve(reflect_type.Elem(), node_types[1])
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
gql_name := strings.ReplaceAll(reflect_type.String(), ".", "_")
|
|
|
|
gql_name = strings.ReplaceAll(gql_name, "[", "_")
|
|
|
|
gql_name = strings.ReplaceAll(gql_name, "]", "_")
|
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
gql_pair := graphql.NewObject(graphql.ObjectConfig{
|
2024-03-08 14:35:23 -07:00
|
|
|
Name: gql_name,
|
2024-03-04 21:30:11 -07:00
|
|
|
Fields: graphql.Fields{
|
|
|
|
"Key": &graphql.Field{
|
|
|
|
Type: key_type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2024-03-09 22:09:40 -07:00
|
|
|
source, ok := p.Source.(Pair)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("%+v is not Pair", source)
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
if key_resolve == nil {
|
|
|
|
return source.Key, nil
|
|
|
|
} else {
|
|
|
|
return key_resolve(source.Key, p)
|
|
|
|
}
|
2024-03-04 21:30:11 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"Value": &graphql.Field{
|
|
|
|
Type: val_type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2024-03-09 22:09:40 -07:00
|
|
|
source, ok := p.Source.(Pair)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("%+v is not Pair", source)
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
if val_resolve == nil {
|
|
|
|
return source.Val, nil
|
|
|
|
} else {
|
|
|
|
return val_resolve(source.Val, p)
|
|
|
|
}
|
2024-03-04 21:30:11 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
gql_map := graphql.NewList(gql_pair)
|
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
serialized_type := SerializeType(reflect_type)
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Types[reflect_type] = &TypeInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
2024-03-04 21:30:11 -07:00
|
|
|
Type: gql_map,
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.TypesReverse[serialized_type] = ctx.Types[reflect_type]
|
2024-03-04 21:30:11 -07:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
func BuildSchema(ctx *Context, query, mutation *graphql.Object) (graphql.Schema, error) {
|
|
|
|
types := []graphql.Type{}
|
2024-03-08 17:43:30 -07:00
|
|
|
ctx.Log.Logf("gql", "Building Schema")
|
2024-03-03 15:45:45 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
for _, info := range(ctx.Types) {
|
2024-03-17 14:25:34 -06:00
|
|
|
if info.Type != nil {
|
|
|
|
types = append(types, info.Type)
|
|
|
|
}
|
2024-03-04 22:11:40 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
for _, info := range(ctx.NodeTypes) {
|
2024-03-08 00:22:51 -07:00
|
|
|
types = append(types, info.Type)
|
2024-03-28 20:28:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range(ctx.Interfaces) {
|
|
|
|
types = append(types, info.Type)
|
2024-03-04 22:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
subscription := graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Subscription",
|
2024-03-10 15:41:13 -06:00
|
|
|
Fields: graphql.Fields{},
|
2024-03-04 17:30:42 -07:00
|
|
|
})
|
2023-10-29 18:26:14 -06:00
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
for query_name, query := range(query.Fields()) {
|
|
|
|
args := graphql.FieldConfigArgument{}
|
|
|
|
for _, arg := range(query.Args) {
|
|
|
|
args[arg.Name()] = &graphql.ArgumentConfig{
|
|
|
|
Type: arg.Type,
|
|
|
|
DefaultValue: arg.DefaultValue,
|
|
|
|
Description: arg.Description(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subscription.AddFieldConfig(query_name, &graphql.Field{
|
|
|
|
Type: query.Type,
|
|
|
|
Args: args,
|
|
|
|
Subscribe: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-25 18:49:52 -06:00
|
|
|
c, err := ctx.Ext.AddSubscription(ctx.ID, ctx)
|
2024-03-10 15:41:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-25 18:49:52 -06:00
|
|
|
c <- nil
|
2024-03-10 15:41:13 -06:00
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
Resolve: query.Resolve,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
return graphql.NewSchema(graphql.SchemaConfig{
|
|
|
|
Types: types,
|
|
|
|
Query: query,
|
|
|
|
Subscription: subscription,
|
|
|
|
Mutation: mutation,
|
|
|
|
})
|
2023-08-31 19:50:32 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func RegisterExtension[E any, T interface { *E; Extension}](ctx *Context, data interface{}) error {
|
2024-03-04 22:11:40 -07:00
|
|
|
reflect_type := reflect.TypeFor[E]()
|
2024-03-28 20:28:07 -06:00
|
|
|
ext_type := ExtTypeFor[E, T]()
|
2023-08-31 19:50:32 -06:00
|
|
|
_, exists := ctx.Extensions[ext_type]
|
2023-07-09 14:30:30 -06:00
|
|
|
if exists == true {
|
2024-03-03 15:45:45 -07:00
|
|
|
return fmt.Errorf("Cannot register extension %+v of type %+v, type already exists in context", reflect_type, ext_type)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
fields := map[string]ExtensionFieldInfo{}
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
for _, field := range(reflect.VisibleFields(reflect_type)) {
|
2024-03-04 17:30:42 -07:00
|
|
|
gv_tag, tagged_gv := field.Tag.Lookup("gv")
|
2024-03-28 20:28:07 -06:00
|
|
|
node_tag := field.Tag.Get("node")
|
2024-03-04 17:30:42 -07:00
|
|
|
if tagged_gv {
|
2024-03-28 20:28:07 -06:00
|
|
|
fields[gv_tag] = ExtensionFieldInfo{
|
2024-03-08 17:43:30 -07:00
|
|
|
Index: field.Index,
|
|
|
|
Type: field.Type,
|
2024-03-28 20:28:07 -06:00
|
|
|
NodeTag: node_tag,
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Extensions[ext_type] = ExtensionInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
ExtType: ext_type,
|
2024-03-28 20:28:07 -06:00
|
|
|
Type: reflect_type,
|
2023-07-26 00:18:11 -06:00
|
|
|
Data: data,
|
2024-03-04 17:30:42 -07:00
|
|
|
Fields: fields,
|
2023-07-10 21:15:01 -06:00
|
|
|
}
|
2023-08-31 19:50:32 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
type FieldMapping struct {
|
|
|
|
Extension ExtType
|
|
|
|
Tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterNodeInterface(ctx *Context, name string, fields map[string]graphql.Type) error {
|
|
|
|
_, exists := ctx.Interfaces[name]
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Cannot register Node Interface %s, already registered", name)
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
gql_fields := graphql.Fields{
|
|
|
|
"ID": &graphql.Field{
|
|
|
|
Type: ctx.Types[reflect.TypeFor[NodeID]()].Type,
|
|
|
|
},
|
|
|
|
"Type": &graphql.Field{
|
|
|
|
Type: ctx.Types[reflect.TypeFor[NodeType]()].Type,
|
|
|
|
},
|
|
|
|
}
|
2024-03-09 22:09:40 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
for field_name, field_type := range(fields) {
|
|
|
|
_, exists := gql_fields[field_name]
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Cannot register interface %s with duplicate field %s", name, field_name)
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
gql_fields[field_name] = &graphql.Field{
|
|
|
|
Type: field_type,
|
2024-03-09 22:09:40 -07:00
|
|
|
}
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
|
|
|
gql := graphql.NewInterface(graphql.InterfaceConfig{
|
2024-03-08 00:22:51 -07:00
|
|
|
Name: name,
|
2024-03-28 20:28:07 -06:00
|
|
|
Fields: gql_fields,
|
2024-03-08 17:43:30 -07:00
|
|
|
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
|
|
|
|
ctx_val := p.Context.Value("resolve")
|
|
|
|
ctx, ok := ctx_val.(*ResolveContext)
|
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
val, ok := p.Value.(NodeResult)
|
|
|
|
if ok == false {
|
2024-03-09 22:09:40 -07:00
|
|
|
ctx.Context.Log.Logf("gql", "Interface ResolveType got bad Value %+v", p.Value)
|
2024-03-08 17:43:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
node_info, exists := ctx.Context.NodeTypes[val.NodeType]
|
2024-03-08 17:43:30 -07:00
|
|
|
if exists == false {
|
2024-03-09 22:09:40 -07:00
|
|
|
ctx.Context.Log.Logf("gql", "Interface ResolveType got bad NodeType", val.NodeType)
|
2024-03-08 17:43:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return node_info.Type
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Interfaces[name] = InterfaceInfo{
|
|
|
|
Type: gql,
|
|
|
|
Fields: fields,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterNodeType(ctx *Context, name string, mappings map[string]FieldMapping) error {
|
|
|
|
node_type := NodeTypeFor(name)
|
|
|
|
_, exists := ctx.NodeTypes[node_type]
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Cannot register node type %s, already registered", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := map[string]NodeFieldInfo{}
|
|
|
|
reverse_fields := map[ExtType]map[Tag]string{}
|
|
|
|
|
|
|
|
gql_fields := graphql.Fields{
|
|
|
|
"ID": &graphql.Field{
|
|
|
|
Type: ctx.Types[reflect.TypeFor[NodeID]()].Type,
|
|
|
|
Resolve: ResolveNodeID,
|
|
|
|
},
|
|
|
|
"Type": &graphql.Field{
|
|
|
|
Type: ctx.Types[reflect.TypeFor[NodeType]()].Type,
|
|
|
|
Resolve: ResolveNodeType,
|
2024-03-08 00:22:51 -07:00
|
|
|
},
|
2024-03-28 20:28:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ext_map := map[ExtType]bool{}
|
|
|
|
for field_name, mapping := range(mappings) {
|
|
|
|
_, duplicate := fields[field_name]
|
|
|
|
if duplicate {
|
|
|
|
return fmt.Errorf("Cannot register node type %s, contains duplicate field %s", name, field_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_info, exists := ctx.Extensions[mapping.Extension]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Cannot register node type %s, unknown extension %s", name, mapping.Extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_map[mapping.Extension] = true
|
|
|
|
|
|
|
|
ext_field, exists := ext_info.Fields[mapping.Tag]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Cannot register node type %s, extension %s has no field %s", name, mapping.Extension, mapping.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
gql_type, err := ctx.GQLType(ext_field.Type, ext_field.NodeTag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot register node type %s, GQLType error: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gql_resolve := ctx.GQLResolve(ext_field.Type, ext_field.NodeTag)
|
|
|
|
|
|
|
|
fields[field_name] = NodeFieldInfo{
|
|
|
|
Extension: mapping.Extension,
|
|
|
|
Index: ext_field.Index,
|
|
|
|
Type: gql_type,
|
|
|
|
}
|
|
|
|
|
|
|
|
gql_fields[field_name] = &graphql.Field{
|
|
|
|
Type: gql_type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
node, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Can't resolve Node field on non-Node %s", reflect.TypeOf(p.Source))
|
|
|
|
}
|
|
|
|
|
|
|
|
return gql_resolve(node.Data[field_name], p)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gql := graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: name,
|
|
|
|
Interfaces: ctx.GQLInterfaces(fields),
|
|
|
|
Fields: gql_fields,
|
2024-03-08 00:22:51 -07:00
|
|
|
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
|
2024-03-08 17:43:30 -07:00
|
|
|
source, ok := p.Value.(NodeResult)
|
|
|
|
if ok == false {
|
2024-03-08 00:22:51 -07:00
|
|
|
return false
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
return source.NodeType == node_type
|
2024-03-08 00:22:51 -07:00
|
|
|
},
|
|
|
|
})
|
2023-09-02 17:30:52 -06:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
extensions := []ExtType{}
|
|
|
|
for ext_type := range(ext_map) {
|
|
|
|
extensions = append(extensions, ext_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.NodeTypes[node_type] = NodeInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
NodeType: node_type,
|
|
|
|
Type: gql,
|
2024-03-09 22:09:40 -07:00
|
|
|
Fields: fields,
|
2024-03-28 20:28:07 -06:00
|
|
|
ReverseFields: reverse_fields,
|
|
|
|
RequiredExtensions: extensions,
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
// Returns a function which returns a list of interfaces from the context whose fields are a subset of fields
|
|
|
|
func (ctx *Context) GQLInterfaces(fields map[string]NodeFieldInfo) graphql.InterfacesThunk {
|
2024-03-09 22:09:40 -07:00
|
|
|
return func() []*graphql.Interface {
|
|
|
|
interfaces := []*graphql.Interface{}
|
2024-03-28 20:28:07 -06:00
|
|
|
for _, interface_info := range(ctx.Interfaces) {
|
|
|
|
match := true
|
|
|
|
for field_name, field_type := range(interface_info.Fields) {
|
|
|
|
field, exists := fields[field_name]
|
|
|
|
if exists == false {
|
|
|
|
match = false
|
|
|
|
break
|
|
|
|
} else if field.Type != field_type {
|
|
|
|
match = false
|
|
|
|
break
|
2024-03-09 22:09:40 -07:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
if match {
|
|
|
|
interfaces = append(interfaces, interface_info.Type)
|
|
|
|
}
|
2024-03-09 22:09:40 -07:00
|
|
|
}
|
|
|
|
return interfaces
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:22:34 -06:00
|
|
|
func RegisterSignal[S Signal](ctx *Context) error {
|
|
|
|
return RegisterObject[S](ctx)
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
func RegisterObject[T any](ctx *Context) error {
|
2024-03-03 15:45:45 -07:00
|
|
|
reflect_type := reflect.TypeFor[T]()
|
2024-03-04 17:30:42 -07:00
|
|
|
serialized_type := SerializedTypeFor[T]()
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
_, exists := ctx.Types[reflect_type]
|
2024-03-04 17:30:42 -07:00
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("%+v already registered in TypeMap", reflect_type)
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
gql_name := strings.ReplaceAll(reflect_type.String(), ".", "_")
|
2024-03-04 17:30:42 -07:00
|
|
|
gql := graphql.NewObject(graphql.ObjectConfig{
|
2024-03-08 14:35:23 -07:00
|
|
|
Name: gql_name,
|
2024-03-04 17:30:42 -07:00
|
|
|
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
|
|
|
|
return reflect_type == reflect.TypeOf(p.Value)
|
|
|
|
},
|
|
|
|
Fields: graphql.Fields{},
|
|
|
|
})
|
2024-03-08 00:22:51 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
field_infos := map[FieldTag]StructFieldInfo{}
|
2024-03-08 00:22:51 -07:00
|
|
|
|
|
|
|
post_deserialize, post_deserialize_exists := reflect.PointerTo(reflect_type).MethodByName("PostDeserialize")
|
|
|
|
post_deserialize_index := -1
|
|
|
|
if post_deserialize_exists {
|
|
|
|
post_deserialize_index = post_deserialize.Index
|
|
|
|
}
|
2024-03-04 17:30:42 -07:00
|
|
|
|
|
|
|
for _, field := range(reflect.VisibleFields(reflect_type)) {
|
|
|
|
gv_tag, tagged_gv := field.Tag.Lookup("gv")
|
|
|
|
if tagged_gv {
|
2024-03-08 17:43:30 -07:00
|
|
|
node_tag := field.Tag.Get("node")
|
2024-03-28 20:28:07 -06:00
|
|
|
field_infos[GetFieldTag(gv_tag)] = StructFieldInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
Type: field.Type,
|
|
|
|
Index: field.Index,
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
gql_type, err := ctx.GQLType(field.Type, node_tag)
|
2024-03-08 14:35:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
2024-03-08 17:43:30 -07:00
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
gql_resolve := ctx.GQLResolve(field.Type, node_tag)
|
2024-03-04 17:30:42 -07:00
|
|
|
gql.AddFieldConfig(gv_tag, &graphql.Field{
|
2024-03-04 21:30:11 -07:00
|
|
|
Type: gql_type,
|
2024-03-04 17:30:42 -07:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
val, ok := p.Source.(T)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("%s is not %s", reflect.TypeOf(p.Source), reflect_type)
|
|
|
|
}
|
2024-03-17 14:25:34 -06:00
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
value, err := reflect.ValueOf(val).FieldByIndexErr(field.Index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
if gql_resolve == nil {
|
|
|
|
return value.Interface(), nil
|
|
|
|
} else {
|
|
|
|
return gql_resolve(value.Interface(), p)
|
|
|
|
}
|
2024-03-04 17:30:42 -07:00
|
|
|
},
|
|
|
|
})
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Types[reflect_type] = &TypeInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
PostDeserializeIndex: post_deserialize_index,
|
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
|
|
|
Fields: field_infos,
|
2024-03-04 17:30:42 -07:00
|
|
|
Type: gql,
|
2023-09-11 21:47:53 -06:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.TypesReverse[serialized_type] = ctx.Types[reflect_type]
|
2023-09-11 21:47:53 -06:00
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:13:54 -06:00
|
|
|
func RegisterObjectNoGQL[T any](ctx *Context) error {
|
|
|
|
reflect_type := reflect.TypeFor[T]()
|
|
|
|
serialized_type := SerializedTypeFor[T]()
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
_, exists := ctx.Types[reflect_type]
|
2024-03-21 14:13:54 -06:00
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("%+v already registered in TypeMap", reflect_type)
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
field_infos := map[FieldTag]StructFieldInfo{}
|
2024-03-21 14:13:54 -06:00
|
|
|
|
|
|
|
post_deserialize, post_deserialize_exists := reflect.PointerTo(reflect_type).MethodByName("PostDeserialize")
|
|
|
|
post_deserialize_index := -1
|
|
|
|
if post_deserialize_exists {
|
|
|
|
post_deserialize_index = post_deserialize.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range(reflect.VisibleFields(reflect_type)) {
|
|
|
|
gv_tag, tagged_gv := field.Tag.Lookup("gv")
|
|
|
|
if tagged_gv {
|
2024-03-28 20:28:07 -06:00
|
|
|
field_infos[GetFieldTag(gv_tag)] = StructFieldInfo{
|
2024-03-21 14:13:54 -06:00
|
|
|
Type: field.Type,
|
|
|
|
Index: field.Index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Types[reflect_type] = &TypeInfo{
|
2024-03-21 14:13:54 -06:00
|
|
|
PostDeserializeIndex: post_deserialize_index,
|
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
|
|
|
Fields: field_infos,
|
|
|
|
Type: nil,
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.TypesReverse[serialized_type] = ctx.Types[reflect_type]
|
2024-03-21 14:13:54 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
func identity(value interface{}) interface{} {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringify(value interface{}) interface{} {
|
|
|
|
v, ok := value.(encoding.TextMarshaler)
|
|
|
|
if ok {
|
|
|
|
b, err := v.MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unstringify[T any, E interface { *T; encoding.TextUnmarshaler }](value interface{}) interface{} {
|
|
|
|
str, ok := value.(string)
|
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 22:09:40 -07:00
|
|
|
var tmp E = new(T)
|
2024-03-04 21:30:11 -07:00
|
|
|
err := tmp.UnmarshalText([]byte(str))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func unstringifyAST[T any, E interface { *T; encoding.TextUnmarshaler}](value ast.Value)interface{} {
|
|
|
|
str, ok := value.(*ast.StringValue)
|
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp E
|
|
|
|
err := tmp.UnmarshalText([]byte(str.Value))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func coerce[T any](value interface{}) interface{} {
|
|
|
|
t := reflect.TypeFor[T]()
|
|
|
|
if reflect.TypeOf(value).ConvertibleTo(t) {
|
|
|
|
return value.(T)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func astString[T ~string](value ast.Value) interface{} {
|
|
|
|
str, ok := value.(*ast.StringValue)
|
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return T(str.Value)
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
func astBool[T ~bool](value ast.Value) interface{} {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case *ast.BooleanValue:
|
|
|
|
if value.Value {
|
|
|
|
return T(true)
|
|
|
|
} else {
|
|
|
|
return T(false)
|
|
|
|
}
|
|
|
|
case *ast.IntValue:
|
|
|
|
i, err := strconv.Atoi(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return i != 0
|
|
|
|
case *ast.StringValue:
|
|
|
|
b, err := strconv.ParseBool(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
func astInt[T constraints.Integer](value ast.Value) interface{} {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case *ast.BooleanValue:
|
|
|
|
if value.Value {
|
|
|
|
return T(1)
|
|
|
|
} else {
|
|
|
|
return T(0)
|
|
|
|
}
|
|
|
|
case *ast.StringValue:
|
|
|
|
i, err := strconv.Atoi(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return T(i)
|
|
|
|
}
|
|
|
|
case *ast.IntValue:
|
|
|
|
i, err := strconv.Atoi(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return T(i)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 14:25:34 -06:00
|
|
|
func RegisterEnum[E comparable](ctx *Context, str_map map[E]string) error {
|
|
|
|
reflect_type := reflect.TypeFor[E]()
|
|
|
|
serialized_type := SerializedTypeFor[E]()
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
_, exists := ctx.Types[reflect_type]
|
2024-03-17 14:25:34 -06:00
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("%+v already registered in TypeMap", reflect_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
value_config := graphql.EnumValueConfigMap{}
|
|
|
|
|
|
|
|
for value, value_name := range(str_map) {
|
|
|
|
value_config[value_name] = &graphql.EnumValueConfig{
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gql_name := strings.ReplaceAll(reflect_type.String(), ".", "_")
|
|
|
|
gql := graphql.NewEnum(graphql.EnumConfig{
|
|
|
|
Name: gql_name,
|
|
|
|
Values: value_config,
|
|
|
|
})
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Types[reflect_type] = &TypeInfo{
|
2024-03-17 14:25:34 -06:00
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
|
|
|
Type: gql,
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.TypesReverse[serialized_type] = ctx.Types[reflect_type]
|
2024-03-17 14:25:34 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
func RegisterScalar[S any](ctx *Context, to_json func(interface{})interface{}, from_json func(interface{})interface{}, from_ast func(ast.Value)interface{}, serialize SerializeFn, sizefn SerializedSizeFn, deserialize DeserializeFn) error {
|
2024-03-04 21:30:11 -07:00
|
|
|
reflect_type := reflect.TypeFor[S]()
|
|
|
|
serialized_type := SerializedTypeFor[S]()
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
_, exists := ctx.Types[reflect_type]
|
2024-03-04 17:30:42 -07:00
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("%+v already registered in TypeMap", reflect_type)
|
2023-08-31 19:50:32 -06:00
|
|
|
}
|
2023-10-29 18:26:14 -06:00
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
gql_name := strings.ReplaceAll(reflect_type.String(), ".", "_")
|
2024-03-04 17:30:42 -07:00
|
|
|
gql := graphql.NewScalar(graphql.ScalarConfig{
|
2024-03-08 14:35:23 -07:00
|
|
|
Name: gql_name,
|
2024-03-04 17:30:42 -07:00
|
|
|
Serialize: to_json,
|
|
|
|
ParseValue: from_json,
|
|
|
|
ParseLiteral: from_ast,
|
|
|
|
})
|
2023-08-31 19:50:32 -06:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.Types[reflect_type] = &TypeInfo{
|
2024-03-08 00:22:51 -07:00
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
2024-03-04 17:30:42 -07:00
|
|
|
Type: gql,
|
2024-03-28 20:28:07 -06:00
|
|
|
|
|
|
|
Serialize: serialize,
|
2024-03-30 23:57:18 -06:00
|
|
|
SerializedSize: sizefn,
|
2024-03-28 20:28:07 -06:00
|
|
|
Deserialize: deserialize,
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.TypesReverse[serialized_type] = ctx.Types[reflect_type]
|
2023-09-11 21:47:53 -06:00
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
func (ctx *Context) AddNode(id NodeID, node *Node, status chan string, command chan string) {
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.Lock()
|
2024-03-30 23:57:18 -06:00
|
|
|
ctx.nodes[id] = ContextNode{
|
|
|
|
Node: node,
|
|
|
|
Status: status,
|
|
|
|
Command: command,
|
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.Unlock()
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Node(id NodeID) (*Node, bool) {
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.RLock()
|
|
|
|
node, exists := ctx.nodes[id]
|
|
|
|
ctx.nodesLock.RUnlock()
|
2024-03-30 23:57:18 -06:00
|
|
|
return node.Node, exists
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ctx *Context) Delete(id NodeID) error {
|
|
|
|
err := ctx.Unload(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-30 23:57:18 -06:00
|
|
|
// TODO: also delete any associated data from DB
|
2024-03-03 16:37:03 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Unload(id NodeID) error {
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.Lock()
|
|
|
|
defer ctx.nodesLock.Unlock()
|
|
|
|
node, exists := ctx.nodes[id]
|
2023-10-06 20:04:53 -06:00
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("%s is not a node in ctx", id)
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
node.Command <- "stop"
|
|
|
|
returned := <- node.Status
|
|
|
|
|
|
|
|
if returned != "stopped" {
|
|
|
|
return fmt.Errorf(returned)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := node.Node.Unload(ctx)
|
2024-03-28 20:28:07 -06:00
|
|
|
delete(ctx.nodes, id)
|
2023-10-06 20:04:53 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ctx *Context) Stop() {
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.Lock()
|
|
|
|
for id, node := range(ctx.nodes) {
|
2024-03-30 23:57:18 -06:00
|
|
|
node.Command <- "stop"
|
|
|
|
returned := <- node.Status
|
|
|
|
|
|
|
|
if returned != "stopped" {
|
|
|
|
ctx.Log.Logf("node", "Node returned %s when commanded to stop", returned)
|
|
|
|
} else {
|
|
|
|
err := node.Node.Unload(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("node", "Error unloading %s: %s", id, err)
|
|
|
|
}
|
|
|
|
delete(ctx.nodes, id)
|
|
|
|
}
|
2023-10-06 20:04:53 -06:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
ctx.nodesLock.Unlock()
|
2023-10-06 20:04:53 -06:00
|
|
|
}
|
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
func (ctx *Context) Load(id NodeID) (*Node, error) {
|
2024-03-30 15:42:06 -06:00
|
|
|
node, err := ctx.DB.LoadNode(ctx, id)
|
2024-03-10 16:31:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
status := make(chan string, 0)
|
|
|
|
command := make(chan string, 0)
|
|
|
|
go runNode(ctx, node, status, command)
|
|
|
|
|
|
|
|
returned := <- status
|
|
|
|
if returned != "active" {
|
|
|
|
return nil, fmt.Errorf(returned)
|
2024-03-10 16:31:14 -06:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
ctx.AddNode(id, node, status, command)
|
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Get a node from the context, or load from the database if not loaded
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ctx *Context) getNode(id NodeID) (*Node, error) {
|
2023-07-28 12:46:06 -06:00
|
|
|
target, exists := ctx.Node(id)
|
|
|
|
|
2023-07-27 15:49:21 -06:00
|
|
|
if exists == false {
|
2023-07-27 16:48:39 -06:00
|
|
|
var err error
|
2024-03-10 16:31:14 -06:00
|
|
|
target, err = ctx.Load(id)
|
2023-07-27 16:48:39 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to load node %s: %w", id, err)
|
2023-07-27 16:48:39 -06:00
|
|
|
}
|
2023-07-27 15:49:21 -06:00
|
|
|
}
|
2023-07-27 16:48:39 -06:00
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 21:41:06 -06:00
|
|
|
// Route Messages to dest. Currently only local context routing is supported
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ctx *Context) Send(node *Node, messages []Message) error {
|
2023-08-07 20:26:02 -06:00
|
|
|
for _, msg := range(messages) {
|
2024-03-31 00:36:50 -06:00
|
|
|
ctx.Log.Logf("signal", "Sending %s to %s", msg.Signal, msg.Node)
|
|
|
|
if msg.Node == ZeroID {
|
2023-08-15 18:23:06 -06:00
|
|
|
panic("Can't send to null ID")
|
|
|
|
}
|
2024-03-31 00:36:50 -06:00
|
|
|
target, err := ctx.getNode(msg.Node)
|
2023-08-07 20:26:02 -06:00
|
|
|
if err == nil {
|
|
|
|
select {
|
2024-03-31 00:36:50 -06:00
|
|
|
case target.MsgChan <- Message{node.ID, msg.Signal}:
|
|
|
|
ctx.Log.Logf("signal_sent", "Sent %s to %s", msg.Signal, msg.Node)
|
2023-08-07 20:26:02 -06:00
|
|
|
default:
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
stack_str := string(buf[:n])
|
2024-03-31 00:36:50 -06:00
|
|
|
return fmt.Errorf("SIGNAL_OVERFLOW: %s - %s", msg.Node, stack_str)
|
2023-08-07 20:26:02 -06:00
|
|
|
}
|
|
|
|
} else if errors.Is(err, NodeNotFoundError) {
|
|
|
|
// TODO: Handle finding nodes in other contexts
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return err
|
2023-07-27 16:48:39 -06:00
|
|
|
}
|
2023-07-27 15:49:21 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return nil
|
2023-07-27 15:49:21 -06:00
|
|
|
}
|
|
|
|
|
2024-03-25 18:49:52 -06:00
|
|
|
func resolveNodeID(val interface{}, p graphql.ResolveParams) (interface{}, error) {
|
2024-03-30 23:57:18 -06:00
|
|
|
id, ok := val.(NodeID)
|
2024-03-25 18:49:52 -06:00
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("%+v is not NodeID", p.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResolveNode(id, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Cache these functions so they're not duplicated when called with the same t
|
2024-03-10 15:41:13 -06:00
|
|
|
func (ctx *Context)GQLResolve(t reflect.Type, node_type string) (func(interface{},graphql.ResolveParams)(interface{},error)) {
|
2024-03-25 18:49:52 -06:00
|
|
|
if t == reflect.TypeFor[NodeID]() {
|
|
|
|
return resolveNodeID
|
2024-03-09 22:09:40 -07:00
|
|
|
} else {
|
|
|
|
switch t.Kind() {
|
2024-03-25 18:49:52 -06:00
|
|
|
case reflect.Map:
|
|
|
|
return func(v interface{}, p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
val := reflect.ValueOf(v)
|
|
|
|
if val.Type() != t {
|
|
|
|
return nil, fmt.Errorf("%s is not %s", reflect.TypeOf(val), t)
|
|
|
|
} else {
|
|
|
|
pairs := make([]Pair, val.Len())
|
|
|
|
iter := val.MapRange()
|
|
|
|
i := 0
|
|
|
|
for iter.Next() {
|
|
|
|
pairs[i] = Pair{
|
|
|
|
Key: iter.Key().Interface(),
|
|
|
|
Val: iter.Value().Interface(),
|
|
|
|
}
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return pairs, nil
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 22:09:40 -07:00
|
|
|
case reflect.Pointer:
|
|
|
|
return ctx.GQLResolve(t.Elem(), node_type)
|
2024-03-10 15:41:13 -06:00
|
|
|
default:
|
2024-03-25 18:49:52 -06:00
|
|
|
return func(v interface{}, p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return v, nil
|
|
|
|
}
|
2024-03-09 22:09:40 -07:00
|
|
|
}
|
|
|
|
}
|
2024-03-10 15:41:13 -06:00
|
|
|
}
|
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
// Create a new Context with the base library content added
|
2024-03-30 15:42:06 -06:00
|
|
|
func NewContext(db Database, log Logger) (*Context, error) {
|
2024-03-28 20:28:07 -06:00
|
|
|
uuid.EnableRandPool()
|
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
ctx := &Context{
|
|
|
|
DB: db,
|
|
|
|
Log: log,
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
Types: map[reflect.Type]*TypeInfo{},
|
|
|
|
TypesReverse: map[SerializedType]*TypeInfo{},
|
|
|
|
Extensions: map[ExtType]ExtensionInfo{},
|
|
|
|
Interfaces: map[string]InterfaceInfo{},
|
|
|
|
NodeTypes: map[NodeType]NodeInfo{},
|
2024-03-03 15:45:45 -07:00
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
nodes: map[NodeID]ContextNode{},
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-03-25 18:49:52 -06:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
err = RegisterScalar[NodeID](ctx, stringify, unstringify[NodeID], unstringifyAST[NodeID],
|
2024-03-28 21:23:22 -06:00
|
|
|
func(ctx *Context, value reflect.Value, data []byte) (int, error) {
|
|
|
|
copy(data, value.Bytes())
|
|
|
|
return 16, nil
|
2024-03-30 23:57:18 -06:00
|
|
|
}, func(ctx *Context, value reflect.Value) (int, error) {
|
|
|
|
return 16, nil
|
2024-03-28 20:28:07 -06:00
|
|
|
}, func(ctx *Context, data []byte) (reflect.Value, []byte, error) {
|
|
|
|
if len(data) < 16 {
|
|
|
|
return reflect.Value{}, nil, fmt.Errorf("Not enough bytes to decode NodeID(got %d, want 16)", len(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
id := new(NodeID)
|
|
|
|
err := id.UnmarshalBinary(data[0:16])
|
|
|
|
if err != nil {
|
|
|
|
return reflect.Value{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.ValueOf(id).Elem(), data[16:], nil
|
|
|
|
})
|
2024-03-10 15:41:13 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register NodeID: %w", err)
|
2024-03-10 15:41:13 -06:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
|
|
|
err = RegisterScalar[uuid.UUID](ctx, stringify, unstringify[uuid.UUID], unstringifyAST[uuid.UUID],
|
2024-03-28 21:23:22 -06:00
|
|
|
func(ctx *Context, value reflect.Value, data []byte) (int, error) {
|
|
|
|
copy(data, value.Bytes())
|
|
|
|
return 16, nil
|
2024-03-30 23:57:18 -06:00
|
|
|
}, func(ctx *Context, value reflect.Value) (int, error) {
|
|
|
|
return 16, nil
|
2024-03-28 20:28:07 -06:00
|
|
|
}, func(ctx *Context, data []byte) (reflect.Value, []byte, error) {
|
|
|
|
if len(data) < 16 {
|
|
|
|
return reflect.Value{}, nil, fmt.Errorf("Not enough bytes to decode uuid.UUID(got %d, want 16)", len(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
id := new(uuid.UUID)
|
|
|
|
err := id.UnmarshalBinary(data[0:16])
|
|
|
|
if err != nil {
|
|
|
|
return reflect.Value{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.ValueOf(id).Elem(), data[16:], nil
|
|
|
|
})
|
2024-03-08 17:43:30 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register uuid.UUID: %w", err)
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
2024-03-28 20:28:07 -06:00
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[NodeType](ctx, identity, coerce[NodeType], astInt[NodeType], nil, nil, nil)
|
2024-03-21 14:13:54 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register NodeType: %w", err)
|
2024-03-21 14:13:54 -06:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[ExtType](ctx, identity, coerce[ExtType], astInt[ExtType], nil, nil, nil)
|
2024-03-08 17:43:30 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register ExtType: %w", err)
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
err = RegisterNodeInterface(ctx, "Base", map[string]graphql.Type{})
|
2024-03-21 14:22:34 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register NodeInterface Base: %w", err)
|
2024-03-21 14:22:34 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
err = RegisterNodeType(ctx, "Node", map[string]FieldMapping{})
|
2024-03-08 17:43:30 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register NodeType Node: %w", err)
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[bool](ctx, identity, coerce[bool], astBool[bool], nil, nil, nil)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register bool: %w", err)
|
2024-03-08 00:22:51 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[int](ctx, identity, coerce[int], astInt[int], nil, nil, nil)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register int: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[uint32](ctx, identity, coerce[uint32], astInt[uint32], nil, nil, nil)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register uint32: %w", err)
|
2024-03-08 00:22:51 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[uint8](ctx, identity, coerce[uint8], astInt[uint8], nil, nil, nil)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register uint8: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[time.Time](ctx, stringify, unstringify[time.Time], unstringifyAST[time.Time], nil, nil, nil)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register time.Time: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[string](ctx, identity, coerce[string], astString[string], nil, nil, nil)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register string: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-17 14:25:34 -06:00
|
|
|
err = RegisterEnum[ReqState](ctx, ReqStateStrings)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register ReqState: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
err = RegisterScalar[Tag](ctx, identity, coerce[Tag], astString[Tag], nil, nil, nil)
|
2024-03-21 14:22:34 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register Tag: %w", err)
|
2024-03-21 14:22:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Register as a GQL type with Signal as an interface
|
2024-03-21 14:13:54 -06:00
|
|
|
err = RegisterObjectNoGQL[QueuedSignal](ctx)
|
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register QueuedSignal: %w", err)
|
2024-03-21 14:13:54 -06:00
|
|
|
}
|
|
|
|
|
2024-03-21 14:22:34 -06:00
|
|
|
err = RegisterSignal[TimeoutSignal](ctx)
|
2024-03-21 14:13:54 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register TimeoutSignal: %w", err)
|
2024-03-21 14:13:54 -06:00
|
|
|
}
|
|
|
|
|
2024-03-21 14:22:34 -06:00
|
|
|
err = RegisterSignal[StatusSignal](ctx)
|
2024-03-21 14:13:54 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register StatusSignal: %w", err)
|
2024-03-21 14:13:54 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[Node](ctx)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register Node: %w", err)
|
2024-03-04 21:30:11 -07:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[LockableExt](ctx, nil)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register LockableExt extension: %w", err)
|
2024-03-08 00:22:51 -07:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[ListenerExt](ctx, nil)
|
2023-09-12 19:48:16 -06:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register ListenerExt extension: %w", err)
|
2023-09-12 19:48:16 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[GQLExt](ctx, nil)
|
2023-11-11 13:53:41 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register GQLExt extension: %w", err)
|
2023-11-11 13:53:41 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
err = RegisterNodeInterface(ctx, "Lockable", map[string]graphql.Type{
|
|
|
|
"LockableState": gqltype(ctx, reflect.TypeFor[ReqState](), ""),
|
|
|
|
"Requirements": gqltype(ctx, reflect.TypeFor[map[NodeID]ReqState](), ":Lockable"),
|
|
|
|
})
|
2023-11-05 21:18:14 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register NodeInterface Lockable: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterNodeType(ctx, "LockableNode", map[string]FieldMapping{
|
|
|
|
"LockableState": {
|
|
|
|
Extension: ExtTypeFor[LockableExt](),
|
|
|
|
Tag: "state",
|
|
|
|
},
|
|
|
|
"Requirements": {
|
|
|
|
Extension: ExtTypeFor[LockableExt](),
|
|
|
|
Tag: "requirements",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to register NodeType LockableNode: %w", err)
|
2023-11-05 21:18:14 -07:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[LockableExt](ctx)
|
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register LockableExt object: %w", err)
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterObject[ListenerExt](ctx)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register ListenerExt object: %w", err)
|
2024-03-08 00:22:51 -07:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[GQLExt](ctx)
|
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to register GQLExt object: %w", err)
|
2024-03-08 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
schema, err := BuildSchema(ctx, graphql.NewObject(graphql.ObjectConfig{
|
2024-03-04 22:11:40 -07:00
|
|
|
Name: "Query",
|
|
|
|
Fields: graphql.Fields{
|
2024-03-08 17:43:30 -07:00
|
|
|
"Self": &graphql.Field{
|
2024-03-28 20:28:07 -06:00
|
|
|
Type: ctx.Interfaces["Base"].Type,
|
2024-03-04 22:11:40 -07:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2024-03-09 22:09:40 -07:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-25 18:49:52 -06:00
|
|
|
|
2024-03-09 22:09:40 -07:00
|
|
|
return ResolveNode(ctx.Server.ID, p)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Node": &graphql.Field{
|
2024-03-28 20:28:07 -06:00
|
|
|
Type: ctx.Interfaces["Base"].Type,
|
2024-03-09 22:09:40 -07:00
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
"id": &graphql.ArgumentConfig{
|
2024-03-28 20:28:07 -06:00
|
|
|
Type: ctx.Types[reflect.TypeFor[NodeID]()].Type,
|
2024-03-09 22:09:40 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
id, err := ExtractParam[NodeID](p, "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResolveNode(id, p)
|
2024-03-04 22:11:40 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}), graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Mutation",
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
"Test": &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return "TEST", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
if err != nil {
|
2024-03-28 20:28:07 -06:00
|
|
|
return nil, fmt.Errorf("Failed to build schema: %w", err)
|
2024-03-04 22:11:40 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
ext_info := ctx.Extensions[ExtTypeFor[GQLExt]()]
|
|
|
|
ext_info.Data = schema
|
|
|
|
ctx.Extensions[ExtTypeFor[GQLExt]()] = ext_info
|
2024-03-08 14:35:23 -07:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return ctx, nil
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|