2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GQLSubscribeSignal(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:52:40 -06:00
|
|
|
return GQLSubscribeFn(p, false, func(ctx *Context, server *Node, ext *GQLExt, signal Signal, p graphql.ResolveParams)(interface{}, error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
return signal, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSubscribeSelf(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-26 15:52:40 -06:00
|
|
|
return GQLSubscribeFn(p, true, func(ctx *Context, server *Node, ext *GQLExt, signal Signal, p graphql.ResolveParams)(interface{}, error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
return server, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:52:40 -06:00
|
|
|
func GQLSubscribeFn(p graphql.ResolveParams, send_nil bool, fn func(*Context, *Node, *GQLExt, Signal, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
|
|
|
|
_, ctx, err := PrepResolve(p)
|
2023-07-21 19:16:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan interface{})
|
2023-07-26 15:52:40 -06:00
|
|
|
go func(c chan interface{}, ext *GQLExt, server *Node) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx.Context.Log.Logf("gqlws", "GQL_SUBSCRIBE_THREAD_START")
|
2023-07-26 15:52:40 -06:00
|
|
|
sig_c := ext.NewSubscriptionChannel(1)
|
2023-07-21 15:16:35 -06:00
|
|
|
if send_nil == true {
|
|
|
|
sig_c <- nil
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
val, ok := <- sig_c
|
|
|
|
if ok == false {
|
|
|
|
return
|
|
|
|
}
|
2023-07-26 15:52:40 -06:00
|
|
|
ret, err := fn(ctx.Context, server, ext, val, p)
|
2023-07-21 15:16:35 -06:00
|
|
|
if err != nil {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx.Context.Log.Logf("gqlws", "type convertor error %s", err)
|
2023-07-21 15:16:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c <- ret
|
|
|
|
}
|
2023-07-26 15:52:40 -06:00
|
|
|
}(c, ctx.Ext, ctx.Server)
|
2023-07-21 15:16:35 -06:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:49:19 -06:00
|
|
|
var GQLSubscriptionSelf = NewField(func()*graphql.Field{
|
|
|
|
gql_subscription_self := &graphql.Field{
|
2023-07-26 20:26:41 -06:00
|
|
|
Type: GQLInterfaceThread.Default,
|
2023-07-21 17:49:19 -06:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return p.Source, nil
|
|
|
|
},
|
|
|
|
Subscribe: GQLSubscribeSelf,
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return gql_subscription_self
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
2023-07-21 15:16:35 -06:00
|
|
|
|
2023-07-21 17:49:19 -06:00
|
|
|
var GQLSubscriptionUpdate = NewField(func()*graphql.Field{
|
|
|
|
gql_subscription_update := &graphql.Field{
|
|
|
|
Type: GQLTypeSignal.Type,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return p.Source, nil
|
|
|
|
},
|
|
|
|
Subscribe: GQLSubscribeSignal,
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
return gql_subscription_update
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
2023-07-21 15:16:35 -06:00
|
|
|
|