-
Notifications
You must be signed in to change notification settings - Fork 2
/
resources_builder.go
180 lines (160 loc) · 5.19 KB
/
resources_builder.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package introspection
import (
"fmt"
"github.com/goxgen/goxgen/consts"
"github.com/goxgen/goxgen/graphql/common"
"github.com/goxgen/goxgen/graphql/generator"
"github.com/vektah/gqlparser/v2/ast"
)
func BuildPerResourceIntroHook(schema *ast.Schema, document *ast.SchemaDocument, introValue *map[string]any) error {
resourceDirective, exists := schema.Directives[consts.SchemaDefDirectiveResourceName]
if !exists {
return fmt.Errorf("failed to find %s directive", consts.SchemaDefDirectiveResourceName)
}
resourceActionDirective, exists := schema.Directives[consts.SchemaDefDirectiveActionName]
if !exists {
return fmt.Errorf("failed to find %s directive", consts.SchemaDefDirectiveResourceName)
}
var (
resourceMapType = &ast.Definition{
Kind: ast.Object,
Name: "XgenResourceMap",
}
resourceQueryField = &ast.FieldDefinition{
Name: "resource",
Type: &ast.Type{
NamedType: resourceMapType.Name,
},
}
resourcePropertyType = &ast.Definition{
Kind: ast.Object,
Name: "XgenResourceProperty",
Fields: common.ArgsToObjectFields(resourceDirective.Arguments),
}
actionType = &ast.Definition{
Kind: ast.Object,
Name: "XgenResourceAction",
Fields: common.ArgsToObjectFields(resourceActionDirective.Arguments),
}
resourceDefinitionType = &ast.Definition{
Kind: ast.Object,
Name: "XgenResourceDefinition",
Fields: []*ast.FieldDefinition{
{
Name: "objectName",
Type: &ast.Type{
NamedType: "String",
},
},
{
Name: "properties",
Type: &ast.Type{
NamedType: resourcePropertyType.Name,
},
},
{
Name: "actions",
Type: ast.NonNullListType(&ast.Type{
NamedType: actionType.Name,
}, nil),
},
},
}
)
objects := common.GetDefinedObjects(schema)
resourceValue := make(map[string]any)
for _, object := range objects {
resourceDirective := object.Directives.ForName(consts.SchemaDefDirectiveResourceName)
if resourceDirective == nil {
continue
}
resourceNameArg := resourceDirective.Arguments.ForName("Name")
if resourceNameArg == nil {
return fmt.Errorf("failed to find Name argument in %s directive", resourceDirective.Name)
}
resourceName, err := resourceNameArg.Value.Value(nil)
if err != nil {
return fmt.Errorf("failed to get value of Name argument in %s directive", resourceDirective.Name)
}
resourceNameStr, ok := resourceName.(string)
if !ok {
return fmt.Errorf("failed to cast Name argument value to string in %s directive", resourceDirective.Name)
}
resourceMapType.Fields = common.AppendFieldIfNotExists(
resourceMapType.Fields,
&ast.FieldDefinition{
Name: resourceNameStr,
Type: &ast.Type{
NamedType: resourceDefinitionType.Name,
},
},
)
singleResourceValue := make(map[string]any)
singleResourceValue["objectName"] = object.Name
props := make(map[string]any)
singleResourceValue["properties"] = &props
actions := make([]map[string]any, 0)
singleResourceValue["actions"] = &actions
for _, arg := range resourceDirective.Arguments {
val, err := arg.Value.Value(nil)
if err != nil {
return fmt.Errorf("failed to get value of %s argument in %s directive", arg.Name, resourceDirective.Name)
}
props[arg.Name] = val
}
for _, _object := range objects {
actionDirs := append(
_object.Directives.ForNames(consts.SchemaDefDirectiveActionName),
_object.Directives.ForNames(consts.SchemaDefDirectiveListActionName)...,
)
for _, actionDirective := range actionDirs {
_resourceNameArg := actionDirective.Arguments.ForName(consts.SchemaDefActionDirectiveArgResource)
if _resourceNameArg == nil {
return fmt.Errorf("failed to find Resource argument in %s directive", actionDirective.Name)
}
_resourceName, err := _resourceNameArg.Value.Value(nil)
if err != nil {
return fmt.Errorf("failed to get value of Resource argument in %s directive", actionDirective.Name)
}
_resourceNameStr, ok := _resourceName.(string)
if !ok {
return fmt.Errorf("failed to cast Resource argument value to string in %s directive", actionDirective.Name)
}
if _resourceNameStr != resourceNameStr {
continue
}
action := make(map[string]any)
for _, arg := range actionDirective.Arguments {
val, err := arg.Value.Value(nil)
if err != nil {
return fmt.Errorf("failed to get value of %s argument in %s directive", arg.Name, actionDirective.Name)
}
action[arg.Name] = val
}
actions = append(actions, action)
}
}
resourceValue[resourceNameStr] = singleResourceValue
}
(*introValue)[resourceQueryField.Name] = resourceValue
if len(resourceMapType.Fields) == 0 {
return nil
}
introspectionType := document.Definitions.ForName(TypeName)
if introspectionType == nil {
return fmt.Errorf("failed to find XgenIntrospection type")
}
introspectionType.Fields = common.AppendFieldIfNotExists(introspectionType.Fields, resourceQueryField)
query := document.Extensions.ForName("Query")
if query == nil {
return fmt.Errorf("failed to find Query type")
}
document.Definitions = generator.AppendDefinitionsIfNotExists(
document.Definitions,
resourceMapType,
resourceDefinitionType,
resourcePropertyType,
actionType,
)
return nil
}