Added vex match as module for gql node

graph-rework
noah metz 2023-06-07 23:01:06 -06:00
parent a403ea82cb
commit 77eb76bc9c
3 changed files with 62 additions and 6 deletions

@ -277,14 +277,16 @@ type GQLServer struct {
abort chan error abort chan error
listen string listen string
gql_channel chan error gql_channel chan error
extended_types map[reflect.Type]*graphql.Object
} }
func NewGQLServer(listen string) * GQLServer { func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object) * GQLServer {
server := &GQLServer{ server := &GQLServer{
BaseResource: NewBaseResource("GQL Server", "graphql server for event signals", []Resource{}), BaseResource: NewBaseResource("GQL Server", "graphql server for event signals", []Resource{}),
listen: listen, listen: listen,
abort: make(chan error, 1), abort: make(chan error, 1),
gql_channel: make(chan error, 1), gql_channel: make(chan error, 1),
extended_types: extended_types,
} }
return server return server
@ -295,17 +297,19 @@ func (server * GQLServer) update(signal GraphSignal) {
server.BaseResource.update(signal) server.BaseResource.update(signal)
} }
func (server * GQLServer) Handler(extended_types map[reflect.Type]*graphql.Object) func(http.ResponseWriter, *http.Request) { func (server * GQLServer) Handler() func(http.ResponseWriter, *http.Request) {
valid_events := map[reflect.Type]*graphql.Object{} valid_events := map[reflect.Type]*graphql.Object{}
valid_events[reflect.TypeOf((*BaseEvent)(nil))] = GQLTypeBaseEvent() valid_events[reflect.TypeOf((*BaseEvent)(nil))] = GQLTypeBaseEvent()
valid_events[reflect.TypeOf((*EventQueue)(nil))] = GQLTypeEventQueue() valid_events[reflect.TypeOf((*EventQueue)(nil))] = GQLTypeEventQueue()
for go_t, gql_t := range(extended_types) { gql_types := []graphql.Type{GQLTypeBaseEvent(), GQLTypeEventQueue()}
for go_t, gql_t := range(server.extended_types) {
valid_events[go_t] = gql_t valid_events[go_t] = gql_t
gql_types = append(gql_types, gql_t)
} }
schemaConfig := graphql.SchemaConfig{ schemaConfig := graphql.SchemaConfig{
Types: []graphql.Type{GQLTypeBaseEvent(), GQLTypeEventQueue()}, Types: gql_types,
Query: graphql.NewObject(graphql.ObjectConfig{ Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query", Name: "Query",
Fields: graphql.Fields{ Fields: graphql.Fields{
@ -337,7 +341,7 @@ func (server * GQLServer) Init(abort chan error) bool {
log.Logf("gql", "GOROUTINE_START for %s", server.ID()) log.Logf("gql", "GOROUTINE_START for %s", server.ID())
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/gql", server.Handler(map[reflect.Type]*graphql.Object{})) mux.HandleFunc("/gql", server.Handler())
mux.HandleFunc("/", GraphiQLHandler()) mux.HandleFunc("/", GraphiQLHandler())
srv := &http.Server{ srv := &http.Server{

@ -0,0 +1,52 @@
package main
import (
"github.com/graphql-go/graphql"
"reflect"
)
func GQLVexTypes() map[reflect.Type]*graphql.Object {
types := map[reflect.Type]*graphql.Object{}
types[reflect.TypeOf((*Match)(nil))] = GQLVexTypeMatch()
return types
}
var gql_vex_type_match * graphql.Object = nil
func GQLVexTypeMatch() * graphql.Object {
if gql_vex_type_match == nil {
gql_vex_type_match = graphql.NewObject(graphql.ObjectConfig{
Name: "Match",
Interfaces: []*graphql.Interface{
GQLInterfaceEvent(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*Match)
return ok
},
Fields: graphql.Fields{},
})
gql_vex_type_match.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventID,
})
gql_vex_type_match.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventName,
})
gql_vex_type_match.AddFieldConfig("Description", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventDescription,
})
gql_vex_type_match.AddFieldConfig("Children", &graphql.Field{
Type: GQLListEvent(),
Resolve: GQLEventChildren,
})
}
return gql_vex_type_match
}

@ -116,7 +116,7 @@ func fake_data() (* EventManager, []Arena, []Arena) {
} }
gql_server := NewGQLServer(":8080") gql_server := NewGQLServer(":8080", GQLVexTypes())
resources = append(resources, gql_server) resources = append(resources, gql_server)
root_event := NewEventQueue("root_event", "", []Resource{gql_server}) root_event := NewEventQueue("root_event", "", []Resource{gql_server})
event_manager := NewEventManager(root_event, resources) event_manager := NewEventManager(root_event, resources)