forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_test.go
307 lines (278 loc) · 8.45 KB
/
graphql_test.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package huma
import (
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type CategoryParam struct {
CategoryID string `path:"category-id"`
}
type CategorySummary struct {
ID string `json:"id" graphParam:"category-id" doc:"Category ID"`
}
type Category struct {
CategorySummary
Featured bool `json:"featured" doc:"Display as featured in the app"`
Code []byte `json:"code" doc:"Category code"`
products map[string]*Product `json:"-"`
}
type ProductParam struct {
ProductID string `path:"product-id"`
}
type ProductSummary struct {
ID string `json:"id" graphParam:"product-id" doc:"Product ID"`
}
type Product struct {
ProductSummary
SuggestedPrice float32 `json:"suggested_price"`
Created *time.Time `json:"created,omitempty" doc:"When this product was created"`
Metadata map[string]string `json:"metadata,omitempty" doc:"Additional information about the product"`
Empty *struct{} `json:"empty"`
stores map[string]*Store `json:"-"`
}
type StoreSummary struct {
ID string `json:"id" graphParam:"store-id" doc:"Store ID"`
}
type Store struct {
StoreSummary
URL string `json:"url" doc:"Web link to buy product"`
}
func TestGraphQL(t *testing.T) {
now, _ := time.Parse(time.RFC3339, "2022-02-22T22:22:22Z")
amazon := &Store{StoreSummary: StoreSummary{ID: "amazon"}, URL: "https://www.amazon.com/"}
target := &Store{StoreSummary: StoreSummary{ID: "target"}, URL: "https://www.target.com/"}
xsx := &Product{ProductSummary: ProductSummary{ID: "xbox_series_x"}, SuggestedPrice: 499.99, Created: &now, Metadata: map[string]string{"foo": "bar"}, stores: map[string]*Store{"amazon": amazon, "target": target}, Empty: &struct{}{}}
ps5 := &Product{ProductSummary: ProductSummary{ID: "playstation_ps5"}, SuggestedPrice: 499.99, Created: &now, stores: map[string]*Store{"amazon": amazon}}
ns := &Product{ProductSummary: ProductSummary{ID: "nintendo_switch"}, SuggestedPrice: 349.99, stores: map[string]*Store{"target": target}}
videoGames := &Category{
CategorySummary: CategorySummary{ID: "video_games"},
Featured: true,
Code: []byte{'h', 'i'},
products: map[string]*Product{
"xbox_series_x": xsx,
"playstation_ps5": ps5,
"nintendo_switch": ns,
},
}
categories := map[string]*Category{
"video_games": videoGames,
}
app := newTestRouter()
categoriesResource := app.Resource("/categories")
categoriesResource.Get("get-categories", "doc",
NewResponse(http.StatusOK, "").Model([]CategorySummary{}).Headers("link"),
).Run(func(ctx Context, input struct {
Cursor string `query:"cursor"`
Limit int `query:"limit" default:"10"`
}) {
summaries := []CategorySummary{}
for _, cat := range categories {
summaries = append(summaries, cat.CategorySummary)
}
sort.Slice(summaries, func(i, j int) bool {
return summaries[i].ID < summaries[j].ID
})
if input.Limit == 0 {
input.Limit = 10
}
if len(summaries) < input.Limit {
input.Limit = len(summaries)
}
ctx.Header().Set("Link", "</categories?cursor=abc123>; rel=\"next\"")
ctx.WriteModel(http.StatusOK, summaries[:input.Limit])
})
categoriesResource.Delete("delete-category", "doc",
NewResponse(http.StatusNoContent, ""),
).Run(func(ctx Context) {
ctx.WriteHeader(http.StatusNoContent)
})
categoriesResource.SubResource("/{category-id}").Get("get-category", "doc",
NewResponse(http.StatusOK, "").Model(&Category{}),
NewResponse(http.StatusNotFound, "").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
CategoryParam
}) {
if categories[input.CategoryID] == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
ctx.WriteModel(http.StatusOK, categories[input.CategoryID])
})
app.Resource("/categories/{category-id}/products").Get("get-items", "doc",
NewResponse(http.StatusOK, "").Model([]ProductSummary{}),
NewResponse(http.StatusNotFound, "").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
CategoryParam
}) {
if categories[input.CategoryID] == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
summaries := []ProductSummary{}
for _, item := range categories[input.CategoryID].products {
summaries = append(summaries, item.ProductSummary)
}
sort.Slice(summaries, func(i, j int) bool {
return summaries[i].ID < summaries[j].ID
})
ctx.WriteModel(http.StatusOK, summaries)
})
app.Resource("/categories/{category-id}/products/{product-id}").Get("get-item", "doc",
NewResponse(http.StatusOK, "").Model(&Product{}),
NewResponse(http.StatusNotFound, "").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
CategoryParam
ProductParam
}) {
if categories[input.CategoryID] == nil || categories[input.CategoryID].products[input.ProductID] == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
ctx.WriteModel(http.StatusOK, categories[input.CategoryID].products[input.ProductID])
})
app.Resource("/categories/{category-id}/products/{product-id}/stores").Get("get-stores", "doc",
NewResponse(http.StatusOK, "").Model([]StoreSummary{}),
NewResponse(http.StatusNotFound, "").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
CategoryParam
ProductParam
}) {
if categories[input.CategoryID] == nil || categories[input.CategoryID].products[input.ProductID] == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
summaries := []StoreSummary{}
for _, store := range categories[input.CategoryID].products[input.ProductID].stores {
summaries = append(summaries, store.StoreSummary)
}
sort.Slice(summaries, func(i, j int) bool {
return summaries[i].ID < summaries[j].ID
})
ctx.WriteModel(http.StatusOK, summaries)
})
app.Resource("/categories/{category-id}/products/{product-id}/stores/{store-id}").Get("get-store", "doc",
NewResponse(http.StatusOK, "").Model(&Store{}),
NewResponse(http.StatusNotFound, "").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
CategoryParam
ProductParam
StoreID string `path:"store-id" doc:"Store ID"`
}) {
if categories[input.CategoryID] == nil || categories[input.CategoryID].products[input.ProductID] == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
ctx.WriteModel(http.StatusOK, categories[input.CategoryID].products[input.ProductID].stores[input.StoreID])
})
app.EnableGraphQL(&GraphQLConfig{
ComplexityLimit: 250,
})
query := strings.Replace(strings.Replace(`{
categories(limit: 1) {
headers {
link
}
links {
next {
key
value
}
}
edges {
categoriesItem {
id
featured
code
products {
edges {
productsItem {
id
suggested_price
created
metadata{
key
value
}
empty {
_
}
stores {
edges {
storesItem {
id
url
}
}
}
}
}
}
}
}
}
}`, "\n", " ", -1), "\t", "", -1)
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/graphql?query="+query, nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.YAMLEq(t, strings.Replace(`
data:
categories:
headers:
link: </categories?cursor=abc123>; rel="next", </schemas/CategorySummaryList.json>; rel="describedby"
links:
next:
- key: cursor
value: abc123
edges:
- categoriesItem:
id: video_games
featured: true
code: aGk=
products:
edges:
- productsItem:
id: nintendo_switch
suggested_price: 349.99
created: null
metadata: null
empty: null
stores:
edges:
- storesItem:
id: target
url: https://www.target.com/
- productsItem:
id: playstation_ps5
suggested_price: 499.99
created: "2022-02-22T22:22:22Z"
metadata: null
empty: null
stores:
edges:
- storesItem:
id: amazon
url: https://www.amazon.com/
- productsItem:
id: xbox_series_x
suggested_price: 499.99
created: "2022-02-22T22:22:22Z"
metadata:
- key: foo
value: bar
empty:
_: null
stores:
edges:
- storesItem:
id: amazon
url: https://www.amazon.com/
- storesItem:
id: target
url: https://www.target.com/
`, "\t", " ", -1), w.Body.String())
}