From d3f435351cb652b60301ea7ff6b1b1e4fe25a4e1 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 21 Jul 2023 13:59:52 -0600 Subject: [PATCH] Moved StartChild mutation to graphvent --- context.go | 1 + gql_graph.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ signal.go | 14 ++++++++++ 3 files changed, 90 insertions(+) diff --git a/context.go b/context.go index f515342..6008111 100644 --- a/context.go +++ b/context.go @@ -209,6 +209,7 @@ func NewContext(db * badger.DB, log Logger) * Context { ctx.GQL.Subscription.AddFieldConfig("Self", GQLSubscriptionSelf()) ctx.GQL.Mutation.AddFieldConfig("SendUpdate", GQLMutationSendUpdate()) + ctx.GQL.Mutation.AddFieldConfig("StartChild", GQLMutationStartChild()) err = ctx.RebuildSchema() if err != nil { diff --git a/gql_graph.go b/gql_graph.go index 1284d08..1703be8 100644 --- a/gql_graph.go +++ b/gql_graph.go @@ -950,6 +950,81 @@ func GQLMutationSendUpdate() *graphql.Field { return gql_mutation_send_update } +var gql_mutation_start_child *graphql.Field = nil +func GQLMutationStartChild() *graphql.Field { + if gql_mutation_start_child == nil { + gql_mutation_start_child = &graphql.Field{ + Type: GQLTypeSignal(), + Args: graphql.FieldConfigArgument{ + "parent_id": &graphql.ArgumentConfig{ + Type: graphql.String, + }, + "child_id": &graphql.ArgumentConfig{ + Type: graphql.String, + }, + "action": &graphql.ArgumentConfig{ + Type: graphql.String, + DefaultValue: "start", + }, + }, + Resolve: func(p graphql.ResolveParams) (interface{}, error) { + server, ok := p.Context.Value("gql_server").(*GQLThread) + if ok == false { + return nil, fmt.Errorf("Failed to cast context gql_server to GQLServer: %+v", p.Context.Value("gql_server")) + } + + ctx, ok := p.Context.Value("graph_context").(*Context) + if ok == false { + return nil, fmt.Errorf("Failed to cast context graph_context to Context: %+v", p.Context.Value("graph_context")) + } + + parent_str, ok := p.Args["parent_id"].(string) + if ok == false { + return nil, fmt.Errorf("Failed to cast arg parent_id to string: %+v", p.Args["parent_id"]) + } + parent_id, err := ParseID(parent_str) + if err != nil { + return nil, err + } + + child_str, ok := p.Args["child_id"].(string) + if ok == false { + return nil, fmt.Errorf("Failed to cast arg child_id to string: %+v", p.Args["child_id"]) + } + child_id, err := ParseID(child_str) + if err != nil { + return nil, err + } + + action, ok := p.Args["action"].(string) + if ok == false { + return nil, fmt.Errorf("Failed to cast arg action to string: %+v", p.Args["action"]) + } + + var signal GraphSignal + err = UseStates(ctx, []Node{server}, func(nodes NodeMap) (error){ + node := FindChild(ctx, server, parent_id, nodes) + if node == nil { + return fmt.Errorf("Failed to find ID: %s as child of server thread", parent_id) + } + return UseMoreStates(ctx, []Node{node}, nodes, func(NodeMap) error { + signal = NewStartChildSignal(server, child_id, action) + return node.Signal(ctx, signal, nodes) + }) + }) + if err != nil { + return nil, err + } + + // TODO: wait for the result of the signal to send back instead of just the signal + return signal, nil + }, + } + } + + return gql_mutation_start_child +} + func GQLPrepResolve(p graphql.ResolveParams) (*Context, *GQLThread, *User, error) { context, ok := p.Context.Value("graph_context").(*Context) if ok == false { diff --git a/signal.go b/signal.go index 811121c..3358ad0 100644 --- a/signal.go +++ b/signal.go @@ -80,3 +80,17 @@ func AbortSignal(source Node) BaseSignal { func CancelSignal(source Node) BaseSignal { return NewBaseSignal(source, "cancel", Down) } + +type StartChildSignal struct { + BaseSignal + ChildID NodeID `json:"child_id"` + Action string `json:"action"` +} + +func NewStartChildSignal(source Node, child_id NodeID, action string) StartChildSignal { + return StartChildSignal{ + BaseSignal: NewBaseSignal(source, "start_child", Direct), + ChildID: child_id, + Action: action, + } +}