@contember/client-content 2.2.0-alpha.2 → 2.2.0-alpha.20

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 (30) hide show
  1. package/dist/development/src/nodes/ContentEntitySelection.cjs +60 -26
  2. package/dist/development/src/nodes/ContentEntitySelection.cjs.map +1 -1
  3. package/dist/development/src/nodes/ContentEntitySelection.js +60 -26
  4. package/dist/development/src/nodes/ContentEntitySelection.js.map +1 -1
  5. package/dist/production/src/nodes/ContentEntitySelection.cjs +60 -26
  6. package/dist/production/src/nodes/ContentEntitySelection.cjs.map +1 -1
  7. package/dist/production/src/nodes/ContentEntitySelection.js +60 -26
  8. package/dist/production/src/nodes/ContentEntitySelection.js.map +1 -1
  9. package/dist/types/ContentClient.d.ts.map +1 -1
  10. package/dist/types/ContentQueryBuilder.d.ts.map +1 -1
  11. package/dist/types/MutationFailedError.d.ts.map +1 -1
  12. package/dist/types/nodes/ContentEntitySelection.d.ts +6 -5
  13. package/dist/types/nodes/ContentEntitySelection.d.ts.map +1 -1
  14. package/dist/types/nodes/ContentOperation.d.ts.map +1 -1
  15. package/dist/types/tsconfig.tsbuildinfo +1 -1
  16. package/dist/types/types/Input.d.ts +6 -1
  17. package/dist/types/types/Input.d.ts.map +1 -1
  18. package/dist/types/utils/ContentOperationSet.d.ts.map +1 -1
  19. package/dist/types/utils/createListArgs.d.ts +1 -1
  20. package/dist/types/utils/createListArgs.d.ts.map +1 -1
  21. package/dist/types/utils/createMutationOperationSet.d.ts.map +1 -1
  22. package/dist/types/utils/createQueryOperationSet.d.ts.map +1 -1
  23. package/dist/types/utils/createTypedArgs.d.ts.map +1 -1
  24. package/package.json +5 -6
  25. package/src/nodes/ContentEntitySelection.ts +66 -26
  26. package/src/types/Input.ts +13 -1
  27. package/tests/cases/unit/mutation.test.ts +2 -2
  28. package/tests/cases/unit/query.test.ts +243 -0
  29. package/tests/cases/unit/types.test.ts +16 -9
  30. package/tests/typeAssertions.ts +5 -0
@@ -36,7 +36,12 @@ export type ContentTransformContext = {
36
36
  rootValue: unknown
37
37
  }
38
38
 
39
+ export type ContentTransform = (value: any, ctx: ContentTransformContext) => any
40
+
39
41
  export class ContentEntitySelection {
42
+ /** @internal */
43
+ public readonly transformFn?: ContentTransform
44
+
40
45
  /**
41
46
  * @internal
42
47
  */
@@ -45,9 +50,10 @@ export class ContentEntitySelection {
45
50
  public readonly context: ContentEntitySelectionContext<string>,
46
51
  /** @internal */
47
52
  public readonly selectionSet: GraphQlSelectionSet,
48
- /** @internal */
49
- public readonly transformFn?: (value: any, ctx: ContentTransformContext) => any,
53
+ private readonly valueTransform?: ContentTransform,
54
+ private readonly fieldTransforms: ReadonlyMap<string, ContentTransform> = new Map(),
50
55
  ) {
56
+ this.transformFn = composeTransform(fieldTransforms, valueTransform)
51
57
  }
52
58
 
53
59
  $(field: string, args?: EntitySelectionColumnArgs): ContentEntitySelection
@@ -102,10 +108,14 @@ export class ContentEntitySelection {
102
108
  ...this.selectionSet,
103
109
  ...columns.map(col => new GraphQlField(null, col)),
104
110
  ]
105
- return new ContentEntitySelection(this.context, nodes)
111
+ return new ContentEntitySelection(this.context, nodes, this.valueTransform, this.fieldTransforms)
106
112
  }
107
113
 
108
114
  omit(...fields: string[]): ContentEntitySelection {
115
+ const fieldTransforms = new Map(this.fieldTransforms)
116
+ for (const field of fields) {
117
+ fieldTransforms.delete(field)
118
+ }
109
119
  return new ContentEntitySelection(
110
120
  this.context,
111
121
  this.selectionSet.filter(it => {
@@ -115,6 +125,8 @@ export class ContentEntitySelection {
115
125
  const alias = it.alias ?? it.name
116
126
  return !fields.includes(alias)
117
127
  }),
128
+ this.valueTransform,
129
+ fieldTransforms,
118
130
  )
119
131
  }
120
132
 
@@ -127,22 +139,29 @@ export class ContentEntitySelection {
127
139
  flagFields.push(new GraphQlField(null, flag))
128
140
  }
129
141
 
130
- return new ContentEntitySelection(this.context, [
131
- ...selectionWithoutMeta,
132
- new GraphQlField(null, '_meta', undefined, [
133
- ...metaField?.selectionSet ?? [],
134
- new GraphQlField(null, field, undefined, flagFields),
135
- ]),
136
- ])
142
+ return new ContentEntitySelection(
143
+ this.context,
144
+ [
145
+ ...selectionWithoutMeta,
146
+ new GraphQlField(null, '_meta', undefined, [
147
+ ...metaField?.selectionSet ?? [],
148
+ new GraphQlField(null, field, undefined, flagFields),
149
+ ]),
150
+ ],
151
+ this.valueTransform,
152
+ this.fieldTransforms,
153
+ )
137
154
  }
138
155
 
139
- transform(transform: (value: any, context: ContentTransformContext) => any): ContentEntitySelection {
156
+ transform(transform: ContentTransform): ContentEntitySelection {
157
+ const previousTransform = this.valueTransform
140
158
  return new ContentEntitySelection(
141
159
  this.context,
142
160
  this.selectionSet,
143
- !this.transformFn ? transform : (value, ctx) => {
144
- return transform(this.transformFn!(value, ctx), ctx)
161
+ !previousTransform ? transform : (value, ctx) => {
162
+ return transform(previousTransform(value, ctx), ctx)
145
163
  },
164
+ this.fieldTransforms,
146
165
  )
147
166
  }
148
167
 
@@ -322,20 +341,41 @@ export class ContentEntitySelection {
322
341
  }
323
342
 
324
343
  private withField(field: GraphQlField) {
325
- return new ContentEntitySelection(this.context, [
326
- ...this.selectionSet,
327
- field,
328
- ], this.transformFn)
344
+ return new ContentEntitySelection(
345
+ this.context,
346
+ [
347
+ ...this.selectionSet,
348
+ field,
349
+ ],
350
+ this.valueTransform,
351
+ this.fieldTransforms,
352
+ )
329
353
  }
330
354
 
331
- private withFieldTransform(alias: string, transform: (value: any, ctx: ContentTransformContext) => any) {
332
- return new ContentEntitySelection(this.context, this.selectionSet, (value, ctx) => {
333
- const transformedValue = transform(value[alias], ctx)
334
- const newValue = {
335
- ...value,
336
- [alias]: transformedValue,
337
- }
338
- return this.transformFn ? this.transformFn(newValue, ctx) : newValue
339
- })
355
+ private withFieldTransform(alias: string, transform: ContentTransform) {
356
+ // A relation selected twice under one alias is merged by the server, so both nested transforms apply.
357
+ const previousTransform = this.fieldTransforms.get(alias)
358
+ const fieldTransform: ContentTransform = !previousTransform ? transform : (value, ctx) => {
359
+ return previousTransform(transform(value, ctx), ctx)
360
+ }
361
+ const fieldTransforms = new Map(this.fieldTransforms).set(alias, fieldTransform)
362
+ return new ContentEntitySelection(this.context, this.selectionSet, this.valueTransform, fieldTransforms)
363
+ }
364
+ }
365
+
366
+ // Field transforms see the raw response; transforms from .transform() run after all of them.
367
+ const composeTransform = (
368
+ fieldTransforms: ReadonlyMap<string, ContentTransform>,
369
+ valueTransform: ContentTransform | undefined,
370
+ ): ContentTransform | undefined => {
371
+ if (fieldTransforms.size === 0) {
372
+ return valueTransform
373
+ }
374
+ return (value, ctx) => {
375
+ const newValue = { ...value }
376
+ for (const [alias, transform] of fieldTransforms) {
377
+ newValue[alias] = transform(value[alias], ctx)
378
+ }
379
+ return valueTransform ? valueTransform(newValue, ctx) : newValue
340
380
  }
341
381
  }
@@ -213,7 +213,19 @@ export namespace ContentClientInput {
213
213
  | UpdateSpecifiedRelationInput<TEntity>
214
214
  | UpsertSpecifiedRelationInput<TEntity>
215
215
 
216
- export type UpdateManyRelationInput<TEntity extends EntityTypeLike> = Array<UpdateManyRelationInputItem<TEntity>>
216
+ export type SetManyRelationInputItem<TEntity extends EntityTypeLike> =
217
+ | CreateRelationInput<TEntity>
218
+ | ConnectRelationInput<TEntity>
219
+ | ConnectOrCreateRelationInput<TEntity>
220
+ | UpdateSpecifiedRelationInput<TEntity>
221
+ | UpsertSpecifiedRelationInput<TEntity>
222
+
223
+ export interface SetManyRelationInput<TEntity extends EntityTypeLike> {
224
+ readonly set: ReadonlyArray<SetManyRelationInputItem<TEntity>>
225
+ readonly orphanStrategy?: `${Input.OrphanRemovalStrategy}`
226
+ }
227
+
228
+ export type UpdateManyRelationInput<TEntity extends EntityTypeLike> = Array<UpdateManyRelationInputItem<TEntity> | SetManyRelationInput<TEntity>>
217
229
 
218
230
  export type FieldOrderBy<TEntity extends EntityTypeLike> =
219
231
  & {
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
2
  import { createClient } from '../../lib.js'
3
3
  import { queryBuilder } from '../../client/index.js'
4
- import { expectTypeOf } from 'expect-type'
4
+ import { assertType, Equal } from '../../typeAssertions.js'
5
5
 
6
6
  const qb = queryBuilder
7
7
  describe('mutations in trx', () => {
@@ -557,7 +557,7 @@ describe('mutations without trx', () => {
557
557
  const result = await client.mutateOrThrow(createAuthor)
558
558
  expect(result.ok).toBe(true)
559
559
  const ok = result.ok
560
- expectTypeOf(ok).toEqualTypeOf<true>()
560
+ assertType<Equal<typeof ok, true>>()
561
561
 
562
562
  expect(calls).toHaveLength(1)
563
563
  expect(calls[0].query).toMatchSnapshot()
@@ -444,6 +444,249 @@ describe('queries', () => {
444
444
  expect(calls).toHaveLength(1)
445
445
  })
446
446
 
447
+ test('meta after has-many keeps the connection unwrapped', async () => {
448
+ const [client, calls] = createClient({
449
+ author: {
450
+ name: 'John',
451
+ posts: {
452
+ edges: [
453
+ { node: { publishedAt: '2021-01-01T00:00:00Z' } },
454
+ ],
455
+ },
456
+ _meta: {
457
+ name: { readable: true, updatable: false },
458
+ },
459
+ },
460
+ })
461
+ const result = await client.query({
462
+ author: qb.get('Author', { by: { id: '123' } }, it =>
463
+ it
464
+ .$('name')
465
+ .$('posts', {}, it => it.$('publishedAt'))
466
+ .meta('name', ['readable', 'updatable'])),
467
+ })
468
+ expect(result as any).toStrictEqual({
469
+ author: {
470
+ name: 'John',
471
+ posts: [
472
+ { publishedAt: '2021-01-01T00:00:00Z' },
473
+ ],
474
+ _meta: {
475
+ name: { readable: true, updatable: false },
476
+ },
477
+ },
478
+ })
479
+ expect(calls).toHaveLength(1)
480
+ })
481
+
482
+ test('omit after has-many keeps the connection unwrapped', async () => {
483
+ const [client] = createClient({
484
+ author: {
485
+ posts: {
486
+ edges: [
487
+ { node: { publishedAt: '2021-01-01T00:00:00Z' } },
488
+ ],
489
+ },
490
+ },
491
+ })
492
+ const result = await client.query({
493
+ author: qb.get('Author', { by: { id: '123' } }, it =>
494
+ it
495
+ .$('name')
496
+ .$('posts', {}, it => it.$('publishedAt'))
497
+ .omit('name')),
498
+ })
499
+ expect(result as any).toStrictEqual({
500
+ author: {
501
+ posts: [
502
+ { publishedAt: '2021-01-01T00:00:00Z' },
503
+ ],
504
+ },
505
+ })
506
+ })
507
+
508
+ test('omit of has-many drops its transform', async () => {
509
+ const [client, calls] = createClient({
510
+ author: {
511
+ name: 'John',
512
+ },
513
+ })
514
+ const result = await client.query({
515
+ author: qb.get('Author', { by: { id: '123' } }, it =>
516
+ it
517
+ .$('name')
518
+ .$('posts', {}, it => it.$('publishedAt'))
519
+ .omit('posts')),
520
+ })
521
+ expect(result as any).toStrictEqual({
522
+ author: {
523
+ name: 'John',
524
+ },
525
+ })
526
+ expect(calls[0].query).not.toContain('paginatePosts')
527
+ })
528
+
529
+ test('$$ after has-many keeps the connection unwrapped', async () => {
530
+ const [client] = createClient({
531
+ author: {
532
+ name: 'John',
533
+ email: 'foo@localhost',
534
+ posts: {
535
+ edges: [
536
+ { node: { publishedAt: '2021-01-01T00:00:00Z' } },
537
+ ],
538
+ },
539
+ },
540
+ })
541
+ const result = await client.query({
542
+ author: qb.get('Author', { by: { id: '123' } }, it =>
543
+ it
544
+ .$('posts', {}, it => it.$('publishedAt'))
545
+ .$$()),
546
+ })
547
+ expect(result as any).toStrictEqual({
548
+ author: {
549
+ name: 'John',
550
+ email: 'foo@localhost',
551
+ posts: [
552
+ { publishedAt: '2021-01-01T00:00:00Z' },
553
+ ],
554
+ },
555
+ })
556
+ })
557
+
558
+ test('has-one selected twice keeps both nested transforms', async () => {
559
+ const [client] = createClient({
560
+ post: {
561
+ author: {
562
+ name: 'John',
563
+ posts: {
564
+ edges: [
565
+ { node: { publishedAt: '2021-01-01T00:00:00Z' } },
566
+ ],
567
+ },
568
+ },
569
+ },
570
+ })
571
+ const result = await client.query({
572
+ post: qb.get('Post', { by: { id: '123' } }, it =>
573
+ it
574
+ .$('author', {}, it => it.$('posts', {}, it => it.$('publishedAt')))
575
+ .$('author', {}, it =>
576
+ it.$('name').transform(it => ({
577
+ ...it,
578
+ label: `by ${it.name}`,
579
+ })))),
580
+ })
581
+ expect(result as any).toStrictEqual({
582
+ post: {
583
+ author: {
584
+ name: 'John',
585
+ label: 'by John',
586
+ posts: [
587
+ { publishedAt: '2021-01-01T00:00:00Z' },
588
+ ],
589
+ },
590
+ },
591
+ })
592
+ })
593
+
594
+ test('transforms around meta and omit are kept', async () => {
595
+ const [client] = createClient({
596
+ author: {
597
+ name: 'John',
598
+ _meta: {
599
+ name: { readable: true },
600
+ },
601
+ },
602
+ })
603
+ const result = await client.query({
604
+ author: qb.get('Author', { by: { id: '123' } }, it =>
605
+ it
606
+ .$('name')
607
+ .$('email')
608
+ .transform(it => ({ ...it, before: true }))
609
+ .meta('name', ['readable'])
610
+ .omit('email')
611
+ .transform(it => ({ ...it, after: true }))),
612
+ })
613
+ expect(result as any).toStrictEqual({
614
+ author: {
615
+ name: 'John',
616
+ _meta: {
617
+ name: { readable: true },
618
+ },
619
+ before: true,
620
+ after: true,
621
+ },
622
+ })
623
+ })
624
+
625
+ test('omit inside a nested has-many keeps the inner connection unwrapped', async () => {
626
+ const [client] = createClient({
627
+ author: {
628
+ posts: {
629
+ edges: [
630
+ {
631
+ node: {
632
+ tags: {
633
+ edges: [
634
+ { node: { name: 'news' } },
635
+ ],
636
+ },
637
+ },
638
+ },
639
+ ],
640
+ },
641
+ },
642
+ })
643
+ const result = await client.query({
644
+ author: qb.get('Author', { by: { id: '123' } }, it =>
645
+ it.$('posts', {}, it =>
646
+ it
647
+ .$('publishedAt')
648
+ .$('tags', {}, it => it.$('name'))
649
+ .omit('publishedAt'))),
650
+ })
651
+ expect(result as any).toStrictEqual({
652
+ author: {
653
+ posts: [
654
+ {
655
+ tags: [
656
+ { name: 'news' },
657
+ ],
658
+ },
659
+ ],
660
+ },
661
+ })
662
+ })
663
+
664
+ test('omit keeps an aliased has-many unwrapped', async () => {
665
+ const [client] = createClient({
666
+ author: {
667
+ articles: {
668
+ edges: [
669
+ { node: { publishedAt: '2021-01-01T00:00:00Z' } },
670
+ ],
671
+ },
672
+ },
673
+ })
674
+ const result = await client.query({
675
+ author: qb.get('Author', { by: { id: '123' } }, it =>
676
+ it
677
+ .$('name')
678
+ .$('posts', { as: 'articles' }, it => it.$('publishedAt'))
679
+ .omit('name')),
680
+ })
681
+ expect(result as any).toStrictEqual({
682
+ author: {
683
+ articles: [
684
+ { publishedAt: '2021-01-01T00:00:00Z' },
685
+ ],
686
+ },
687
+ })
688
+ })
689
+
447
690
  test('omit', async () => {
448
691
  const [client, calls] = createClient({
449
692
  authors: [
@@ -2,15 +2,16 @@
2
2
  import { describe, expect, test } from 'bun:test'
3
3
  import { ContentClientInput } from '../../../src/index.js'
4
4
  import { ContemberClientEntities, FragmentOf, FragmentType, PostStatus, queryBuilder } from '../../client/index.js'
5
- import { expectTypeOf } from 'expect-type'
5
+ import { assertType, Equal } from '../../typeAssertions.js'
6
+ import { Input, JSONValue, Value } from '@contember/schema'
6
7
 
7
8
  const qb = queryBuilder
8
9
  describe('ts types', () => {
9
10
  test('fragment - valid', async () => {
10
- expectTypeOf(
11
- qb.fragment('Post')
12
- .$('author', qb.fragment('Author')),
13
- ).toMatchTypeOf<FragmentOf<'Post'>>()
11
+ const fragment = qb.fragment('Post')
12
+ .$('author', qb.fragment('Author'))
13
+ const asPostFragment: FragmentOf<'Post'> = fragment
14
+ expect(asPostFragment).toBe(fragment)
14
15
  })
15
16
 
16
17
  test('fragment - invalid', async () => {
@@ -75,9 +76,7 @@ describe('ts types', () => {
75
76
  test('has one field on fragment type', async () => {
76
77
  const fragment = qb.fragment('Post', it => it.$('author', it => it.$$().$('posts', it => it.$('id'))))
77
78
  type fragmentType = FragmentType<typeof fragment>
78
- expectTypeOf<fragmentType>().toEqualTypeOf<
79
- { author: ({ id: string; name: string | null; email: string | null } & { posts: { id: string }[] }) | null }
80
- >()
79
+ assertType<Equal<fragmentType, { author: ({ id: string; name: string | null; email: string | null } & { posts: { id: string }[] }) | null }>>()
81
80
  })
82
81
 
83
82
  test('has one without arg', async () => {
@@ -144,6 +143,14 @@ describe('ts types', () => {
144
143
  test('omit', async () => {
145
144
  const fragment = qb.fragment('Post', it => it.$$().omit('publishedAt'))
146
145
  type fragmentType = FragmentType<typeof fragment>
147
- expectTypeOf<fragmentType>().toEqualTypeOf<{ id: string; status: PostStatus | null }>()
146
+ assertType<Equal<fragmentType, { id: string; status: PostStatus | null }>>()
147
+ })
148
+
149
+ test('includes condition', async () => {
150
+ assertType<Equal<Input.Condition<string[]>['includes'], string | undefined>>()
151
+ assertType<Equal<Input.Condition<readonly string[]>['includes'], string | undefined>>()
152
+ assertType<Equal<Input.Condition<JSONValue | null>['includes'], JSONValue | undefined>>()
153
+ assertType<Equal<Input.Condition['includes'], Value.FieldValue | undefined>>()
154
+ assertType<Equal<Input.Condition<string>['includes'], undefined>>()
148
155
  })
149
156
  })
@@ -0,0 +1,5 @@
1
+ /** Strict type identity: `true` only when X and Y are mutually assignable with identical modifiers. */
2
+ export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false
3
+
4
+ /** Compile-time assertion; the call only exists so the type argument is checked by `tsc`. */
5
+ export const assertType = <T extends true>(): void => {}