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-08 17:43:30 -07:00
|
|
|
"slices"
|
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-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"
|
|
|
|
|
|
|
|
badger "github.com/dgraph-io/badger/v3"
|
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-08 00:22:51 -07:00
|
|
|
type SerializeFn func(ctx *Context, value reflect.Value) ([]byte, error)
|
|
|
|
type DeserializeFn func(ctx *Context, data []byte) (reflect.Value, []byte, error)
|
|
|
|
|
|
|
|
type FieldInfo struct {
|
|
|
|
Index []int
|
2024-03-08 17:43:30 -07:00
|
|
|
Tag string
|
|
|
|
NodeTag string
|
2024-03-08 00:22:51 -07:00
|
|
|
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
|
|
|
|
|
|
|
Fields map[FieldTag]FieldInfo
|
|
|
|
PostDeserializeIndex int
|
|
|
|
|
|
|
|
Serialize SerializeFn
|
|
|
|
Deserialize DeserializeFn
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
2024-03-03 15:45:45 -07:00
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
type ExtensionInfo struct {
|
2024-03-08 00:22:51 -07:00
|
|
|
ExtType
|
2024-03-08 17:43:30 -07:00
|
|
|
Fields map[string]FieldInfo
|
2023-08-31 19:50:32 -06:00
|
|
|
Data interface{}
|
|
|
|
}
|
2023-08-01 20:55:15 -06:00
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
type FieldIndex struct {
|
2024-03-08 17:43:30 -07:00
|
|
|
FieldTag FieldTag
|
2024-03-03 15:45:45 -07:00
|
|
|
Extension ExtType
|
|
|
|
}
|
|
|
|
|
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-08 17:43:30 -07:00
|
|
|
Interface *graphql.Interface
|
2023-08-31 19:50:32 -06:00
|
|
|
Extensions []ExtType
|
2023-08-01 20:55:15 -06:00
|
|
|
}
|
2023-07-29 00:28:44 -06:00
|
|
|
|
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
|
2023-07-09 14:30:30 -06:00
|
|
|
DB * badger.DB
|
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-08 00:22:51 -07:00
|
|
|
TypeMap map[SerializedType]*TypeInfo
|
|
|
|
TypeTypes map[reflect.Type]*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-08 00:22:51 -07:00
|
|
|
Extensions map[ExtType]*ExtensionInfo
|
|
|
|
ExtensionTypes map[reflect.Type]*ExtensionInfo
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Map between database type hashes and the registered info
|
2024-03-08 00:22:51 -07:00
|
|
|
Nodes map[NodeType]*NodeInfo
|
|
|
|
NodeTypes map[string]*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
|
2023-08-31 19:50:32 -06:00
|
|
|
nodeMapLock sync.RWMutex
|
|
|
|
nodeMap map[NodeID]*Node
|
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]() {
|
|
|
|
node_info, mapped := ctx.NodeTypes[node_type]
|
|
|
|
if mapped == false {
|
|
|
|
return nil, fmt.Errorf("Cannot get GQL type for unregistered Node Type \"%s\"", node_type)
|
|
|
|
} else {
|
|
|
|
return node_info.Interface, nil
|
|
|
|
}
|
2024-03-04 21:30:11 -07:00
|
|
|
} else {
|
2024-03-08 17:43:30 -07:00
|
|
|
info, mapped := ctx.TypeTypes[t]
|
|
|
|
if mapped {
|
|
|
|
return info.Type, nil
|
|
|
|
} else {
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Array:
|
|
|
|
info, mapped := ctx.TypeTypes[t.Elem()]
|
|
|
|
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:
|
|
|
|
info, mapped := ctx.TypeTypes[t.Elem()]
|
|
|
|
if mapped {
|
|
|
|
return graphql.NewList(info.Type), nil
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
info, exists := ctx.TypeTypes[t]
|
|
|
|
if exists {
|
|
|
|
return info.Type, nil
|
|
|
|
} else {
|
|
|
|
err := RegisterMap(ctx, t, node_type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
map_type := ctx.TypeTypes[t].Type
|
|
|
|
ctx.Log.Logf("gql", "Getting type for %s: %s", t, map_type)
|
|
|
|
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-08 17:43:30 -07:00
|
|
|
func RegisterMap(ctx *Context, reflect_type reflect.Type, node_type string) error {
|
|
|
|
ctx.Log.Logf("gql", "Registering map %s with node_type %s", reflect_type, node_type)
|
|
|
|
node_types := strings.Split(node_type, ":")
|
|
|
|
|
|
|
|
if len(node_types) != 2 {
|
|
|
|
return fmt.Errorf("Invalid node tag for map type %s: \"%s\"", reflect_type, node_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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, "]", "_")
|
|
|
|
ctx.Log.Logf("gql", "Registering %s with gql name %s", reflect_type, 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) {
|
|
|
|
return nil, fmt.Errorf("NOT_IMPLEMENTED")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Value": &graphql.Field{
|
|
|
|
Type: val_type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return nil, fmt.Errorf("NOT_IMPLEMENTED")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
ctx.Log.Logf("gql", "Registering new map with pair type %+v", gql_pair)
|
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)
|
|
|
|
ctx.TypeMap[serialized_type] = &TypeInfo{
|
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
2024-03-04 21:30:11 -07:00
|
|
|
Type: gql_map,
|
|
|
|
}
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.TypeTypes[reflect_type] = ctx.TypeMap[serialized_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-04 22:11:40 -07:00
|
|
|
for _, info := range(ctx.TypeMap) {
|
2024-03-08 17:43:30 -07:00
|
|
|
ctx.Log.Logf("gql", "Adding type %+v", info.Type)
|
2024-03-04 22:11:40 -07:00
|
|
|
types = append(types, info.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range(ctx.Nodes) {
|
2024-03-08 17:43:30 -07:00
|
|
|
ctx.Log.Logf("gql", "Adding node type object %+v", info.Type)
|
2024-03-08 00:22:51 -07:00
|
|
|
types = append(types, info.Type)
|
2024-03-08 17:43:30 -07:00
|
|
|
ctx.Log.Logf("gql", "Adding node type interface %+v", info.Interface)
|
|
|
|
types = append(types, info.Interface)
|
2024-03-04 22:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
subscription := graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Subscription",
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
"Test": &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return "TEST", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-03-04 17:30:42 -07:00
|
|
|
})
|
2023-10-29 18:26:14 -06:00
|
|
|
|
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-03 15:45:45 -07:00
|
|
|
ext_type := ExtType(SerializedTypeFor[E]())
|
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-08 17:43:30 -07:00
|
|
|
fields := map[string]FieldInfo{}
|
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")
|
|
|
|
if tagged_gv {
|
2024-03-08 17:43:30 -07:00
|
|
|
fields[gv_tag] = FieldInfo{
|
|
|
|
Index: field.Index,
|
|
|
|
Tag: gv_tag,
|
|
|
|
NodeTag: field.Tag.Get("node"),
|
|
|
|
Type: field.Type,
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("serialize_types", "Registered ExtType: %+v - %+v", reflect_type, ext_type)
|
2023-09-12 19:00:48 -06:00
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.Extensions[ext_type] = &ExtensionInfo{
|
|
|
|
ExtType: ext_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
|
|
|
}
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.ExtensionTypes[reflect_type] = ctx.Extensions[ext_type]
|
2023-08-31 19:50:32 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
func RegisterNodeType(ctx *Context, name string, extensions []ExtType) error {
|
|
|
|
ctx.Log.Logf("gql", "Registering NodeType %s with extensions %+v", name, extensions)
|
|
|
|
node_type := NodeTypeFor(extensions)
|
2024-03-04 17:30:42 -07:00
|
|
|
_, exists := ctx.Nodes[node_type]
|
2023-09-02 17:30:52 -06:00
|
|
|
if exists == true {
|
2024-03-04 17:30:42 -07:00
|
|
|
return fmt.Errorf("Cannot register node type %+v, type already exists in context", node_type)
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
ext_found := map[ExtType]bool{}
|
|
|
|
for _, extension := range(extensions) {
|
|
|
|
_, in_ctx := ctx.Extensions[extension]
|
|
|
|
if in_ctx == false {
|
2024-03-08 17:43:30 -07:00
|
|
|
return fmt.Errorf("Cannot register node type %+v, required extension %+v not in context", name, extension)
|
2024-03-04 17:30:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_, duplicate := ext_found[extension]
|
|
|
|
if duplicate == true {
|
|
|
|
return fmt.Errorf("Duplicate extension %+v found in extension list", extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_found[extension] = true
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
2024-03-08 00:22:51 -07:00
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
gql_interface := graphql.NewInterface(graphql.InterfaceConfig{
|
2024-03-08 00:22:51 -07:00
|
|
|
Name: name,
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
"ID": &graphql.Field{
|
2024-03-08 17:43:30 -07:00
|
|
|
Type: ctx.TypeTypes[reflect.TypeFor[NodeID]()].Type,
|
2024-03-08 00:22:51 -07:00
|
|
|
},
|
|
|
|
"Type": &graphql.Field{
|
2024-03-08 17:43:30 -07:00
|
|
|
Type: ctx.TypeTypes[reflect.TypeFor[NodeType]()].Type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node_info, exists := ctx.Context.Nodes[val.NodeType]
|
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ext_type := range(extensions) {
|
|
|
|
if slices.Contains(node_info.Extensions, ext_type) == false {
|
|
|
|
// node_info does not contain required extension, so this cannot be a type of this interface
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node_info.Type
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
gql := graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: name + "Node",
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
"ID": &graphql.Field{
|
|
|
|
Type: ctx.TypeTypes[reflect.TypeFor[NodeID]()].Type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
source, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("GQL Node value is not NodeResult")
|
|
|
|
}
|
|
|
|
return source.NodeID, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Type": &graphql.Field{
|
|
|
|
Type: ctx.TypeTypes[reflect.TypeFor[NodeType]()].Type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
source, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("GQL Node value is not NodeResult")
|
|
|
|
}
|
|
|
|
return source.NodeType, nil
|
|
|
|
},
|
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-08 00:22:51 -07:00
|
|
|
ctx.Nodes[node_type] = &NodeInfo{
|
|
|
|
NodeType: node_type,
|
2024-03-08 17:43:30 -07:00
|
|
|
Interface: gql_interface,
|
2024-03-08 00:22:51 -07:00
|
|
|
Type: gql,
|
2024-03-04 17:30:42 -07:00
|
|
|
Extensions: extensions,
|
|
|
|
}
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.NodeTypes[name] = ctx.Nodes[node_type]
|
2023-10-29 18:26:14 -06:00
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
for _, ext_type := range(extensions) {
|
|
|
|
ext_info, ext_found := ctx.Extensions[ext_type]
|
|
|
|
if ext_found == false {
|
|
|
|
return fmt.Errorf("Extension %s not found", ext_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
for field_name, field_info := range(ext_info.Fields) {
|
|
|
|
gql_type, err := ctx.GQLType(field_info.Type, field_info.NodeTag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("gql", "Adding field %s[%+v] to %s with gql type %+v", field_name, field_info, name, gql_type)
|
|
|
|
|
|
|
|
gql_interface.AddFieldConfig(field_name, &graphql.Field{
|
|
|
|
Type: gql_type,
|
|
|
|
})
|
|
|
|
|
|
|
|
gql.AddFieldConfig(field_name, &graphql.Field{
|
|
|
|
Type: gql_type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return nil, fmt.Errorf("NOT_IMPLEMENTED: TODO")
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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]()
|
|
|
|
|
|
|
|
_, exists := ctx.TypeTypes[reflect_type]
|
|
|
|
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(), ".", "_")
|
|
|
|
ctx.Log.Logf("gql", "Registering %s with gql name %s", reflect_type, gql_name)
|
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
|
|
|
|
|
|
|
field_infos := map[FieldTag]FieldInfo{}
|
|
|
|
|
|
|
|
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-08 00:22:51 -07:00
|
|
|
field_infos[GetFieldTag(gv_tag)] = FieldInfo{
|
|
|
|
Type: field.Type,
|
|
|
|
Index: field.Index,
|
2024-03-08 17:43:30 -07:00
|
|
|
NodeTag: node_tag,
|
|
|
|
Tag: gv_tag,
|
2024-03-08 00:22:51 -07: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-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)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := reflect.ValueOf(val).FieldByIndexErr(field.Index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.Interface(), nil
|
|
|
|
},
|
|
|
|
})
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.TypeMap[serialized_type] = &TypeInfo{
|
|
|
|
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-08 00:22:51 -07:00
|
|
|
ctx.TypeTypes[reflect_type] = ctx.TypeMap[serialized_type]
|
2023-09-11 21:47:53 -06:00
|
|
|
|
2024-03-04 17:30:42 -07: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
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp E
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterScalar[S any](ctx *Context, to_json func(interface{})interface{}, from_json func(interface{})interface{}, from_ast func(ast.Value)interface{}) error {
|
|
|
|
reflect_type := reflect.TypeFor[S]()
|
|
|
|
serialized_type := SerializedTypeFor[S]()
|
2024-03-04 17:30:42 -07:00
|
|
|
|
|
|
|
_, exists := ctx.TypeTypes[reflect_type]
|
|
|
|
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(), ".", "_")
|
|
|
|
ctx.Log.Logf("gql", "Registering %s with gql name %s", reflect_type, gql_name)
|
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-08 00:22:51 -07:00
|
|
|
ctx.TypeMap[serialized_type] = &TypeInfo{
|
|
|
|
Serialized: serialized_type,
|
|
|
|
Reflect: reflect_type,
|
2024-03-04 17:30:42 -07:00
|
|
|
Type: gql,
|
|
|
|
}
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.TypeTypes[reflect_type] = ctx.TypeMap[serialized_type]
|
2023-09-11 21:47:53 -06:00
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-28 12:46:06 -06:00
|
|
|
func (ctx *Context) AddNode(id NodeID, node *Node) {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.nodeMapLock.Lock()
|
|
|
|
ctx.nodeMap[id] = node
|
|
|
|
ctx.nodeMapLock.Unlock()
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Node(id NodeID) (*Node, bool) {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.nodeMapLock.RLock()
|
|
|
|
node, exists := ctx.nodeMap[id]
|
|
|
|
ctx.nodeMapLock.RUnlock()
|
2023-07-28 12:46:06 -06:00
|
|
|
return node, exists
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ctx *Context) Delete(id NodeID) error {
|
|
|
|
err := ctx.Unload(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: also delete any associated data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Unload(id NodeID) error {
|
2023-10-06 20:04:53 -06:00
|
|
|
ctx.nodeMapLock.Lock()
|
|
|
|
defer ctx.nodeMapLock.Unlock()
|
|
|
|
node, exists := ctx.nodeMap[id]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("%s is not a node in ctx", id)
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
err := node.Unload(ctx)
|
2023-10-06 20:04:53 -06:00
|
|
|
delete(ctx.nodeMap, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ctx *Context) Stop() {
|
2023-10-06 20:04:53 -06:00
|
|
|
ctx.nodeMapLock.Lock()
|
|
|
|
for id, node := range(ctx.nodeMap) {
|
2024-03-03 16:37:03 -07:00
|
|
|
node.Unload(ctx)
|
2023-10-06 20:04:53 -06:00
|
|
|
delete(ctx.nodeMap, id)
|
|
|
|
}
|
|
|
|
ctx.nodeMapLock.Unlock()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
target, err = LoadNode(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-04 17:30:42 -07:00
|
|
|
func (ctx *Context) Send(node *Node, messages []SendMsg) error {
|
2023-08-07 20:26:02 -06:00
|
|
|
for _, msg := range(messages) {
|
2023-11-05 21:18:14 -07:00
|
|
|
ctx.Log.Logf("signal", "Sending %s -> %+v", msg.Dest, msg)
|
2023-08-15 18:23:06 -06:00
|
|
|
if msg.Dest == ZeroID {
|
|
|
|
panic("Can't send to null ID")
|
|
|
|
}
|
2023-08-31 19:50:32 -06:00
|
|
|
target, err := ctx.getNode(msg.Dest)
|
2023-08-07 20:26:02 -06:00
|
|
|
if err == nil {
|
|
|
|
select {
|
2024-03-04 17:30:42 -07:00
|
|
|
case target.MsgChan <- RecvMsg{node.ID, msg.Signal}:
|
2023-08-08 14:00:17 -06:00
|
|
|
ctx.Log.Logf("signal", "Sent %s -> %+v", target.ID, msg)
|
2023-08-07 20:26:02 -06:00
|
|
|
default:
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
stack_str := string(buf[:n])
|
2023-08-08 14:00:17 -06:00
|
|
|
return fmt.Errorf("SIGNAL_OVERFLOW: %s - %s", msg.Dest, 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
|
|
|
}
|
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
// Create a new Context with the base library content added
|
|
|
|
func NewContext(db * badger.DB, log Logger) (*Context, error) {
|
|
|
|
ctx := &Context{
|
|
|
|
DB: db,
|
|
|
|
Log: log,
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
TypeMap: map[SerializedType]*TypeInfo{},
|
|
|
|
TypeTypes: map[reflect.Type]*TypeInfo{},
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
Extensions: map[ExtType]*ExtensionInfo{},
|
|
|
|
ExtensionTypes: map[reflect.Type]*ExtensionInfo{},
|
2024-03-04 17:30:42 -07:00
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
Nodes: map[NodeType]*NodeInfo{},
|
|
|
|
NodeTypes: map[string]*NodeInfo{},
|
2024-03-03 15:45:45 -07:00
|
|
|
|
|
|
|
nodeMap: map[NodeID]*Node{},
|
2023-09-02 17:30:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2023-09-12 16:56:01 -06:00
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterScalar[NodeID](ctx, stringify, unstringify[NodeID], unstringifyAST[NodeID])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[NodeType](ctx, identity, coerce[NodeType], astInt[NodeType])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterNodeType(ctx, "Base", []ExtType{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
err = RegisterScalar[bool](ctx, identity, coerce[bool], astBool[bool])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
err = RegisterScalar[int](ctx, identity, coerce[int], astInt[int])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:22:51 -07:00
|
|
|
err = RegisterScalar[uint32](ctx, identity, coerce[uint32], astInt[uint32])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:30:11 -07:00
|
|
|
err = RegisterScalar[uint8](ctx, identity, coerce[uint8], astInt[uint8])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[time.Time](ctx, stringify, unstringify[time.Time], unstringifyAST[time.Time])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[string](ctx, identity, coerce[string], astString[string])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[EventState](ctx, identity, coerce[EventState], astString[EventState])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[ReqState](ctx, identity, coerce[ReqState], astInt[ReqState])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterScalar[uuid.UUID](ctx, stringify, unstringify[uuid.UUID], unstringifyAST[uuid.UUID])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterScalar[WaitReason](ctx, identity, coerce[WaitReason], astString[WaitReason])
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[Node](ctx)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[WaitInfo](ctx)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[LockableExt](ctx, nil)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[EventExt](ctx, nil)
|
2024-03-04 21:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[ListenerExt](ctx, nil)
|
2023-09-12 19:48:16 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterExtension[GQLExt](ctx, nil)
|
2023-11-11 13:53:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterNodeType(ctx, "Lockable", []ExtType{ExtTypeFor[LockableExt]()})
|
2023-11-05 21:18:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[LockableExt](ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RegisterObject[EventExt](ctx)
|
2023-10-13 13:45:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[ListenerExt](ctx)
|
2024-03-08 00:22:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:43:30 -07:00
|
|
|
err = RegisterObject[GQLExt](ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Type: ctx.NodeTypes["Base"].Type,
|
2024-03-04 22:11:40 -07:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2024-03-08 17:43:30 -07:00
|
|
|
// TODO: Send read request and get response instead of hard-coding
|
|
|
|
return NodeResult{
|
|
|
|
NodeID: RandID(),
|
|
|
|
NodeType: NodeTypeFor([]ExtType{}),
|
|
|
|
}, nil
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
ctx.ExtensionTypes[reflect.TypeFor[GQLExt]()].Data = schema
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return ctx, nil
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|