2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
var GQLMutationAbort = NewField(func()*graphql.Field {
|
|
|
|
gql_mutation_abort := &graphql.Field{
|
2023-07-21 17:49:19 -06:00
|
|
|
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
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:52:40 -06:00
|
|
|
_, ctx, err := PrepResolve(p)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := ExtractID(p, "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:52:40 -06:00
|
|
|
var node *Node = nil
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewReadContext(ctx.Context)
|
2023-07-26 15:52:40 -06:00
|
|
|
err = UseStates(context, ctx.User, NewACLMap(
|
|
|
|
NewACLInfo(ctx.Server, []string{"children"}),
|
2023-07-23 17:57:47 -06:00
|
|
|
), func(context *StateContext) (error){
|
2023-07-26 15:52:40 -06:00
|
|
|
node, err = FindChild(context, ctx.User, ctx.Server, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-26 15:52:40 -06:00
|
|
|
return SendSignal(context, node, ctx.User, AbortSignal)
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return AbortSignal, nil
|
2023-07-21 17:49:19 -06:00
|
|
|
},
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return gql_mutation_abort
|
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-26 15:52:40 -06:00
|
|
|
_, ctx, err := PrepResolve(p)
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:52:40 -06:00
|
|
|
var signal Signal
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx.Context)
|
2023-07-26 15:52:40 -06:00
|
|
|
err = UseStates(context, ctx.User, NewACLMap(
|
|
|
|
NewACLInfo(ctx.Server, []string{"children"}),
|
2023-07-23 17:57:47 -06:00
|
|
|
), func(context *StateContext) error {
|
2023-07-26 15:52:40 -06:00
|
|
|
parent, err := FindChild(context, ctx.User, ctx.Server, parent_id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
if parent == nil {
|
2023-07-26 15:52:40 -06:00
|
|
|
return fmt.Errorf("%s is not a child of %s", parent_id, ctx.Server.ID)
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
|
2023-07-23 21:14:15 -06:00
|
|
|
signal = NewStartChildSignal(child_id, action)
|
2023-07-26 15:52:40 -06:00
|
|
|
return SendSignal(context, ctx.User, parent, signal)
|
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
|
|
|
})
|
|
|
|
|