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 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 { func FindChild(event Event, id string) Event {
if id == event.ID() { if id == event.ID() {
return event return event

@ -855,7 +855,7 @@ type GQLServer struct {
extended_mutations map[string]*graphql.Field 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{ 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,
@ -864,6 +864,7 @@ func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object
extended_types: extended_types, extended_types: extended_types,
extended_queries: extended_queries, extended_queries: extended_queries,
extended_mutations: extended_mutations, extended_mutations: extended_mutations,
extended_subscriptions: extended_subscriptions,
} }
return server return server

@ -27,28 +27,67 @@ func GQLVexQueries() map[string]*graphql.Field {
return queries return queries
} }
func FindResources(event Event, resource_type reflect.Type) []Resource { func GQLVexSubscriptions() map[string]*graphql.Field {
resources := event.RequiredResources() subs := map[string]*graphql.Field{}
found := []Resource{} subs["Arena"] = GQLVexSubscriptionArena()
for _, resource := range(resources) { return subs
if reflect.TypeOf(resource) == resource_type { }
found = append(found, resource)
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()) { func GQLVexSubscribeArena(p graphql.ResolveParams)(interface{}, error) {
found = append(found, FindResources(child, resource_type)...) 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{} c := make(chan interface{})
for _, resource := range(found) { arena_id, ok := p.Args["arena_id"].(string)
m[resource.ID()] = resource 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{} resource := FindRequiredResource(owner, arena_id)
for _, resource := range(m) { if resource == nil {
ret = append(ret, resource) 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 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) resources = append(resources, gql_server)
root_event := NewEventQueue("root_event", "", []Resource{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: () => {
},
});