2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
2023-07-29 16:34:21 -06:00
|
|
|
"reflect"
|
2023-09-18 11:15:58 -06:00
|
|
|
"fmt"
|
2023-07-21 15:16:35 -06:00
|
|
|
"github.com/graphql-go/graphql"
|
2023-07-29 11:03:41 -06:00
|
|
|
"github.com/graphql-go/graphql/language/ast"
|
2023-07-21 15:16:35 -06:00
|
|
|
)
|
|
|
|
|
2023-09-18 11:15:58 -06:00
|
|
|
func ResolveNodeID(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
node, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Can't get NodeID from %+v", reflect.TypeOf(p.Source))
|
|
|
|
}
|
|
|
|
|
2023-09-18 12:02:30 -06:00
|
|
|
return node.NodeID, nil
|
2023-09-18 11:15:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveNodeTypeHash(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
node, ok := p.Source.(NodeResult)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Can't get TypeHash from %+v", reflect.TypeOf(p.Source))
|
|
|
|
}
|
|
|
|
|
2023-09-18 12:02:30 -06:00
|
|
|
return uint64(node.NodeType), nil
|
2023-09-18 11:15:58 -06:00
|
|
|
}
|
|
|
|
|
2023-07-29 16:34:21 -06:00
|
|
|
func GetFieldNames(ctx *Context, selection_set *ast.SelectionSet) []string {
|
2023-07-29 11:03:41 -06:00
|
|
|
names := []string{}
|
2023-07-29 16:34:21 -06:00
|
|
|
if selection_set == nil {
|
|
|
|
return names
|
|
|
|
}
|
2023-07-29 11:03:41 -06:00
|
|
|
|
2023-07-29 16:34:21 -06:00
|
|
|
for _, sel := range(selection_set.Selections) {
|
|
|
|
switch field := sel.(type) {
|
|
|
|
case *ast.Field:
|
|
|
|
names = append(names, field.Name.Value)
|
|
|
|
case *ast.InlineFragment:
|
|
|
|
names = append(names, GetFieldNames(ctx, field.SelectionSet)...)
|
|
|
|
default:
|
|
|
|
ctx.Log.Logf("gql", "Unknown selection type: %s", reflect.TypeOf(field))
|
2023-07-29 11:03:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:34:21 -06:00
|
|
|
func GetResolveFields(ctx *Context, p graphql.ResolveParams) []string {
|
|
|
|
names := []string{}
|
|
|
|
for _, field := range(p.Info.FieldASTs) {
|
|
|
|
names = append(names, GetFieldNames(ctx, field.SelectionSet)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|