/
examples.graphql
612 lines (530 loc) · 14.6 KB
/
examples.graphql
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# the definition of directive
directive @fetchSource(name: String!, sourceConvert:String) on FIELD
directive @skipBy(predicate: String!) on FIELD | INLINE_FRAGMENT | FRAGMENT_SPREAD
directive @includeBy(predicate: String!) on FIELD | INLINE_FRAGMENT | FRAGMENT_SPREAD
directive @mock(value: String!) on FIELD
directive @filter(predicate: String!) on FIELD
directive @sort(key: String!,reversed: Boolean = false) on FIELD
directive @distinct(comparator:String) on FIELD
directive @sortBy(comparator: String!, reversed: Boolean = false) on FIELD
directive @map(mapper:String!, dependencySources:[String!]) on FIELD
directive @argumentTransform(argumentName:String!, operateType:ParamTransformType = MAP, expression:String, dependencySources:[String!]) on FIELD
#directive @argumentTransform(argument:String!, operaType:ParamTransformType, expression:String, dependencySource:String) repeatable on FIELD
enum ParamTransformType{
MAP
FILTER
LIST_MAP
}
query repeatableArgumentTest($userId:Int, $clientVersion:String){
consumer{
userInfo(userId: $userId,clientVersion: $clientVersion)
@argumentTransform(argumentName: "userId",expression: "userId*10")
@argumentTransform(argumentName: "clientVersion", expression: "clientVersion+'Plus'")
{
userId
name
clientVersion
}
}
}
query defaultOperatorType($itemIds:[Int]){
commodity{
itemList(itemIds: $itemIds){
itemId
sellerId @fetchSource(name: "sellerIds")
}
}
consumer{
userInfoList(userIds: 1)
@argumentTransform(argumentName: "userIds" ,expression: "sellerIds",dependencySources: ["sellerIds"])
{
userId
name
}
userInfo(userId: 1)
@argumentTransform(argumentName: "userId",expression: "userId*2")
{
userId
name
}
}
}
query nestedFetchSource_case01($itemIds:[Int]){
commodity{
itemList(itemIds: $itemIds){
skuList{
itemId
skuId
sellerId @fetchSource(name:"sellerIds")
}
}
}
consumer{
userInfoList(userIds: 1)
@argumentTransform(argumentName: "userIds",operateType: MAP,expression: "sellerIds",dependencySources: ["sellerIds"])
{
userId
name
}
}
}
query skipByTest_exceptionQueryTest01($userId: Int) {
consumer{
userInfo(userId: $userId)
@skipBy(predicate: "userId+18")
{
userId
}
}
}
query skipByTest_FragmentWithSkip($userId: Int) {
consumer{
userInfo(userId: $userId) {
userId
...skipUserInfo
}
}
}
fragment skipUserInfo on User{
email @skipBy(predicate: "userId<18")
}
query skipByTest_fragmentSpreadTest01($userId: Int) {
consumer{
userInfo(userId: $userId) {
userId
...userinfo @skipBy(predicate: "userId>18")
}
notSkipUserInfo: userInfo(userId: $userId) {
userId
...userinfo @skipBy(predicate: "userId<18")
}
}
}
# 跟 @skip 和 @include 保持一致,FRAGMENT_DEFINITION 不进行判断
fragment userinfo on User{
age
name
email
}
query skipByTest_inlineFragmentTest01($userId: Int) {
consumer{
userInfo(userId: $userId) {
userId
... @skipBy(predicate: "userId>18") {
age
name
email
}
}
notSkipUserInfo: userInfo(userId: $userId) {
userId
... @skipBy(predicate: "userId<18") {
age
name
email
}
}
}
}
query distinctSimpleCase_notSetComparator($userIds:[Int]){
consumer{
distinctUserInfoList: userInfoList(userIds: $userIds)
@distinct
{
userId
age
}
userInfoList(userIds: $userIds)
{
userId
age
}
}
}
query distinctSimpleCase_01($userIds:[Int]){
consumer{
distinctUserInfoList: userInfoList(userIds: $userIds)
@distinct(comparator: "age")
{
userId
age
}
userInfoList(userIds: $userIds)
{
userId
age
}
}
}
query parseArgumentFromVariableSimpleCase_01($userIds:[Int]){
consumer{
userInfoList(userIds: $userIds){
userId
age
name
}
# get argument from variable, 'testUserInfoList' haven't use any variable
testUserInfoList: userInfoList(userIds: 1)
@argumentTransform(argumentName: "userIds",operateType: MAP,expression: "userIds")
{
userId
age
name
}
}
}
#
# 假设存在如下查询:刚开始只查询了 someInfo 字段,后续查询commodity字段的时候,
# 希望从之前的请求变量userIdAnditemIdPair解析参数,而非调用方在硬编码添加参数
#
#query parseArgumentFromVariable_case01($userIdAnditemIdPair:Any){
#
# someInfo: variableBlackhole(anyArgument: $userIdAnditemIdPair){
# ignored
# }
#
# commodity{
# itemList(itemIds: 1)
# @argumentTransform(argumentName: "itemIds",operateType: MAP,expression: "mapValues(userIdAnditemIdPair)")
# {
# itemId
# name
# salePrice
# }
# }
#}
query queryMoreDetail_case01($userId:Int,$clientVersion:String){
consumer{
userInfo(
userId: $userId,
# 受限于graphql原生语法校验,变量必须被明确的作为参数使用
clientVersion: $clientVersion){
userId
age
name
# 只在v2版本的客户端中展示
email @includeBy(predicate: "clientVersion == 'v2'")
}
}
}
query sortItemListBySaleAmount($itemIdList:[Int]){
commodity{
itemList(itemIds: $itemIdList)
@sortBy(comparator: "saleAmount",reversed: true)
{
itemId
name
saleAmount
}
originalItemList: itemList(itemIds: $itemIdList){
itemId
name
saleAmount
}
}
}
query filterUnSaleCommodity($ItemIds:[Int]){
commodity{
filteredItemList: itemList(itemIds: $ItemIds)
@filter(predicate: "onSale")
{
itemId
onSale
name
salePrice
}
}
}
query sourceInList_case01($itemIds:[Int]){
commodity{
itemList(itemIds: $itemIds){
# 保存商品的卖家id,结果为 List<Integer>
sellerId @fetchSource(name: "sellerIdList")
name
saleAmount
salePrice
}
}
consumer{
userInfoList(userIds: 1)
@argumentTransform(argumentName: "userIds", # 对参数 userIds 进行转换
operateType: MAP, # 操作类型为参数整体转换
expression: "sellerIdList", # 表达式表示使用表达式变量 sellerIdList 对参数作整体替换
dependencySources: ["sellerIdList"] # 依赖了全局变量 sellerIdList
){
userId
name
age
}
}
}
query simpleIncludeBy_case01($userId:Int){
consumer{
userInfo(userId: $userId){
userId
name @includeBy(predicate: "userId!=2")
}
}
}
query filterPrimitiveType_case01{
marketing{
coupon(couponId: 1){
bindingItemIds @filter(predicate: "ele%2 == 0")
}
}
}
query filter_case01{
commodity{
itemList(itemIds: [9,11,10,12])
@filter(predicate: "!isContainBindingItemIds")
{
isContainBindingItemIds:onSale @map(mapper: "listContain(bindingItemIds,itemId)",dependencySources: "bindingItemIds")
itemId
name
salePrice
}
}
marketing{
coupon(couponId: 1){
bindingItemIds @fetchSource(name: "bindingItemIds")
}
}
}
query skipBy_case01($userId:Int){
consumer{
userInfo(userId: $userId)
# the userInfo field would not be queried if 'userId>100' is true
@skipBy(predicate: "userId>100")
{
userId
name
}
}
}
query includeBy_case01($userId:Int){
consumer{
userInfo(userId: $userId)
# the userInfo field will be queried if 'userId>100' is true
@includeBy(predicate: "userId>100")
{
userId
name
}
}
}
query sortResult_case01{
commodity{
itemList(itemIds: [1,2,3,4])
{
itemId
sortKey: itemId @map(mapper: "itemId/2")
salePrice
saleAmount(itemId: 1)
}
}
}
# 有数据依赖的排序
query sortByWithSource_case01{
commodity{
itemList(itemIds: [9,11,10,12])
@sortBy(comparator: "!isContainBindingItemIds")
{
isContainBindingItemIds:onSale @map(mapper: "listContain(bindingItemIds,itemId)",dependencySources: "bindingItemIds")
itemId
name
salePrice
}
}
marketing{
coupon(couponId: 1){
bindingItemIds @fetchSource(name: "bindingItemIds")
}
}
}
# 表达式排序
query sortByCase_01{
consumer{
userInfoList(userIds: [1,2,3,4])
@sortBy(comparator: "userId%2",reversed: true)
{
userId
name
}
}
}
# 简单排序
query sortCase_01{
consumer{
userInfoList(userIds: [3,4,1,2])
@sort(key: "userId",reversed: true)
{
userId
name
}
}
}
# 参数拼接
query userNewInfo($userId: Int){
consumer{
isNewUser(redisKey: "",userId: $userId)
# 将参数拼接为 redis 的key,fashion:shoes:{userId}
@argumentTransform(argumentName: "redisKey",operateType: MAP ,expression: "'fashion:shoes:' + str(userId)")
{
userId
isNewUser
sceneKey
}
}
}
# 父子路径上有 @fetchSource
query sourceOnAncestorPath_case01{
consumer{
userInfo(userId: 2)
@fetchSource(name: "userInfo")
{
userId @fetchSource(name: "userId")
}
}
commodity{
item(itemId: 1){
itemId
userId: itemId @map(mapper: "userId",dependencySources: "userId")
userIdInUserInfo: itemId @map(mapper: "userInfo.userId",dependencySources: "userInfo")
}
}
}
# 父子路径上有 @fetchSource,结果均为null
query sourceOnAncestorPath_case02{
consumer{
userInfo
@fetchSource(name: "userInfo")
{
userId @fetchSource(name: "userId")
}
}
commodity{
item(itemId: 1){
itemId
userId: itemId @map(mapper: "userId",dependencySources: "userId")
userIdInUserInfo: itemId @map(mapper: "userInfo.userId",dependencySources: "userInfo")
}
}
}
# 查找券信息和列表商品信息;使用 @mapper 拼接券的描述文案;如果商品绑定了券则返回券后价和是否绑定的标识
query calculateCouponPrice_Case01 ($couponId: Int, $itemIds: [Int]){
marketing{
coupon(couponId: $couponId)
@fetchSource(name: "itemCouponInfo",sourceConvert: "list2MapWithAssignedValue('bindingItemIds','price')")
{
base
price
bindingItemIds
desc: couponText @map(mapper: "'满' + base + '减' + price")
}
}
commodity{
itemList(itemIds: $itemIds){
itemId
name
salePrice
isUsedCoupon: onSale @map(dependencySources: "itemCouponInfo",mapper: "seq.get(itemCouponInfo,itemId)!=nil")
# 券后价
couponPrice: salePrice @map(dependencySources: "itemCouponInfo",mapper: "salePrice - (seq.get(itemCouponInfo,itemId) == nil? 0:seq.get(itemCouponInfo,itemId)) ")
}
}
}
query sourceConvert_case01 ($itemIds: [Int]){
commodity{
itemList(itemIds: $itemIds){
itemId
sellerId @fetchSource(name: "sellerIds",sourceConvert: "filter(sellerId,seq.gt(3))")
name
salePrice
}
}
consumer{
userInfoList(userIds: 1)
@argumentTransform(argumentName: "userIds",operateType: MAP,expression: "sellerIds", dependencySources: ["sellerIds"])
{
userId
age
name
}
}
}
## 查询某个红包可用的商品集合信息,并过滤掉下架的商品
query getItemListBindingCouponIdAndFilterUnSaleItems ( $couponId: Int) {
commodity{
itemList(itemIds: 1)
@argumentTransform(argumentName: "itemIds", operateType: MAP,dependencySources: "itemIdList",expression: "itemIdList")
@filter(predicate: "onSale")
{
itemId
name
salePrice
onSale
}
}
marketing{
coupon(couponId: $couponId){
bindingItemIds
@fetchSource(name: "itemIdList")
}
}
}
# 过滤掉该券不可用的参数,然后在请求下游数据源
query filterItemListByBindingCouponIdAndFilterUnSaleItems ( $couponId: Int,$itemIds: [Int]) {
commodity{
itemList(itemIds: $itemIds)
@argumentTransform(argumentName: "itemIds", operateType: FILTER,dependencySources: "itemIdList",expression: "listContain(itemIdList,ele)")
{
itemId
name
salePrice
onSale
}
}
marketing{
coupon(couponId: $couponId){
bindingItemIds
@fetchSource(name: "itemIdList")
}
}
}
# 对参数进行处理
query mapListArgument($itemIds: [Int]){
commodity{
itemList(itemIds: $itemIds)
@argumentTransform(argumentName: "itemIds",operateType: LIST_MAP,expression: "ele*10")
{
itemId
name
}
}
}
# 如果用户不在 ab实验实验组区间[0,3]内,则对其查看的页面不展示优惠券、即不请求券数据
#query abUserForCouponAcquire($userId: Int, $couponId: Int,$abKey:String){
#
# marketing
# @skipBy(predicate: "abValue <= 3",dependencySources: "abValue")
# {
# coupon(couponId: $couponId){
# couponId
# couponText
# price
# }
# }
#
# toolInfo{
# abInfo(userId: $userId,abKey:$abKey) @fetchSource(name: "abValue")
# }
#}
# moc
query mockEmail{
consumer{
userInfo(userId: 1){
userId
email @mock(value: "mockedValue@foxmail.com")
}
}
}