Added subscription to arena

graph-rework
noah metz 2023-06-16 18:27:03 -06:00
parent 6715bcbf01
commit bd9edb19ee
5 changed files with 117 additions and 17 deletions

@ -97,6 +97,47 @@ func (event * BaseEvent) Handler(signal_type string) (func(GraphSignal)(string,
return handler, exists
}
func FindResources(event Event, resource_type reflect.Type) []Resource {
resources := event.RequiredResources()
found := []Resource{}
for _, resource := range(resources) {
if reflect.TypeOf(resource) == resource_type {
found = append(found, resource)
}
}
for _, child := range(event.Children()) {
found = append(found, FindResources(child, resource_type)...)
}
m := map[string]Resource{}
for _, resource := range(found) {
m[resource.ID()] = resource
}
ret := []Resource{}
for _, resource := range(m) {
ret = append(ret, resource)
}
return ret
}
func FindRequiredResource(event Event, id string) Resource {
for _, resource := range(event.RequiredResources()) {
if resource.ID() == id {
return resource
}
}
for _, child := range(event.Children()) {
result := FindRequiredResource(child, id)
if result != nil {
return result
}
}
return nil
}
func FindChild(event Event, id string) Event {
if id == event.ID() {
return event

@ -855,7 +855,7 @@ type GQLServer struct {
extended_mutations map[string]*graphql.Field
}
func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object, extended_queries map[string]*graphql.Field, extended_mutations map[string]*graphql.Field) * GQLServer {
func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object, extended_queries map[string]*graphql.Field, extended_mutations map[string]*graphql.Field, extended_subscriptions map[string]*graphql.Field) * GQLServer {
server := &GQLServer{
BaseResource: NewBaseResource("GQL Server", "graphql server for event signals", []Resource{}),
listen: listen,
@ -864,6 +864,7 @@ func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object
extended_types: extended_types,
extended_queries: extended_queries,
extended_mutations: extended_mutations,
extended_subscriptions: extended_subscriptions,
}
return server

@ -27,28 +27,67 @@ func GQLVexQueries() map[string]*graphql.Field {
return queries
}
func FindResources(event Event, resource_type reflect.Type) []Resource {
resources := event.RequiredResources()
found := []Resource{}
for _, resource := range(resources) {
if reflect.TypeOf(resource) == resource_type {
found = append(found, resource)
func GQLVexSubscriptions() map[string]*graphql.Field {
subs := map[string]*graphql.Field{}
subs["Arena"] = GQLVexSubscriptionArena()
return subs
}
var gql_vex_subscription_arena *graphql.Field = nil
func GQLVexSubscriptionArena() *graphql.Field {
if gql_vex_subscription_arena == nil {
gql_vex_subscription_arena = &graphql.Field{
Type: GQLVexTypeArena(),
Args: graphql.FieldConfigArgument{
"arena_id": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return p.Source, nil
},
Subscribe: GQLVexSubscribeArena,
}
}
return gql_vex_subscription_arena
}
func GQLVexSubscribeArena(p graphql.ResolveParams)(interface{}, error) {
server, ok := p.Context.Value("gql_server").(*GQLServer)
if ok == false {
return nil, fmt.Errorf("Failed to get gql_Server from context and cast to GQLServer")
}
for _, child := range(event.Children()) {
found = append(found, FindResources(child, resource_type)...)
c := make(chan interface{})
arena_id, ok := p.Args["arena_id"].(string)
if ok == false {
return nil, fmt.Errorf("Failed to get arena_id arg")
}
owner, ok := server.Owner().(Event)
if ok == false {
return nil, fmt.Errorf("Failed to cast owner to event")
}
resource := FindRequiredResource(owner, arena_id)
if resource == nil {
return nil, fmt.Errorf("Failed to find resource under owner")
}
arena, ok := resource.(Arena)
if ok == false {
return nil, fmt.Errorf("Failed to cast resource to arena")
}
m := map[string]Resource{}
for _, resource := range(found) {
m[resource.ID()] = resource
sig_c := arena.UpdateChannel()
go func(c chan interface{}, sig_c chan GraphSignal, arena Arena) {
c <- arena
for {
_, ok := <- sig_c
if ok == false {
return
}
ret := []Resource{}
for _, resource := range(m) {
ret = append(ret, resource)
c <- arena
}
return ret
}(c, sig_c, arena)
return c, nil
}
var gql_vex_mutation_set_match_state *graphql.Field= nil

@ -117,7 +117,7 @@ func fake_data() (* EventManager) {
}
gql_server := NewGQLServer(":8080", GQLVexTypes(), GQLVexQueries(), GQLVexMutations())
gql_server := NewGQLServer(":8080", GQLVexTypes(), GQLVexQueries(), GQLVexMutations(), GQLVexSubscriptions())
resources = append(resources, gql_server)
root_event := NewEventQueue("root_event", "", []Resource{gql_server})

@ -46,6 +46,25 @@ client.subscribe({
},
});
client.subscribe({
query: "subscription($arena_id:String) { Arena(arena_id:$arena_id) { Owner { Name }} }",
variables: {
arena_id: "a3e8af0b-deb3-4543-b2f7-cc82d3326174"
},
},
{
next: (data) => {
console.log("ARENA_SUB")
console.log(data)
},
error: (err) => {
console.log("ARENA_SUB")
console.log(err)
},
complete: () => {
},
});