@contember/engine-content-api 2.1.0-rc.7 → 2.1.0-rc.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 (28) hide show
  1. package/CLAUDE.md +1 -1
  2. package/dist/development/src/mapper/ErrorUtils.cjs.map +1 -1
  3. package/dist/development/src/mapper/ErrorUtils.js.map +1 -1
  4. package/dist/development/src/mapper/select/Path.cjs +2 -2
  5. package/dist/development/src/mapper/select/Path.cjs.map +1 -1
  6. package/dist/development/src/mapper/select/Path.js +2 -2
  7. package/dist/development/src/mapper/select/Path.js.map +1 -1
  8. package/dist/production/src/mapper/ErrorUtils.cjs.map +1 -1
  9. package/dist/production/src/mapper/ErrorUtils.js.map +1 -1
  10. package/dist/production/src/mapper/select/Path.cjs +2 -2
  11. package/dist/production/src/mapper/select/Path.cjs.map +1 -1
  12. package/dist/production/src/mapper/select/Path.js +2 -2
  13. package/dist/production/src/mapper/select/Path.js.map +1 -1
  14. package/dist/tests/cases/integration/graphQlRequests/query/acl/internalColumnAliases.test.d.ts +2 -0
  15. package/dist/tests/cases/integration/graphQlRequests/query/acl/internalColumnAliases.test.d.ts.map +1 -0
  16. package/dist/tests/cases/integration/graphQlRequests/query/acl/mergedRolePredicate.test.d.ts +2 -0
  17. package/dist/tests/cases/integration/graphQlRequests/query/acl/mergedRolePredicate.test.d.ts.map +1 -0
  18. package/dist/tests/cases/unit/internalColumnAliases.test.d.ts +2 -0
  19. package/dist/tests/cases/unit/internalColumnAliases.test.d.ts.map +1 -0
  20. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  21. package/dist/types/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +9 -9
  23. package/src/mapper/ErrorUtils.ts +1 -1
  24. package/src/mapper/select/Path.ts +2 -2
  25. package/tests/cases/integration/graphQlRequests/query/acl/internalColumnAliases.test.ts +326 -0
  26. package/tests/cases/integration/graphQlRequests/query/acl/mergedRolePredicate.test.ts +124 -0
  27. package/tests/cases/unit/internalColumnAliases.test.ts +41 -0
  28. package/tests/cases/unit/mutationSqlError.test.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-content-api",
3
- "version": "2.1.0-rc.7",
3
+ "version": "2.1.0-rc.8",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/production/index.js",
6
6
  "typings": "./dist/types/index.d.ts",
@@ -26,17 +26,17 @@
26
26
  "generate": "gql-gen --config graphql.codegen.yml"
27
27
  },
28
28
  "dependencies": {
29
- "@contember/database": "2.1.0-rc.7",
30
- "@contember/dic": "2.1.0-rc.7",
31
- "@contember/graphql-utils": "2.1.0-rc.7",
32
- "@contember/logger": "2.1.0-rc.7",
33
- "@contember/schema": "2.1.0-rc.7",
34
- "@contember/schema-definition": "2.1.0-rc.7",
35
- "@contember/schema-utils": "2.1.0-rc.7",
29
+ "@contember/database": "2.1.0-rc.8",
30
+ "@contember/dic": "2.1.0-rc.8",
31
+ "@contember/graphql-utils": "2.1.0-rc.8",
32
+ "@contember/logger": "2.1.0-rc.8",
33
+ "@contember/schema": "2.1.0-rc.8",
34
+ "@contember/schema-definition": "2.1.0-rc.8",
35
+ "@contember/schema-utils": "2.1.0-rc.8",
36
36
  "fast-deep-equal": "^3.1.3"
37
37
  },
38
38
  "devDependencies": {
39
- "@contember/database-tester": "2.1.0-rc.7",
39
+ "@contember/database-tester": "2.1.0-rc.8",
40
40
  "@types/node": "^24.12.0",
41
41
  "graphql": "^16.13.1",
42
42
  "pg": "^8.20.0"
@@ -155,7 +155,7 @@ export const convertError = (
155
155
  }
156
156
 
157
157
  // Data (22) and integrity (23) errors and project trigger exceptions (P0001) carry messages clients act on. Other
158
- // messages (e.g. class 42) can quote internal SQL identifiers.
158
+ // messages (e.g. class 42) can quote internal SQL identifiers, which embed ACL predicate names.
159
159
  const hasClientFacingMessage = (code: string | undefined): boolean =>
160
160
  code !== undefined && (code.startsWith('22') || code.startsWith('23') || code === 'P0001')
161
161
 
@@ -50,8 +50,8 @@ export class Path {
50
50
  return new Path([...this.path, path], this.aliasContext, this.rootAlias)
51
51
  }
52
52
 
53
- // Segment for a column the engine selects for itself. `$` cannot occur in a GraphQL alias, so these aliases stay
54
- // distinct from those of selected fields; `kind` must not contain `_`.
53
+ // Engine-owned columns share the alias namespace with client GraphQL aliases. `$` is outside the GraphQL Name
54
+ // grammar, so no client alias (nor `_`-joined aliases) can produce the segment; `kind` must not contain `_`.
55
55
  public forInternal(kind: string) {
56
56
  return this.for('$' + kind)
57
57
  }
@@ -0,0 +1,326 @@
1
+ import { c, createSchema, SchemaBuilder, SchemaDefinition as def } from '@contember/schema-definition'
2
+ import { Acl, Model } from '@contember/schema'
3
+ import { test } from 'bun:test'
4
+ import { execute } from '../../../../../src/test'
5
+ import { GQL, SQL } from '../../../../../src/tags'
6
+ import { testUuid } from '../../../../../src/testUuid'
7
+ import { viewComputedId } from '../../../../../../src/utils/viewComputedId'
8
+
9
+ // A client chooses GraphQL aliases freely, and they become SQL column aliases. Engine-owned columns (ACL predicate
10
+ // results, relation keys) must stay in a namespace no alias can reach, otherwise the client's column is read instead.
11
+
12
+ const schema = new SchemaBuilder()
13
+ .entity('Author', entity =>
14
+ entity
15
+ .column('name', column => column.type(Model.ColumnType.String))
16
+ .column('verified', column => column.type(Model.ColumnType.Bool)))
17
+ .entity('Post', entity =>
18
+ entity
19
+ .column('externalId', column => column.type(Model.ColumnType.Uuid))
20
+ .manyHasOne('author', relation => relation.target('Author')))
21
+ .buildSchema()
22
+
23
+ const longPredicateName = 'nameVisible' + 'Long'.repeat(13)
24
+
25
+ const permissions: Acl.Permissions = {
26
+ Author: {
27
+ predicates: {
28
+ nameVisible: { verified: { eq: true } },
29
+ [longPredicateName]: { verified: { eq: true } },
30
+ },
31
+ operations: {
32
+ read: { id: true, verified: true, name: 'nameVisible' },
33
+ update: { name: 'nameVisible' },
34
+ },
35
+ },
36
+ Post: {
37
+ predicates: {},
38
+ operations: {
39
+ read: { id: true, externalId: true, author: true },
40
+ },
41
+ },
42
+ }
43
+
44
+ test('a root alias equal to a predicate column does not unmask the field', async () => {
45
+ await execute({
46
+ schema,
47
+ permissions,
48
+ query: GQL`
49
+ query {
50
+ listAuthor {
51
+ id
52
+ name
53
+ __predicate_nameVisible: verified
54
+ }
55
+ }`,
56
+ executes: [
57
+ {
58
+ sql: SQL`
59
+ select "root_"."id" as "root_id",
60
+ "root_"."verified" = ? as "root_$predicate_nameVisible",
61
+ "root_"."name" as "root_name",
62
+ "root_"."verified" as "root___predicate_nameVisible"
63
+ from "public"."author" as "root_"`,
64
+ parameters: [true],
65
+ response: {
66
+ rows: [
67
+ {
68
+ root_id: testUuid(1),
69
+ 'root_$predicate_nameVisible': false,
70
+ root_name: 'masked',
71
+ root___predicate_nameVisible: true,
72
+ },
73
+ ],
74
+ },
75
+ },
76
+ ],
77
+ return: {
78
+ data: {
79
+ listAuthor: [{ id: testUuid(1), name: null, __predicate_nameVisible: true }],
80
+ },
81
+ },
82
+ })
83
+ })
84
+
85
+ test('a nested alias equal to a predicate column does not unmask the field', async () => {
86
+ await execute({
87
+ schema,
88
+ permissions,
89
+ query: GQL`
90
+ query {
91
+ listPost {
92
+ id
93
+ author {
94
+ id
95
+ name
96
+ __predicate_nameVisible: verified
97
+ }
98
+ }
99
+ }`,
100
+ executes: [
101
+ {
102
+ sql: SQL`
103
+ select "root_"."id" as "root_id",
104
+ "root_"."author_id" as "root_$key_author"
105
+ from "public"."post" as "root_"`,
106
+ parameters: [],
107
+ response: {
108
+ rows: [{ root_id: testUuid(1), 'root_$key_author': testUuid(2) }],
109
+ },
110
+ },
111
+ {
112
+ sql: SQL`
113
+ select "root_"."id" as "root_$index_id",
114
+ "root_"."id" as "root_id",
115
+ "root_"."verified" = ? as "root_$predicate_nameVisible",
116
+ "root_"."name" as "root_name",
117
+ "root_"."verified" as "root___predicate_nameVisible"
118
+ from "public"."author" as "root_"
119
+ where "root_"."id" in (?)`,
120
+ parameters: [true, testUuid(2)],
121
+ response: {
122
+ rows: [
123
+ {
124
+ 'root_$index_id': testUuid(2),
125
+ root_id: testUuid(2),
126
+ 'root_$predicate_nameVisible': false,
127
+ root_name: 'masked',
128
+ root___predicate_nameVisible: true,
129
+ },
130
+ ],
131
+ },
132
+ },
133
+ ],
134
+ return: {
135
+ data: {
136
+ listPost: [{ id: testUuid(1), author: { id: testUuid(2), name: null, __predicate_nameVisible: true } }],
137
+ },
138
+ },
139
+ })
140
+ })
141
+
142
+ test('an alias equal to a predicate column does not change _meta.updatable', async () => {
143
+ await execute({
144
+ schema,
145
+ permissions,
146
+ query: GQL`
147
+ query {
148
+ listAuthor {
149
+ id
150
+ __predicate_nameVisible: verified
151
+ _meta {
152
+ name {
153
+ updatable
154
+ }
155
+ }
156
+ }
157
+ }`,
158
+ executes: [
159
+ {
160
+ sql: SQL`
161
+ select "root_"."id" as "root_id",
162
+ "root_"."verified" as "root___predicate_nameVisible",
163
+ "root_"."verified" = ? as "root_$predicate_nameVisible"
164
+ from "public"."author" as "root_"`,
165
+ parameters: [true],
166
+ response: {
167
+ rows: [
168
+ {
169
+ root_id: testUuid(1),
170
+ root___predicate_nameVisible: true,
171
+ 'root_$predicate_nameVisible': false,
172
+ },
173
+ ],
174
+ },
175
+ },
176
+ ],
177
+ return: {
178
+ data: {
179
+ listAuthor: [{ id: testUuid(1), __predicate_nameVisible: true, _meta: { name: { updatable: false } } }],
180
+ },
181
+ },
182
+ })
183
+ })
184
+
185
+ test('aliases over the identifier length limit stay apart from predicate columns', async () => {
186
+ const longPermissions: Acl.Permissions = {
187
+ ...permissions,
188
+ Author: {
189
+ ...permissions.Author,
190
+ operations: {
191
+ read: { id: true, verified: true, name: longPredicateName },
192
+ },
193
+ },
194
+ }
195
+ await execute({
196
+ schema,
197
+ permissions: longPermissions,
198
+ query: `
199
+ query {
200
+ listAuthor {
201
+ id
202
+ name
203
+ __predicate_${longPredicateName}: verified
204
+ }
205
+ }`,
206
+ executes: [
207
+ {
208
+ sql: SQL`
209
+ select "root_"."id" as "root_id",
210
+ "root_"."verified" = ? as "root_$predicate_nameVisibleLongLongLongLongLongLongLongLongLo_1",
211
+ "root_"."name" as "root_name",
212
+ "root_"."verified" as "root___predicate_nameVisibleLongLongLongLongLongLongLongLongL_2"
213
+ from "public"."author" as "root_"`,
214
+ parameters: [true],
215
+ response: {
216
+ rows: [
217
+ {
218
+ root_id: testUuid(1),
219
+ 'root_$predicate_nameVisibleLongLongLongLongLongLongLongLongLo_1': false,
220
+ root_name: 'masked',
221
+ root___predicate_nameVisibleLongLongLongLongLongLongLongLongL_2: true,
222
+ },
223
+ ],
224
+ },
225
+ },
226
+ ],
227
+ return: {
228
+ data: {
229
+ listAuthor: [{ id: testUuid(1), name: null, [`__predicate_${longPredicateName}`]: true }],
230
+ },
231
+ },
232
+ })
233
+ })
234
+
235
+ test('an alias equal to a relation field does not replace the relation key', async () => {
236
+ await execute({
237
+ schema,
238
+ permissions,
239
+ query: GQL`
240
+ query {
241
+ listPost {
242
+ id
243
+ writer: author {
244
+ id
245
+ }
246
+ author: externalId
247
+ }
248
+ }`,
249
+ executes: [
250
+ {
251
+ sql: SQL`
252
+ select "root_"."id" as "root_id",
253
+ "root_"."author_id" as "root_$key_author",
254
+ "root_"."external_id" as "root_author"
255
+ from "public"."post" as "root_"`,
256
+ parameters: [],
257
+ response: {
258
+ rows: [{ root_id: testUuid(1), 'root_$key_author': testUuid(2), root_author: testUuid(9) }],
259
+ },
260
+ },
261
+ {
262
+ sql: SQL`
263
+ select "root_"."id" as "root_$index_id",
264
+ "root_"."id" as "root_id"
265
+ from "public"."author" as "root_"
266
+ where "root_"."id" in (?)`,
267
+ parameters: [testUuid(2)],
268
+ response: {
269
+ rows: [{ 'root_$index_id': testUuid(2), root_id: testUuid(2) }],
270
+ },
271
+ },
272
+ ],
273
+ return: {
274
+ data: {
275
+ listPost: [{ id: testUuid(1), writer: { id: testUuid(2) }, author: testUuid(9) }],
276
+ },
277
+ },
278
+ })
279
+ })
280
+
281
+ namespace ViewSchema {
282
+ @c.View(`SELECT gen_random_uuid() AS id, 'a' AS first, 'b' AS second`, { idSource: ['first', 'second'] })
283
+ export class Pair {
284
+ first = def.stringColumn()
285
+ second = def.stringColumn()
286
+ }
287
+ }
288
+
289
+ test('an alias equal to a view id source column does not change the computed id', async () => {
290
+ await execute({
291
+ schema: createSchema(ViewSchema).model,
292
+ query: GQL`
293
+ query {
294
+ listPair {
295
+ id
296
+ id_first__id_src: second
297
+ }
298
+ }`,
299
+ executes: [
300
+ {
301
+ sql: SQL`
302
+ select "root_"."first" as "root_id_$idSrc_first",
303
+ "root_"."second" as "root_id_$idSrc_second",
304
+ "root_"."id" as "root_id",
305
+ "root_"."second" as "root_id_first__id_src"
306
+ from "public"."pair" as "root_"`,
307
+ parameters: [],
308
+ response: {
309
+ rows: [
310
+ {
311
+ 'root_id_$idSrc_first': 'a',
312
+ 'root_id_$idSrc_second': 'b',
313
+ root_id: testUuid(1),
314
+ root_id_first__id_src: 'b',
315
+ },
316
+ ],
317
+ },
318
+ },
319
+ ],
320
+ return: {
321
+ data: {
322
+ listPair: [{ id: viewComputedId(['a', 'b']), id_first__id_src: 'b' }],
323
+ },
324
+ },
325
+ })
326
+ })
@@ -0,0 +1,124 @@
1
+ import { SchemaBuilder } from '@contember/schema-definition'
2
+ import { Acl, Model, Schema } from '@contember/schema'
3
+ import { emptySchema } from '@contember/schema-utils'
4
+ import { expect, test } from 'bun:test'
5
+ import { PermissionFactory } from '../../../../../../src'
6
+ import { execute } from '../../../../../src/test'
7
+ import { GQL, SQL } from '../../../../../src/tags'
8
+ import { testUuid } from '../../../../../src/testUuid'
9
+
10
+ // Two roles granting a field with equally named predicates (as generated from equal conditions), plus a `through`
11
+ // grant from a third role, make the merger mint a name that exists only in the through-inclusive set:
12
+ // `__merge____merge__p__p__q`.
13
+
14
+ const model = new SchemaBuilder()
15
+ .entity('Project', entity => entity.oneHasMany('versions', c => c.target('Version').ownedBy('project')))
16
+ .entity('Version', entity =>
17
+ entity
18
+ .column('label', column => column.type(Model.ColumnType.String))
19
+ .column('isPublic', column => column.type(Model.ColumnType.Bool))
20
+ .column('isHidden', column => column.type(Model.ColumnType.Bool)))
21
+ .buildSchema()
22
+
23
+ // Each role gets its own definition object, as roles loaded from a stored schema do.
24
+ const createRoleWithLabel = (): Acl.RolePermissions => ({
25
+ variables: {},
26
+ stages: '*',
27
+ entities: {
28
+ Project: { predicates: {}, operations: { read: { id: true, versions: true } } },
29
+ Version: {
30
+ predicates: { isPublic: { isPublic: { eq: true } } },
31
+ operations: { read: { id: true, project: true, label: 'isPublic' } },
32
+ },
33
+ },
34
+ })
35
+
36
+ const schema: Schema = {
37
+ ...emptySchema,
38
+ model,
39
+ acl: {
40
+ roles: {
41
+ editor: createRoleWithLabel(),
42
+ viewer: createRoleWithLabel(),
43
+ listing: {
44
+ variables: {},
45
+ stages: '*',
46
+ entities: {
47
+ Version: {
48
+ predicates: { notHidden: { isHidden: { eq: false } } },
49
+ operations: { read: { id: true, label: 'notHidden' }, noRoot: ['read'] },
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ }
56
+
57
+ test('nested cell predicate minted by merging roles twice is resolved', async () => {
58
+ const { root, all } = new PermissionFactory().createContextual(schema, ['editor', 'viewer', 'listing'])
59
+ const mergedName = '__merge____merge__isPublic__isPublic__notHidden'
60
+ expect(all.Version.operations.read?.label).toBe(mergedName)
61
+ expect(root.Version.operations.read?.label).toBe('__merge__isPublic__isPublic')
62
+ expect(root.Version.predicates[mergedName]).toBeUndefined()
63
+
64
+ await execute({
65
+ schema: model,
66
+ permissions: root,
67
+ allPermissions: all,
68
+ query: GQL`
69
+ query {
70
+ listProject {
71
+ versions {
72
+ id
73
+ label
74
+ }
75
+ }
76
+ }`,
77
+ executes: [
78
+ {
79
+ sql: SQL`select "root_"."id" as "root_$key_id", "root_"."id" as "root_id" from "public"."project" as "root_"`,
80
+ parameters: [],
81
+ response: {
82
+ rows: [{ root_id: testUuid(1), 'root_$key_id': testUuid(1) }],
83
+ },
84
+ },
85
+ {
86
+ sql: SQL`
87
+ select "root_"."project_id" as "__grouping_key",
88
+ "root_"."id" as "root_id",
89
+ ("root_"."is_public" = ? or "root_"."is_hidden" = ?) as "root_$predicate___merge____merge__isPublic__isPublic__notHidd_1",
90
+ "root_"."label" as "root_label"
91
+ from "public"."version" as "root_" where "root_"."project_id" in (?)`,
92
+ parameters: [true, false, testUuid(1)],
93
+ response: {
94
+ rows: [
95
+ {
96
+ __grouping_key: testUuid(1),
97
+ root_id: testUuid(2),
98
+ 'root_$predicate___merge____merge__isPublic__isPublic__notHidd_1': true,
99
+ root_label: 'v1',
100
+ },
101
+ {
102
+ __grouping_key: testUuid(1),
103
+ root_id: testUuid(3),
104
+ 'root_$predicate___merge____merge__isPublic__isPublic__notHidd_1': false,
105
+ root_label: 'v2',
106
+ },
107
+ ],
108
+ },
109
+ },
110
+ ],
111
+ return: {
112
+ data: {
113
+ listProject: [
114
+ {
115
+ versions: [
116
+ { id: testUuid(2), label: 'v1' },
117
+ { id: testUuid(3), label: null },
118
+ ],
119
+ },
120
+ ],
121
+ },
122
+ },
123
+ })
124
+ })
@@ -0,0 +1,41 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { PathFactory } from '../../../src/mapper'
3
+
4
+ describe('internal column aliases', () => {
5
+ const internalAlias = (factory: PathFactory) => factory.create([]).for('rel').forInternal('predicate').for('x').alias
6
+
7
+ it('cannot be produced by a single client alias', () => {
8
+ const factory = new PathFactory()
9
+ expect(factory.create([]).for('rel___predicate_x').alias).not.toBe(internalAlias(factory))
10
+ })
11
+
12
+ it('cannot be produced by concatenated client aliases', () => {
13
+ const factory = new PathFactory()
14
+ const internal = internalAlias(factory)
15
+ for (const segments of [['rel', '__predicate', 'x'], ['rel_', '_predicate_x'], ['rel', '__predicate_x'], ['rel___predicate', 'x']]) {
16
+ expect(segments.reduce((path, segment) => path.for(segment), factory.create([])).alias).not.toBe(internal)
17
+ }
18
+ })
19
+
20
+ it('stays apart from a client alias after truncation', () => {
21
+ const factory = new PathFactory()
22
+ const name = 'p'.repeat(80)
23
+ const internal = factory.create([]).forInternal('predicate').for(name).alias
24
+ const client = factory.create([]).for(`__predicate_${name}`).alias
25
+ expect(internal.length).toBe(63)
26
+ expect(client.length).toBe(63)
27
+ expect(client).not.toBe(internal)
28
+ })
29
+
30
+ it('stays unique within the PostgreSQL byte limit for multi-byte names', () => {
31
+ const factory = new PathFactory()
32
+ const name = 'x' + 'é'.repeat(23)
33
+ // 63 and 64 bytes untruncated: PostgreSQL would cut the second to the first
34
+ const first = factory.create([]).forInternal('predicate').for(name).alias
35
+ const second = factory.create([]).forInternal('predicate').for(name + 'x').alias
36
+ const encoder = new TextEncoder()
37
+ expect(encoder.encode(first).length).toBe(63)
38
+ expect(encoder.encode(second).length).toBe(63)
39
+ expect(second).not.toBe(first)
40
+ })
41
+ })
@@ -16,7 +16,7 @@ const convert = (code: string, message: string) => {
16
16
 
17
17
  describe('mutation SQL error', () => {
18
18
  it('returns a generic message for an error that can quote internal identifiers, and logs the original', () => {
19
- const { message, logged, error } = convert('42702', 'column reference "root_$predicate_rule" is ambiguous')
19
+ const { message, logged, error } = convert('42702', 'column reference "root_$predicate_secretRule" is ambiguous')
20
20
  expect(message).toBe('Database query failed')
21
21
  expect(logged).toContain(error)
22
22
  })