-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
280 lines (246 loc) · 7.28 KB
/
Copy pathutils.go
File metadata and controls
280 lines (246 loc) · 7.28 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package graph
import (
"encoding/json"
"fmt"
"github.com/graphql-go/graphql"
)
// QueryField represents a GraphQL query field with its configuration.
// Implementations must provide both the field configuration and its name.
//
// Use NewResolver to create QueryField instances:
//
// query := graph.NewResolver[User]("user").
// WithArgs(...).
// WithResolver(...).
// BuildQuery()
type QueryField interface {
// Serve returns the GraphQL field configuration
Serve() *graphql.Field
// Name returns the field name used in the GraphQL schema
Name() string
}
// MutationField represents a GraphQL mutation field with its configuration.
// Implementations must provide both the field configuration and its name.
//
// Use NewResolver to create MutationField instances:
//
// mutation := graph.NewResolver[User]("createUser").
// WithInputObject(CreateUserInput{}).
// WithResolver(...).
// BuildMutation()
type MutationField interface {
// Serve returns the GraphQL field configuration
Serve() *graphql.Field
// Name returns the field name used in the GraphQL schema
Name() string
}
// GetRootInfo safely extracts a value from p.Info.RootValue and unmarshals it into the target.
// This is commonly used to retrieve user details set by UserDetailsFn in the GraphContext.
//
// The function handles:
// - Primitive types (string, int) with optimized direct assignment
// - Complex types using JSON marshaling/unmarshaling for type conversion
// - Type mismatches with descriptive error messages
//
// Returns an error if:
// - Root value is nil or not a map
// - The key doesn't exist in the root value
// - Type conversion fails
//
// Example:
//
// // In your resolver
// var user UserDetails
// if err := graph.GetRootInfo(p, "details", &user); err != nil {
// return nil, fmt.Errorf("authentication required")
// }
// // Use user.ID, user.Email, etc.
func GetRootInfo(p ResolveParams, key string, target interface{}) error {
if p.Info.RootValue == nil {
return fmt.Errorf("root value is nil")
}
rootMap, ok := p.Info.RootValue.(map[string]interface{})
if !ok {
return fmt.Errorf("root value is not a map")
}
value, exists := rootMap[key]
if !exists {
return fmt.Errorf("key '%s' not found in root value", key)
}
// If the target is a pointer to a string and value is already a string
if strPtr, ok := target.(*string); ok {
if str, ok := value.(string); ok {
*strPtr = str
return nil
}
}
// If the target is a pointer to an int and value is already an int
if intPtr, ok := target.(*int); ok {
if i, ok := value.(int); ok {
*intPtr = i
return nil
}
if f, ok := value.(float64); ok {
*intPtr = int(f)
return nil
}
}
// For complex types, use JSON marshaling/unmarshaling
jsonBytes, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshal value: %w", err)
}
if err := json.Unmarshal(jsonBytes, target); err != nil {
return fmt.Errorf("failed to unmarshal value into target: %w", err)
}
return nil
}
// GetRootString safely extracts a string value from p.Info.RootValue.
// This is commonly used to retrieve the authentication token.
//
// Returns an error if:
// - Root value is nil or not a map
// - The key doesn't exist in the root value
// - The value is not a string
//
// Example:
//
// // Get authentication token
// token, err := graph.GetRootString(p, "token")
// if err != nil {
// return nil, fmt.Errorf("authentication required")
// }
// // Validate token...
func GetRootString(p ResolveParams, key string) (string, error) {
if p.Info.RootValue == nil {
return "", fmt.Errorf("root value is nil")
}
rootMap, ok := p.Info.RootValue.(map[string]interface{})
if !ok {
return "", fmt.Errorf("root value is not a map")
}
value, exists := rootMap[key]
if !exists {
return "", fmt.Errorf("key '%s' not found in root value", key)
}
str, ok := value.(string)
if !ok {
return "", fmt.Errorf("value for key '%s' is not a string", key)
}
return str, nil
}
// GetArg safely extracts a value from p.Args and unmarshals it into the target.
// This is useful for extracting complex types like structs, slices, or maps.
//
// The function handles:
// - Primitive types (string, int, bool) with optimized direct assignment
// - Complex types using JSON marshaling/unmarshaling for type conversion
// - Type mismatches with descriptive error messages
//
// Returns an error if:
// - The argument key doesn't exist
// - Type conversion fails
//
// Example:
//
// var input CreateUserInput
// if err := graph.GetArg(p, "input", &input); err != nil {
// return nil, err
// }
// // Use input.Name, input.Email, etc.
func GetArg(p ResolveParams, key string, target interface{}) error {
value, exists := p.Args[key]
if !exists {
return fmt.Errorf("argument '%s' not found", key)
}
// If the target is a pointer to a string and value is already a string
if strPtr, ok := target.(*string); ok {
if str, ok := value.(string); ok {
*strPtr = str
return nil
}
}
// If the target is a pointer to an int and value is already an int
if intPtr, ok := target.(*int); ok {
if i, ok := value.(int); ok {
*intPtr = i
return nil
}
if f, ok := value.(float64); ok {
*intPtr = int(f)
return nil
}
}
// If the target is a pointer to a bool and value is already a bool
if boolPtr, ok := target.(*bool); ok {
if b, ok := value.(bool); ok {
*boolPtr = b
return nil
}
}
// For complex types, use JSON marshaling/unmarshaling
jsonBytes, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshal argument: %w", err)
}
if err := json.Unmarshal(jsonBytes, target); err != nil {
return fmt.Errorf("failed to unmarshal argument into target: %w", err)
}
return nil
}
// GetArgString safely extracts a string argument from p.Args.
// Returns an error if the argument doesn't exist or is not a string.
//
// Example:
//
// name, err := graph.GetArgString(p, "name")
func GetArgString(p ResolveParams, key string) (string, error) {
value, exists := p.Args[key]
if !exists {
return "", fmt.Errorf("argument '%s' not found", key)
}
str, ok := value.(string)
if !ok {
return "", fmt.Errorf("argument '%s' is not a string", key)
}
return str, nil
}
// GetArgInt safely extracts an int argument from p.Args.
// Handles both int and float64 types (JSON numbers are parsed as float64).
// Returns an error if the argument doesn't exist or is not a number.
//
// Example:
//
// age, err := graph.GetArgInt(p, "age")
func GetArgInt(p ResolveParams, key string) (int, error) {
value, exists := p.Args[key]
if !exists {
return 0, fmt.Errorf("argument '%s' not found", key)
}
// Handle both int and float64 (JSON numbers are parsed as float64)
switch v := value.(type) {
case int:
return v, nil
case float64:
return int(v), nil
default:
return 0, fmt.Errorf("argument '%s' is not a number", key)
}
}
// GetArgBool safely extracts a bool argument from p.Args.
// Returns an error if the argument doesn't exist or is not a boolean.
//
// Example:
//
// active, err := graph.GetArgBool(p, "active")
func GetArgBool(p ResolveParams, key string) (bool, error) {
value, exists := p.Args[key]
if !exists {
return false, fmt.Errorf("argument '%s' not found", key)
}
b, ok := value.(bool)
if !ok {
return false, fmt.Errorf("argument '%s' is not a boolean", key)
}
return b, nil
}