@contember/client-content 2.0.0-alpha.6 → 2.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/api-extractor.json +3 -0
  2. package/dist/development/index.cjs +5 -5
  3. package/dist/development/index.js +5 -5
  4. package/dist/development/src/ContentClient.cjs.map +1 -0
  5. package/dist/development/src/ContentClient.js.map +1 -0
  6. package/dist/development/src/ContentQueryBuilder.cjs.map +1 -0
  7. package/dist/development/src/ContentQueryBuilder.js.map +1 -0
  8. package/dist/development/src/nodes/ContentEntitySelection.cjs.map +1 -0
  9. package/dist/development/src/nodes/ContentEntitySelection.js.map +1 -0
  10. package/dist/development/src/nodes/ContentOperation.cjs.map +1 -0
  11. package/dist/development/src/nodes/ContentOperation.js.map +1 -0
  12. package/dist/development/src/utils/ContentOperationSet.cjs.map +1 -0
  13. package/dist/development/src/utils/ContentOperationSet.js.map +1 -0
  14. package/dist/development/src/utils/createListArgs.cjs.map +1 -0
  15. package/dist/development/src/utils/createListArgs.js.map +1 -0
  16. package/dist/development/src/utils/createMutationOperationSet.cjs.map +1 -0
  17. package/dist/development/src/utils/createMutationOperationSet.js.map +1 -0
  18. package/dist/development/src/utils/createQueryOperationSet.cjs.map +1 -0
  19. package/dist/development/src/utils/createQueryOperationSet.js.map +1 -0
  20. package/dist/development/src/utils/createTypedArgs.cjs.map +1 -0
  21. package/dist/development/src/utils/createTypedArgs.js.map +1 -0
  22. package/dist/development/src/utils/mutationFragments.cjs.map +1 -0
  23. package/dist/development/src/utils/mutationFragments.js.map +1 -0
  24. package/dist/production/index.cjs +5 -5
  25. package/dist/production/index.js +5 -5
  26. package/dist/production/src/ContentClient.cjs.map +1 -0
  27. package/dist/production/src/ContentClient.js.map +1 -0
  28. package/dist/production/src/ContentQueryBuilder.cjs.map +1 -0
  29. package/dist/production/src/ContentQueryBuilder.js.map +1 -0
  30. package/dist/production/src/nodes/ContentEntitySelection.cjs.map +1 -0
  31. package/dist/production/src/nodes/ContentEntitySelection.js.map +1 -0
  32. package/dist/production/src/nodes/ContentOperation.cjs.map +1 -0
  33. package/dist/production/src/nodes/ContentOperation.js.map +1 -0
  34. package/dist/production/src/utils/ContentOperationSet.cjs.map +1 -0
  35. package/dist/production/src/utils/ContentOperationSet.js.map +1 -0
  36. package/dist/production/src/utils/createListArgs.cjs.map +1 -0
  37. package/dist/production/src/utils/createListArgs.js.map +1 -0
  38. package/dist/production/src/utils/createMutationOperationSet.cjs.map +1 -0
  39. package/dist/production/src/utils/createMutationOperationSet.js.map +1 -0
  40. package/dist/production/src/utils/createQueryOperationSet.cjs.map +1 -0
  41. package/dist/production/src/utils/createQueryOperationSet.js.map +1 -0
  42. package/dist/production/src/utils/createTypedArgs.cjs.map +1 -0
  43. package/dist/production/src/utils/createTypedArgs.js.map +1 -0
  44. package/dist/production/src/utils/mutationFragments.cjs.map +1 -0
  45. package/dist/production/src/utils/mutationFragments.js.map +1 -0
  46. package/dist/types/nodes/ContentEntitySelection.d.ts.map +1 -1
  47. package/dist/types/nodes/ContentOperation.d.ts.map +1 -1
  48. package/dist/types/nodes/TypedEntitySelection.d.ts.map +1 -1
  49. package/dist/types/tsconfig.tsbuildinfo +1 -1
  50. package/dist/types/utils/createListArgs.d.ts +1 -1
  51. package/dist/types/utils/createListArgs.d.ts.map +1 -1
  52. package/dist/types/utils/createMutationOperationSet.d.ts.map +1 -1
  53. package/dist/types/utils/createQueryOperationSet.d.ts.map +1 -1
  54. package/dist/types/utils/createTypedArgs.d.ts.map +1 -1
  55. package/package.json +9 -16
  56. package/src/ContentQueryBuilder.ts +1 -1
  57. package/src/nodes/TypedEntitySelection.ts +1 -1
  58. package/src/tsconfig.json +3 -0
  59. package/src/utils/createListArgs.ts +1 -1
  60. package/tests/cases/unit/mutation.test.ts +1476 -0
  61. package/tests/cases/unit/query.test.ts +519 -0
  62. package/tests/cases/unit/types.test.ts +144 -0
  63. package/tests/client/entities.ts +108 -0
  64. package/tests/client/enums.ts +1 -0
  65. package/tests/client/index.ts +17 -0
  66. package/tests/client/names.ts +116 -0
  67. package/tests/generate.ts +13 -0
  68. package/tests/lib.ts +16 -0
  69. package/tests/schema.ts +37 -0
  70. package/tests/tsconfig.json +13 -0
  71. package/tsconfig.json +8 -0
  72. package/tsdoc.json +6 -0
  73. package/vitest.config.ts +3 -0
  74. package/dist/development/ContentClient.cjs.map +0 -1
  75. package/dist/development/ContentClient.js.map +0 -1
  76. package/dist/development/ContentQueryBuilder.cjs.map +0 -1
  77. package/dist/development/ContentQueryBuilder.js.map +0 -1
  78. package/dist/development/nodes/ContentEntitySelection.cjs.map +0 -1
  79. package/dist/development/nodes/ContentEntitySelection.js.map +0 -1
  80. package/dist/development/nodes/ContentOperation.cjs.map +0 -1
  81. package/dist/development/nodes/ContentOperation.js.map +0 -1
  82. package/dist/development/utils/ContentOperationSet.cjs.map +0 -1
  83. package/dist/development/utils/ContentOperationSet.js.map +0 -1
  84. package/dist/development/utils/createListArgs.cjs.map +0 -1
  85. package/dist/development/utils/createListArgs.js.map +0 -1
  86. package/dist/development/utils/createMutationOperationSet.cjs.map +0 -1
  87. package/dist/development/utils/createMutationOperationSet.js.map +0 -1
  88. package/dist/development/utils/createQueryOperationSet.cjs.map +0 -1
  89. package/dist/development/utils/createQueryOperationSet.js.map +0 -1
  90. package/dist/development/utils/createTypedArgs.cjs.map +0 -1
  91. package/dist/development/utils/createTypedArgs.js.map +0 -1
  92. package/dist/development/utils/mutationFragments.cjs.map +0 -1
  93. package/dist/development/utils/mutationFragments.js.map +0 -1
  94. package/dist/production/ContentClient.cjs.map +0 -1
  95. package/dist/production/ContentClient.js.map +0 -1
  96. package/dist/production/ContentQueryBuilder.cjs.map +0 -1
  97. package/dist/production/ContentQueryBuilder.js.map +0 -1
  98. package/dist/production/nodes/ContentEntitySelection.cjs.map +0 -1
  99. package/dist/production/nodes/ContentEntitySelection.js.map +0 -1
  100. package/dist/production/nodes/ContentOperation.cjs.map +0 -1
  101. package/dist/production/nodes/ContentOperation.js.map +0 -1
  102. package/dist/production/utils/ContentOperationSet.cjs.map +0 -1
  103. package/dist/production/utils/ContentOperationSet.js.map +0 -1
  104. package/dist/production/utils/createListArgs.cjs.map +0 -1
  105. package/dist/production/utils/createListArgs.js.map +0 -1
  106. package/dist/production/utils/createMutationOperationSet.cjs.map +0 -1
  107. package/dist/production/utils/createMutationOperationSet.js.map +0 -1
  108. package/dist/production/utils/createQueryOperationSet.cjs.map +0 -1
  109. package/dist/production/utils/createQueryOperationSet.js.map +0 -1
  110. package/dist/production/utils/createTypedArgs.cjs.map +0 -1
  111. package/dist/production/utils/createTypedArgs.js.map +0 -1
  112. package/dist/production/utils/mutationFragments.cjs.map +0 -1
  113. package/dist/production/utils/mutationFragments.js.map +0 -1
  114. /package/dist/development/{ContentClient.cjs → src/ContentClient.cjs} +0 -0
  115. /package/dist/development/{ContentClient.js → src/ContentClient.js} +0 -0
  116. /package/dist/development/{ContentQueryBuilder.cjs → src/ContentQueryBuilder.cjs} +0 -0
  117. /package/dist/development/{ContentQueryBuilder.js → src/ContentQueryBuilder.js} +0 -0
  118. /package/dist/development/{nodes → src/nodes}/ContentEntitySelection.cjs +0 -0
  119. /package/dist/development/{nodes → src/nodes}/ContentEntitySelection.js +0 -0
  120. /package/dist/development/{nodes → src/nodes}/ContentOperation.cjs +0 -0
  121. /package/dist/development/{nodes → src/nodes}/ContentOperation.js +0 -0
  122. /package/dist/development/{utils → src/utils}/ContentOperationSet.cjs +0 -0
  123. /package/dist/development/{utils → src/utils}/ContentOperationSet.js +0 -0
  124. /package/dist/development/{utils → src/utils}/createListArgs.cjs +0 -0
  125. /package/dist/development/{utils → src/utils}/createListArgs.js +0 -0
  126. /package/dist/development/{utils → src/utils}/createMutationOperationSet.cjs +0 -0
  127. /package/dist/development/{utils → src/utils}/createMutationOperationSet.js +0 -0
  128. /package/dist/development/{utils → src/utils}/createQueryOperationSet.cjs +0 -0
  129. /package/dist/development/{utils → src/utils}/createQueryOperationSet.js +0 -0
  130. /package/dist/development/{utils → src/utils}/createTypedArgs.cjs +0 -0
  131. /package/dist/development/{utils → src/utils}/createTypedArgs.js +0 -0
  132. /package/dist/development/{utils → src/utils}/mutationFragments.cjs +0 -0
  133. /package/dist/development/{utils → src/utils}/mutationFragments.js +0 -0
  134. /package/dist/production/{ContentClient.cjs → src/ContentClient.cjs} +0 -0
  135. /package/dist/production/{ContentClient.js → src/ContentClient.js} +0 -0
  136. /package/dist/production/{ContentQueryBuilder.cjs → src/ContentQueryBuilder.cjs} +0 -0
  137. /package/dist/production/{ContentQueryBuilder.js → src/ContentQueryBuilder.js} +0 -0
  138. /package/dist/production/{nodes → src/nodes}/ContentEntitySelection.cjs +0 -0
  139. /package/dist/production/{nodes → src/nodes}/ContentEntitySelection.js +0 -0
  140. /package/dist/production/{nodes → src/nodes}/ContentOperation.cjs +0 -0
  141. /package/dist/production/{nodes → src/nodes}/ContentOperation.js +0 -0
  142. /package/dist/production/{utils → src/utils}/ContentOperationSet.cjs +0 -0
  143. /package/dist/production/{utils → src/utils}/ContentOperationSet.js +0 -0
  144. /package/dist/production/{utils → src/utils}/createListArgs.cjs +0 -0
  145. /package/dist/production/{utils → src/utils}/createListArgs.js +0 -0
  146. /package/dist/production/{utils → src/utils}/createMutationOperationSet.cjs +0 -0
  147. /package/dist/production/{utils → src/utils}/createMutationOperationSet.js +0 -0
  148. /package/dist/production/{utils → src/utils}/createQueryOperationSet.cjs +0 -0
  149. /package/dist/production/{utils → src/utils}/createQueryOperationSet.js +0 -0
  150. /package/dist/production/{utils → src/utils}/createTypedArgs.cjs +0 -0
  151. /package/dist/production/{utils → src/utils}/createTypedArgs.js +0 -0
  152. /package/dist/production/{utils → src/utils}/mutationFragments.cjs +0 -0
  153. /package/dist/production/{utils → src/utils}/mutationFragments.js +0 -0
@@ -0,0 +1,519 @@
1
+ import { describe, expect, test } from 'vitest'
2
+ import { createClient } from '../../lib'
3
+ import { Input } from '@contember/schema'
4
+ import OrderDirection = Input.OrderDirection
5
+ import { queryBuilder } from '../../client'
6
+
7
+ describe('queries', () => {
8
+
9
+ const qb = queryBuilder
10
+ test('list', async () => {
11
+ const [client, calls] = createClient({
12
+ authors: [
13
+ {
14
+ name: 'John',
15
+ email: 'foo@localhost',
16
+ },
17
+ {
18
+ name: 'John',
19
+ email: 'bar@localhost',
20
+ },
21
+ ],
22
+ })
23
+ const result = await client.query({
24
+ authors: qb.list('Author', {}, it => it.$$()),
25
+ })
26
+ expect(result).toEqual({
27
+ authors: [
28
+ {
29
+ name: 'John',
30
+ email: 'foo@localhost',
31
+ },
32
+ {
33
+ name: 'John',
34
+ email: 'bar@localhost',
35
+ },
36
+ ],
37
+ })
38
+ expect(calls).toHaveLength(1)
39
+ expect(calls[0].query).toMatchInlineSnapshot(`
40
+ "query {
41
+ authors: listAuthor {
42
+ id
43
+ name
44
+ email
45
+ }
46
+ }
47
+ "
48
+ `)
49
+ expect(calls[0].variables).toMatchInlineSnapshot('{}')
50
+ })
51
+
52
+ test('multiple queries', async () => {
53
+ const [client, calls] = createClient({
54
+ authors: [
55
+ {
56
+ name: 'John',
57
+ email: 'foo@localhost',
58
+ },
59
+ {
60
+ name: 'John',
61
+ email: 'bar@localhost',
62
+ },
63
+ ],
64
+ posts: [
65
+ {
66
+ title: 'Post 1',
67
+ content: 'Content 1',
68
+ },
69
+ {
70
+ title: 'Post 2',
71
+ content: 'Content 2',
72
+ },
73
+ ],
74
+ })
75
+ const result = await client.query({
76
+ authors: qb.list('Author', {}, it => it.$$()),
77
+ posts: qb.list('Post', {}, it => it.$$()),
78
+ })
79
+ expect(result).toEqual({
80
+ authors: [
81
+ {
82
+ name: 'John',
83
+ email: 'foo@localhost',
84
+ },
85
+ {
86
+ name: 'John',
87
+ email: 'bar@localhost',
88
+ },
89
+ ],
90
+ posts: [
91
+ {
92
+ title: 'Post 1',
93
+ content: 'Content 1',
94
+ },
95
+ {
96
+ title: 'Post 2',
97
+ content: 'Content 2',
98
+ },
99
+ ],
100
+ })
101
+ expect(calls).toHaveLength(1)
102
+ expect(calls[0].query).toMatchInlineSnapshot(`
103
+ "query {
104
+ authors: listAuthor {
105
+ id
106
+ name
107
+ email
108
+ }
109
+ posts: listPost {
110
+ id
111
+ publishedAt
112
+ }
113
+ }
114
+ "
115
+ `)
116
+ expect(calls[0].variables).toMatchInlineSnapshot('{}')
117
+ })
118
+
119
+
120
+ test('single query', async () => {
121
+ const [client, calls] = createClient({
122
+ value: [
123
+ {
124
+ title: 'Post 1',
125
+ content: 'Content 1',
126
+ },
127
+ {
128
+ title: 'Post 2',
129
+ content: 'Content 2',
130
+ },
131
+ ],
132
+ })
133
+
134
+ const result = await client.query(qb.list('Post', {}, it => it.$$()))
135
+ expect(result).toEqual([
136
+ {
137
+ title: 'Post 1',
138
+ content: 'Content 1',
139
+ },
140
+ {
141
+ title: 'Post 2',
142
+ content: 'Content 2',
143
+ },
144
+ ])
145
+ expect(calls).toHaveLength(1)
146
+ expect(calls[0].query).toMatchInlineSnapshot(`
147
+ "query {
148
+ value: listPost {
149
+ id
150
+ publishedAt
151
+ }
152
+ }
153
+ "
154
+ `)
155
+ expect(calls[0].variables).toMatchInlineSnapshot('{}')
156
+ })
157
+
158
+
159
+ test('nested object', async () => {
160
+ const [client, calls] = createClient()
161
+ await client.query({
162
+ authors: qb.list('Author', {}, it => it.$$().$('posts', {}, it => it.$$().$('tags', {}, it => it.$$()))),
163
+ })
164
+ expect(calls).toHaveLength(1)
165
+ expect(calls[0].query).toMatchInlineSnapshot(`
166
+ "query {
167
+ authors: listAuthor {
168
+ id
169
+ name
170
+ email
171
+ posts {
172
+ id
173
+ publishedAt
174
+ tags {
175
+ id
176
+ name
177
+ }
178
+ }
179
+ }
180
+ }
181
+ "
182
+ `)
183
+ expect(calls[0].variables).toMatchInlineSnapshot('{}')
184
+ })
185
+ test('nested object args', async () => {
186
+ const [client, calls] = createClient()
187
+ await client.query({
188
+ authors: qb.list('Author', {}, it => it.$$().$('posts', {
189
+ limit: 10,
190
+ filter: { tags: { name: { eq: 'foo' } } },
191
+ }, it => it.$$())),
192
+ })
193
+ expect(calls).toHaveLength(1)
194
+ expect(calls[0].query).toMatchInlineSnapshot(`
195
+ "query($PostWhere_0: PostWhere, $Int_1: Int) {
196
+ authors: listAuthor {
197
+ id
198
+ name
199
+ email
200
+ posts(filter: $PostWhere_0, limit: $Int_1) {
201
+ id
202
+ publishedAt
203
+ }
204
+ }
205
+ }
206
+ "
207
+ `)
208
+ expect(calls[0].variables).toMatchInlineSnapshot(`
209
+ {
210
+ "Int_1": 10,
211
+ "PostWhere_0": {
212
+ "tags": {
213
+ "name": {
214
+ "eq": "foo",
215
+ },
216
+ },
217
+ },
218
+ }
219
+ `)
220
+ })
221
+
222
+
223
+ test('list with args', async () => {
224
+ const [client, calls] = createClient()
225
+ await client.query({
226
+ authors: qb.list('Author', {
227
+ filter: { name: { eq: 'John' } },
228
+ orderBy: [{ name: OrderDirection.asc }],
229
+ limit: 10,
230
+ offset: 20,
231
+ }, it => it.$$()),
232
+ })
233
+ expect(calls).toHaveLength(1)
234
+ expect(calls[0].query).toMatchInlineSnapshot(`
235
+ "query($AuthorWhere_0: AuthorWhere, $AuthorOrderBy_1: [AuthorOrderBy!], $Int_2: Int, $Int_3: Int) {
236
+ authors: listAuthor(filter: $AuthorWhere_0, orderBy: $AuthorOrderBy_1, limit: $Int_2, offset: $Int_3) {
237
+ id
238
+ name
239
+ email
240
+ }
241
+ }
242
+ "
243
+ `)
244
+ expect(calls[0].variables).toMatchInlineSnapshot(`
245
+ {
246
+ "AuthorOrderBy_1": [
247
+ {
248
+ "name": "asc",
249
+ },
250
+ ],
251
+ "AuthorWhere_0": {
252
+ "name": {
253
+ "eq": "John",
254
+ },
255
+ },
256
+ "Int_2": 10,
257
+ "Int_3": 20,
258
+ }
259
+ `)
260
+ })
261
+
262
+
263
+ test('get by id', async () => {
264
+ const [client, calls] = createClient()
265
+ const result = await client.query({
266
+ authors: qb.get('Author', {
267
+ by: { id: 'ca7a9b84-efbb-435d-a063-da11f205335a' },
268
+ }, it => it.$$()),
269
+ })
270
+ expect(calls).toHaveLength(1)
271
+ expect(calls[0].query).toMatchInlineSnapshot(`
272
+ "query($AuthorUniqueWhere_0: AuthorUniqueWhere!) {
273
+ authors: getAuthor(by: $AuthorUniqueWhere_0) {
274
+ id
275
+ name
276
+ email
277
+ }
278
+ }
279
+ "
280
+ `)
281
+ expect(calls[0].variables).toMatchInlineSnapshot(`
282
+ {
283
+ "AuthorUniqueWhere_0": {
284
+ "id": "ca7a9b84-efbb-435d-a063-da11f205335a",
285
+ },
286
+ }
287
+ `)
288
+ })
289
+
290
+ test('top level list transform', async () => {
291
+ const [client, calls] = createClient({
292
+ authors: [
293
+ {
294
+ name: 'John',
295
+ email: 'foo@localhost',
296
+ },
297
+ {
298
+ name: 'Jane',
299
+ email: 'bar@localhost',
300
+ },
301
+ ],
302
+ })
303
+ const result = await client.query({
304
+ authors: qb.list('Author', {}, it => it
305
+ .$$()
306
+ .transform(it => ({
307
+ ...it,
308
+ label: `${it.name} (${it.email})`,
309
+ })),
310
+ ),
311
+ })
312
+ expect(result).toEqual({
313
+ authors: [
314
+ {
315
+ name: 'John',
316
+ email: 'foo@localhost',
317
+ label: 'John (foo@localhost)',
318
+ },
319
+ {
320
+ name: 'Jane',
321
+ email: 'bar@localhost',
322
+ label: 'Jane (bar@localhost)',
323
+ },
324
+ ],
325
+ })
326
+ expect(calls).toHaveLength(1)
327
+ })
328
+
329
+ test('top level get transform', async () => {
330
+ const [client, calls] = createClient({
331
+ author: {
332
+ name: 'John',
333
+ email: 'foo@localhost',
334
+ },
335
+ })
336
+ const result = await client.query({
337
+ author: qb.get('Author', { by: { id: '123' } }, it => it
338
+ .$$()
339
+ .transform(it => ({
340
+ ...it,
341
+ label: `${it.name} (${it.email})`,
342
+ })),
343
+ ),
344
+ })
345
+ expect(result).toEqual({
346
+ author: {
347
+ name: 'John',
348
+ email: 'foo@localhost',
349
+ label: 'John (foo@localhost)',
350
+ },
351
+ })
352
+ expect(calls).toHaveLength(1)
353
+ })
354
+
355
+
356
+ test('nested has-many transform', async () => {
357
+ const [client, calls] = createClient({
358
+ author: {
359
+ name: 'John',
360
+ email: 'foo@localhost',
361
+ posts: [
362
+ {
363
+ publishedAt: '2021-01-01T00:00:00Z',
364
+ },
365
+ {
366
+ publishedAt: '2021-01-02T00:00:00Z',
367
+ },
368
+ ],
369
+ },
370
+ })
371
+ const result = await client.query({
372
+ author: qb.get('Author', { by: { id: '123' } }, it => it
373
+ .$$()
374
+ .$('posts', {}, it => it.$$().transform(it => ({
375
+ ...it,
376
+ publishYear: it.publishedAt ? new Date(it.publishedAt).getFullYear() : null,
377
+ }))),
378
+ ),
379
+ })
380
+ expect(result).toEqual({
381
+ author: {
382
+ name: 'John',
383
+ email: 'foo@localhost',
384
+ posts: [
385
+ {
386
+ publishedAt: '2021-01-01T00:00:00Z',
387
+ publishYear: 2021,
388
+ },
389
+ {
390
+ publishedAt: '2021-01-02T00:00:00Z',
391
+ publishYear: 2021,
392
+ },
393
+ ],
394
+ },
395
+ })
396
+ expect(calls).toHaveLength(1)
397
+ })
398
+
399
+
400
+ test('nested has-one transform', async () => {
401
+ const [client, calls] = createClient({
402
+ post: {
403
+ publishedAt: '2021-01-01T00:00:00Z',
404
+ author: {
405
+ name: 'John',
406
+ email: 'foo@localhost',
407
+ },
408
+ },
409
+ })
410
+ const author = qb.fragment('Author', it => it
411
+ .$$()
412
+ .transform(it => ({
413
+ ...it,
414
+ label: `${it.name} (${it.email})`,
415
+ })),
416
+ )
417
+
418
+ const result = await client.query({
419
+ post: qb.get('Post', { by: { id: '123' } }, it => it
420
+ .$$()
421
+ .$('author', author),
422
+ ),
423
+ })
424
+ expect(result).toEqual({
425
+ post: {
426
+ publishedAt: '2021-01-01T00:00:00Z',
427
+ author: {
428
+ name: 'John',
429
+ email: 'foo@localhost',
430
+ label: 'John (foo@localhost)',
431
+ },
432
+ },
433
+ })
434
+ })
435
+
436
+
437
+ test('nested has-many-by transform', async () => {
438
+ const [client, calls] = createClient({
439
+ post: {
440
+ publishedAt: '2021-01-01T00:00:00Z',
441
+ localesByLocale: {
442
+ title: 'Hello',
443
+ },
444
+ },
445
+ })
446
+ const result = await client.query({
447
+ post: qb.get('Post', { by: { id: '123' } }, it => it
448
+ .$$()
449
+ .$('localesByLocale', { by: { locale: { code: 'en' } } }, it => it.$$().transform(it => ({
450
+ ...it,
451
+ label: it.title?.toUpperCase(),
452
+ }))),
453
+ ),
454
+ })
455
+ expect(result).toEqual({
456
+ post: {
457
+ publishedAt: '2021-01-01T00:00:00Z',
458
+ localesByLocale: {
459
+ title: 'Hello',
460
+ label: 'HELLO',
461
+ },
462
+ },
463
+ })
464
+ })
465
+
466
+ test('multiple transforms', async () => {
467
+ const [client, calls] = createClient({
468
+ author: {
469
+ name: 'John',
470
+ email: 'foo@localhost',
471
+ posts: [
472
+ {
473
+ publishedAt: '2021-01-01T00:00:00Z',
474
+ },
475
+ {
476
+ publishedAt: '2021-01-02T00:00:00Z',
477
+ },
478
+ ],
479
+ },
480
+ })
481
+ const result = await client.query({
482
+ author: qb.get('Author', { by: { id: '123' } }, it => it
483
+ .$$()
484
+ .transform(it => ({
485
+ ...it,
486
+ value1: 'foo',
487
+ }))
488
+ .$('posts', {}, it => it.$$().transform(it => ({
489
+ ...it,
490
+ postLabel: 'foo bar',
491
+ })))
492
+ .transform(it => ({
493
+ ...it,
494
+ value2: 'bar',
495
+ })),
496
+ ),
497
+ })
498
+ expect(result).toEqual({
499
+ author: {
500
+ name: 'John',
501
+ email: 'foo@localhost',
502
+ value1: 'foo',
503
+ value2: 'bar',
504
+ posts: [
505
+ {
506
+ publishedAt: '2021-01-01T00:00:00Z',
507
+ postLabel: 'foo bar',
508
+ },
509
+ {
510
+ publishedAt: '2021-01-02T00:00:00Z',
511
+ postLabel: 'foo bar',
512
+ },
513
+ ],
514
+ },
515
+ })
516
+ expect(calls).toHaveLength(1)
517
+ })
518
+
519
+ })
@@ -0,0 +1,144 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ import { assertType, describe, expect, expectTypeOf, test } from 'vitest'
3
+ import { ContentClientInput } from '../../../src'
4
+ import { ContemberClientEntities, FragmentOf, FragmentType, queryBuilder } from '../../client'
5
+
6
+ const qb = queryBuilder
7
+ describe('ts types', () => {
8
+ test('fragment - valid', async () => {
9
+ expectTypeOf(
10
+ qb.fragment('Post')
11
+ .$('author', qb.fragment('Author')),
12
+ ).toMatchTypeOf<FragmentOf<'Post'>>()
13
+ })
14
+
15
+ test('fragment - invalid', async () => {
16
+ const postFragment = qb.fragment('Post')
17
+
18
+ // @ts-expect-error
19
+ assertType(postFragment.$('author', qb.fragment('Post')))
20
+ })
21
+
22
+ test('where - valid', async () => {
23
+ qb.list('Post', {
24
+ filter: {
25
+ publishedAt: { eq: 'foo' },
26
+ },
27
+ }, it => it.$$())
28
+ })
29
+
30
+ test('where - invalid with literal', async () => {
31
+ qb.list('Post', {
32
+ filter: {
33
+ // @ts-expect-error
34
+ caption: { eq: 'foo' },
35
+ },
36
+ }, it => it.$$())
37
+ })
38
+
39
+ test('where - invalid assignment to type', async () => {
40
+ const where: ContentClientInput.Where<ContemberClientEntities['Post']> = {
41
+ // @ts-expect-error
42
+ caption: { eq: 'foo' },
43
+ }
44
+ })
45
+
46
+ test('where - valid extracted without a type', async () => {
47
+ const where = {
48
+ publishedAt: { eq: 'foo' },
49
+ }
50
+ qb.list('Post', {
51
+ filter: where,
52
+ }, it => it.$$())
53
+ })
54
+
55
+ test('where - valid extracted with a type', async () => {
56
+ const where: ContentClientInput.Where<ContemberClientEntities['Post']> = {
57
+ publishedAt: { eq: 'foo' },
58
+ }
59
+ qb.list('Post', {
60
+ filter: where,
61
+ }, it => it.$$())
62
+ })
63
+
64
+ test('where - incompatible types', async () => {
65
+ const where: ContentClientInput.Where<ContemberClientEntities['Post']> = {
66
+ publishedAt: { eq: 'foo' },
67
+ }
68
+ qb.list('Author', {
69
+ // @ts-expect-error
70
+ filter: where,
71
+ }, it => it.$$())
72
+ })
73
+
74
+ test('has one field on fragment type', async () => {
75
+ const fragment = qb.fragment('Post', it => it.$('author', it => it.$$().$('posts', it => it.$('id'))))
76
+ type fragmentType = FragmentType<typeof fragment>
77
+ expectTypeOf<fragmentType>().toEqualTypeOf<{ author: ({ id: string; name: string | null; email: string | null } & { posts: { id: string }[] }) | null }>()
78
+ })
79
+
80
+
81
+ test('has one without arg', async () => {
82
+ qb.list('Post', {}, it => it.$('author', it => it.$$()))
83
+ })
84
+
85
+ test('has one with arg', async () => {
86
+ qb.list('Post', {}, it => it.$('author', { filter: { name: { eq: 'John' } } }, it => it.$$()))
87
+ })
88
+
89
+ test('has one with invalid arg', async () => {
90
+ qb.list('Post', {}, it => it
91
+ .$('author',
92
+ // @ts-expect-error
93
+ { filter: { foo: { eq: 'xx' } } },
94
+ it => it.$$(),
95
+ ),
96
+ )
97
+ })
98
+
99
+ test('has many without arg', async () => {
100
+ qb.list('Post', {}, it => it.$('locales', it => it.$$()))
101
+ })
102
+
103
+ test('has many with arg', async () => {
104
+ qb.list('Post', {}, it => it.$('locales', { filter: { locale: { code: { eq: 'cs' } } } }, it => it.$$()))
105
+ })
106
+
107
+ test('has many with invalid arg', async () => {
108
+ qb.list('Post', {}, it => it
109
+ .$('locales',
110
+ // @ts-expect-error
111
+ { filter: { locale: { eq: 'cs' } } },
112
+ it => it.$$(),
113
+ ),
114
+ )
115
+ })
116
+
117
+ test('reduced has may', async () => {
118
+ qb.list('Post', {}, it => it.$('localesByLocale', { by: { locale: { code: 'cs' } } }, it => it.$$()))
119
+ })
120
+
121
+ test('reduced has may - invalid "by"', async () => {
122
+ qb.list('Post',
123
+ {},
124
+ it => it
125
+ .$('localesByLocale',
126
+ // @ts-expect-error
127
+ { by: { locale: {} } },
128
+ qb.fragment('PostLocale'),
129
+ ),
130
+ )
131
+ })
132
+
133
+
134
+ test('invalid field', async () => {
135
+ expect(() => {
136
+ qb.list(
137
+ 'Post',
138
+ {},
139
+ // @ts-expect-error
140
+ it => it.$('lorem', it => it.$$()),
141
+ )
142
+ }).toThrow('Field Post.lorem does not exist.')
143
+ })
144
+ })