-
Notifications
You must be signed in to change notification settings - Fork 25
/
gql.go
111 lines (96 loc) · 2.76 KB
/
gql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ndt
import (
"errors"
"github.com/graphql-go/graphql"
"github.com/usnistgov/ndn-dpdk/core/gqlserver"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndni"
)
var (
// GqlNdt is the NDT instance accessible via GraphQL.
GqlNdt *Ndt
errNoGqlNdt = errors.New("NDT unavailable")
//lint:ignore ST1005 'Index' is a field name
errNoIndex = errors.New("Index is unspecified")
)
// GraphQL types.
var (
GqlConfigType *graphql.Object
GqlEntryType *graphql.Object
)
func init() {
GqlConfigType = graphql.NewObject(graphql.ObjectConfig{
Name: "NdtConfig",
Fields: gqlserver.BindFields[Config](nil),
})
gqlserver.AddQuery(&graphql.Field{
Name: "ndtConfig",
Description: "NDT configuration.",
Type: graphql.NewNonNull(GqlConfigType),
Resolve: func(graphql.ResolveParams) (any, error) {
if GqlNdt == nil {
return nil, errNoGqlNdt
}
return GqlNdt.Config(), nil
},
})
GqlEntryType = graphql.NewObject(graphql.ObjectConfig{
Name: "NdtEntry",
Fields: gqlserver.BindFields[Entry](nil),
})
gqlserver.AddQuery(&graphql.Field{
Name: "ndt",
Description: "List of NDT entries.",
Type: gqlserver.NewListNonNullBoth(GqlEntryType),
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Description: "Filter by name.",
Type: ndni.GqlNameType,
},
},
Resolve: func(p graphql.ResolveParams) (any, error) {
if GqlNdt == nil {
return nil, errNoGqlNdt
}
if name, ok := p.Args["name"].(ndn.Name); ok {
entry := GqlNdt.Get(GqlNdt.IndexOfName(name))
return []Entry{entry}, nil
}
return GqlNdt.List(), nil
},
})
gqlserver.AddMutation(&graphql.Field{
Name: "updateNdt",
Description: "Update NDT entry.",
Args: graphql.FieldConfigArgument{
"index": &graphql.ArgumentConfig{
Description: "Entry index or hash. Either 'index' or 'name' is required; if both are specified, 'index' is preferred.",
Type: gqlserver.Uint64,
},
"name": &graphql.ArgumentConfig{
Description: "Name to derive index. Either 'index' or 'name' is required; if both are specified, 'index' is preferred.",
Type: ndni.GqlNameType,
},
"value": &graphql.ArgumentConfig{
Description: "Entry value.",
Type: gqlserver.NonNullInt,
},
},
Type: graphql.NewNonNull(GqlEntryType),
Resolve: func(p graphql.ResolveParams) (any, error) {
if GqlNdt == nil {
return nil, errNoGqlNdt
}
var index uint64
if i, ok := p.Args["index"].(uint64); ok {
index = GqlNdt.IndexOfHash(i)
} else if name, ok := p.Args["name"].(ndn.Name); ok {
index = GqlNdt.IndexOfName(name)
} else {
return nil, errNoIndex
}
GqlNdt.Update(index, uint8(p.Args["value"].(int)))
return GqlNdt.Get(index), nil
},
})
}