2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
2023-07-21 17:49:19 -06:00
|
|
|
var GQLMutationSendUpdate = NewField(func()*graphql.Field {
|
|
|
|
gql_mutation_send_update := &graphql.Field{
|
|
|
|
Type: GQLTypeSignal.Type,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
"id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
2023-07-21 15:16:35 -06:00
|
|
|
},
|
2023-07-21 17:49:19 -06:00
|
|
|
"signal": &graphql.ArgumentConfig{
|
|
|
|
Type: GQLTypeSignalInput.Type,
|
2023-07-21 15:16:35 -06:00
|
|
|
},
|
2023-07-21 17:49:19 -06:00
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:51:42 -06:00
|
|
|
err = ctx.Server.Allowed("signal", "self", ctx.User)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_map, err := ExtractParam[map[string]interface{}](p, "signal")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var signal GraphSignal = nil
|
|
|
|
if signal_map["Direction"] == "up" {
|
2023-07-21 18:51:42 -06:00
|
|
|
signal = NewSignal(ctx.Server, signal_map["Type"].(string))
|
2023-07-21 17:49:19 -06:00
|
|
|
} else if signal_map["Direction"] == "down" {
|
2023-07-21 18:51:42 -06:00
|
|
|
signal = NewDownSignal(ctx.Server, signal_map["Type"].(string))
|
2023-07-21 17:49:19 -06:00
|
|
|
} else if signal_map["Direction"] == "direct" {
|
2023-07-21 18:51:42 -06:00
|
|
|
signal = NewDirectSignal(ctx.Server, signal_map["Type"].(string))
|
2023-07-21 17:49:19 -06:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Bad direction: %d", signal_map["Direction"])
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := ExtractID(p, "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var node Node = nil
|
2023-07-21 18:51:42 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{ctx.Server}, func(nodes NodeMap) (error){
|
|
|
|
node = FindChild(ctx.Context, ctx.Server, id, nodes)
|
2023-07-21 17:49:19 -06:00
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("Failed to find ID: %s as child of server thread", id)
|
|
|
|
}
|
2023-07-21 18:51:42 -06:00
|
|
|
node.Signal(ctx.Context, signal, nodes)
|
2023-07-21 17:49:19 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return signal, nil
|
|
|
|
},
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return gql_mutation_send_update
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
var GQLMutationStartChild = NewField(func()*graphql.Field{
|
|
|
|
gql_mutation_start_child := &graphql.Field{
|
|
|
|
Type: GQLTypeSignal.Type,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
"parent_id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
2023-07-21 15:16:35 -06:00
|
|
|
},
|
2023-07-21 17:49:19 -06:00
|
|
|
"child_id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
"action": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
DefaultValue: "start",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:51:42 -06:00
|
|
|
err = ctx.Server.Allowed("start_child", "self", ctx.User)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_id, err := ExtractID(p, "parent_id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
child_id, err := ExtractID(p, "child_id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
action, err := ExtractParam[string](p, "action")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var signal GraphSignal
|
2023-07-21 18:51:42 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{ctx.Server}, func(nodes NodeMap) (error){
|
|
|
|
node := FindChild(ctx.Context, ctx.Server, parent_id, nodes)
|
2023-07-21 17:49:19 -06:00
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("Failed to find ID: %s as child of server thread", parent_id)
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
2023-07-21 18:51:42 -06:00
|
|
|
return UseMoreStates(ctx.Context, []Node{node}, nodes, func(NodeMap) error {
|
|
|
|
signal = NewStartChildSignal(ctx.Server, child_id, action)
|
|
|
|
return node.Signal(ctx.Context, signal, nodes)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
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
|
|
|
|
},
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return gql_mutation_start_child
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
|