From bd9edb19ee9b6f3d1185aad8748c402e494d3559 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 16 Jun 2023 18:27:03 -0600 Subject: [PATCH] Added subscription to arena --- event.go | 41 ++++++++++++++++++ gql.go | 3 +- gql_vex.go | 69 ++++++++++++++++++++++++------- main.go | 2 +- test-site/src/routes/+page.svelte | 19 +++++++++ 5 files changed, 117 insertions(+), 17 deletions(-) diff --git a/event.go b/event.go index 2f19d66..394985b 100644 --- a/event.go +++ b/event.go @@ -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 diff --git a/gql.go b/gql.go index 9e121c3..250c59b 100644 --- a/gql.go +++ b/gql.go @@ -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 diff --git a/gql_vex.go b/gql_vex.go index 828c308..6bfd63b 100644 --- a/gql_vex.go +++ b/gql_vex.go @@ -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 +} - for _, child := range(event.Children()) { - found = append(found, FindResources(child, resource_type)...) +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") } - m := map[string]Resource{} - for _, resource := range(found) { - m[resource.ID()] = resource + 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") } - ret := []Resource{} - for _, resource := range(m) { - ret = append(ret, resource) + resource := FindRequiredResource(owner, arena_id) + if resource == nil { + return nil, fmt.Errorf("Failed to find resource under owner") } - return ret + arena, ok := resource.(Arena) + if ok == false { + return nil, fmt.Errorf("Failed to cast resource to arena") + } + + 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 + } + c <- arena + } + }(c, sig_c, arena) + return c, nil } var gql_vex_mutation_set_match_state *graphql.Field= nil diff --git a/main.go b/main.go index 43ab945..bdcdc43 100644 --- a/main.go +++ b/main.go @@ -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}) diff --git a/test-site/src/routes/+page.svelte b/test-site/src/routes/+page.svelte index 002b8e1..39eb18e 100644 --- a/test-site/src/routes/+page.svelte +++ b/test-site/src/routes/+page.svelte @@ -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: () => { + }, +}); +