2023-07-21 18:06:53 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewField(init func()*graphql.Field) *graphql.Field {
|
|
|
|
return init()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Singleton[K graphql.Type] struct {
|
|
|
|
Type K
|
|
|
|
List *graphql.List
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSingleton[K graphql.Type](init func() K, post_init func(K, *graphql.List)) *Singleton[K] {
|
|
|
|
val := init()
|
|
|
|
list := graphql.NewList(val)
|
|
|
|
if post_init != nil {
|
|
|
|
post_init(val, list)
|
|
|
|
}
|
|
|
|
return &Singleton[K]{
|
|
|
|
Type: val,
|
|
|
|
List: list,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:27:52 -06:00
|
|
|
func NodeInterfaceDefaultIsType(extensions []ExtType) func(graphql.IsTypeOfParams) bool {
|
2023-07-26 20:26:41 -06:00
|
|
|
return func(p graphql.IsTypeOfParams) bool {
|
2023-07-29 16:00:01 -06:00
|
|
|
node, ok := p.Value.(NodeResult)
|
2023-07-26 20:26:41 -06:00
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
for _, ext := range(extensions) {
|
|
|
|
_, has := node.Result.Extensions[ext]
|
|
|
|
if has == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2023-07-26 20:26:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:27:52 -06:00
|
|
|
func NodeInterfaceResolveType(required_extensions []ExtType, default_type **graphql.Object)func(graphql.ResolveTypeParams) *graphql.Object {
|
2023-07-26 20:26:41 -06:00
|
|
|
return func(p graphql.ResolveTypeParams) *graphql.Object {
|
|
|
|
ctx, ok := p.Context.Value("resolve").(*ResolveContext)
|
|
|
|
if ok == false {
|
2023-07-21 18:06:53 -06:00
|
|
|
return nil
|
2023-07-26 20:26:41 -06:00
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
node, ok := p.Value.(NodeResult)
|
2023-07-26 20:26:41 -06:00
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:00:01 -06:00
|
|
|
gql_type, exists := ctx.GQLContext.NodeTypes[node.Result.NodeType]
|
2023-07-31 19:38:30 -06:00
|
|
|
ctx.Context.Log.Logf("gql", "GQL_INTERFACE_RESOLVE_TYPE(%+v): %+v - %t - %+v", node, gql_type, exists, required_extensions)
|
2023-07-26 20:26:41 -06:00
|
|
|
if exists == false {
|
|
|
|
for _, ext := range(required_extensions) {
|
2023-07-29 16:00:01 -06:00
|
|
|
_, exists := node.Result.Extensions[ext]
|
2023-07-26 20:26:41 -06:00
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *default_type
|
|
|
|
}
|
2023-07-26 00:18:11 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
return gql_type
|
|
|
|
}
|
|
|
|
}
|