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 11:03:41 -06:00
|
|
|
func AddNodeInterfaceFields(gql *Interface) {
|
2023-07-26 20:26:41 -06:00
|
|
|
gql.Interface.AddFieldConfig("ID", &graphql.Field{
|
2023-07-21 18:06:53 -06:00
|
|
|
Type: graphql.String,
|
|
|
|
})
|
2023-07-26 00:18:11 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
gql.Interface.AddFieldConfig("TypeHash", &graphql.Field{
|
2023-07-26 00:18:11 -06:00
|
|
|
Type: graphql.String,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
func AddLockableInterfaceFields(gql *Interface) {
|
|
|
|
addLockableInterfaceFields(gql, InterfaceLockable)
|
2023-07-21 18:06:53 -06:00
|
|
|
}
|
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
func addLockableInterfaceFields(gql *Interface, gql_lockable *Interface) {
|
2023-07-26 20:26:41 -06:00
|
|
|
AddNodeInterfaceFields(gql)
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
gql.Interface.AddFieldConfig("Requirements", &graphql.Field{
|
|
|
|
Type: gql_lockable.List,
|
|
|
|
})
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
gql.Interface.AddFieldConfig("Dependencies", &graphql.Field{
|
|
|
|
Type: gql_lockable.List,
|
|
|
|
})
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
gql.Interface.AddFieldConfig("Owner", &graphql.Field{
|
|
|
|
Type: gql_lockable.Interface,
|
2023-07-21 18:06:53 -06:00
|
|
|
})
|
2023-07-26 20:26:41 -06:00
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
func NodeHasExtensions(node *Node, extensions []ExtType) bool {
|
|
|
|
if node == nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
for _, ext := range(extensions) {
|
|
|
|
_, has := node.Extensions[ext]
|
|
|
|
if has == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
return true
|
|
|
|
}
|
2023-07-26 00:18:11 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
func GQLNodeHasExtensions(extensions []ExtType) func(graphql.IsTypeOfParams) bool {
|
|
|
|
return func(p graphql.IsTypeOfParams) bool {
|
|
|
|
node, ok := p.Value.(*Node)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-21 18:06:53 -06:00
|
|
|
|
2023-07-26 20:26:41 -06:00
|
|
|
return NodeHasExtensions(node, extensions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NodeResolver(required_extensions []ExtType, default_type **graphql.Object)func(graphql.ResolveTypeParams) *graphql.Object {
|
|
|
|
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-26 20:26:41 -06:00
|
|
|
node, ok := p.Value.(*Node)
|
|
|
|
if ok == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gql_type, exists := ctx.GQLContext.NodeTypes[node.Type]
|
|
|
|
if exists == false {
|
|
|
|
for _, ext := range(required_extensions) {
|
|
|
|
_, exists := node.Extensions[ext]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 00:18:11 -06:00
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
var InterfaceNode = NewInterface("Node", "DefaultNode", []*graphql.Interface{}, []ExtType{}, func(gql *Interface) {
|
2023-07-26 23:57:50 -06:00
|
|
|
AddNodeInterfaceFields(gql)
|
2023-07-29 11:03:41 -06:00
|
|
|
}, func(gql *Interface) {
|
2023-07-26 23:57:50 -06:00
|
|
|
AddNodeFields(gql.Default)
|
|
|
|
})
|
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
var InterfaceLockable = NewInterface("Lockable", "DefaultLockable", []*graphql.Interface{InterfaceNode.Interface}, []ExtType{LockableExtType}, func(gql *Interface) {
|
2023-07-26 23:57:50 -06:00
|
|
|
addLockableInterfaceFields(gql, gql)
|
2023-07-29 11:03:41 -06:00
|
|
|
}, func(gql *Interface) {
|
2023-07-26 23:57:50 -06:00
|
|
|
addLockableFields(gql.Default, gql.Interface, gql.List)
|
|
|
|
})
|
|
|
|
|