@contember/engine-content-api 2.2.0-alpha.14 → 2.2.0-alpha.15

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 (156) hide show
  1. package/CLAUDE.md +1 -0
  2. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.cjs +1 -0
  3. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.cjs.map +1 -1
  4. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.js +1 -0
  5. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.js.map +1 -1
  6. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.cjs +1 -0
  7. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.cjs.map +1 -1
  8. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.js +1 -0
  9. package/dist/development/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.js.map +1 -1
  10. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.cjs +1 -0
  11. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.cjs.map +1 -1
  12. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.js +1 -0
  13. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.js.map +1 -1
  14. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.cjs +1 -0
  15. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.cjs.map +1 -1
  16. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.js +1 -0
  17. package/dist/development/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.js.map +1 -1
  18. package/dist/development/src/mapper/ErrorUtils.cjs +7 -1
  19. package/dist/development/src/mapper/ErrorUtils.cjs.map +1 -1
  20. package/dist/development/src/mapper/ErrorUtils.js +7 -1
  21. package/dist/development/src/mapper/ErrorUtils.js.map +1 -1
  22. package/dist/development/src/mapper/Mapper.cjs +2 -1
  23. package/dist/development/src/mapper/Mapper.cjs.map +1 -1
  24. package/dist/development/src/mapper/Mapper.js +2 -1
  25. package/dist/development/src/mapper/Mapper.js.map +1 -1
  26. package/dist/development/src/mapper/select/Path.cjs +23 -3
  27. package/dist/development/src/mapper/select/Path.cjs.map +1 -1
  28. package/dist/development/src/mapper/select/Path.js +23 -3
  29. package/dist/development/src/mapper/select/Path.js.map +1 -1
  30. package/dist/development/src/mapper/select/SelectBuilder.cjs +5 -5
  31. package/dist/development/src/mapper/select/SelectBuilder.cjs.map +1 -1
  32. package/dist/development/src/mapper/select/SelectBuilder.js +5 -5
  33. package/dist/development/src/mapper/select/SelectBuilder.js.map +1 -1
  34. package/dist/development/src/mapper/select/handlers/FieldsVisitor.cjs +1 -1
  35. package/dist/development/src/mapper/select/handlers/FieldsVisitor.cjs.map +1 -1
  36. package/dist/development/src/mapper/select/handlers/FieldsVisitor.js +1 -1
  37. package/dist/development/src/mapper/select/handlers/FieldsVisitor.js.map +1 -1
  38. package/dist/development/src/resolvers/MutationResolver.cjs +1 -0
  39. package/dist/development/src/resolvers/MutationResolver.cjs.map +1 -1
  40. package/dist/development/src/resolvers/MutationResolver.js +1 -0
  41. package/dist/development/src/resolvers/MutationResolver.js.map +1 -1
  42. package/dist/development/src/resolvers/ReadHelpers.cjs +1 -0
  43. package/dist/development/src/resolvers/ReadHelpers.cjs.map +1 -1
  44. package/dist/development/src/resolvers/ReadHelpers.js +1 -0
  45. package/dist/development/src/resolvers/ReadHelpers.js.map +1 -1
  46. package/dist/development/src/resolvers/ReadResolver.cjs +21 -7
  47. package/dist/development/src/resolvers/ReadResolver.cjs.map +1 -1
  48. package/dist/development/src/resolvers/ReadResolver.js +21 -7
  49. package/dist/development/src/resolvers/ReadResolver.js.map +1 -1
  50. package/dist/development/src/schema/GraphQlSchemaBuilderFactory.cjs +1 -0
  51. package/dist/development/src/schema/GraphQlSchemaBuilderFactory.cjs.map +1 -1
  52. package/dist/development/src/schema/GraphQlSchemaBuilderFactory.js +1 -0
  53. package/dist/development/src/schema/GraphQlSchemaBuilderFactory.js.map +1 -1
  54. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.cjs +1 -0
  55. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.cjs.map +1 -1
  56. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.js +1 -0
  57. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneReducerExecutionHandler.js.map +1 -1
  58. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.cjs +1 -0
  59. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.cjs.map +1 -1
  60. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.js +1 -0
  61. package/dist/production/src/extensions/hasManyToHasOneReducer/HasManyToHasOneRelationReducerVisitor.js.map +1 -1
  62. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.cjs +1 -0
  63. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.cjs.map +1 -1
  64. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.js +1 -0
  65. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyExecutionHandler.js.map +1 -1
  66. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.cjs +1 -0
  67. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.cjs.map +1 -1
  68. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.js +1 -0
  69. package/dist/production/src/extensions/paginatedHasMany/PaginatedHasManyFieldProviderVisitor.js.map +1 -1
  70. package/dist/production/src/mapper/ErrorUtils.cjs +7 -1
  71. package/dist/production/src/mapper/ErrorUtils.cjs.map +1 -1
  72. package/dist/production/src/mapper/ErrorUtils.js +7 -1
  73. package/dist/production/src/mapper/ErrorUtils.js.map +1 -1
  74. package/dist/production/src/mapper/Mapper.cjs +2 -1
  75. package/dist/production/src/mapper/Mapper.cjs.map +1 -1
  76. package/dist/production/src/mapper/Mapper.js +2 -1
  77. package/dist/production/src/mapper/Mapper.js.map +1 -1
  78. package/dist/production/src/mapper/select/Path.cjs +23 -3
  79. package/dist/production/src/mapper/select/Path.cjs.map +1 -1
  80. package/dist/production/src/mapper/select/Path.js +23 -3
  81. package/dist/production/src/mapper/select/Path.js.map +1 -1
  82. package/dist/production/src/mapper/select/SelectBuilder.cjs +5 -5
  83. package/dist/production/src/mapper/select/SelectBuilder.cjs.map +1 -1
  84. package/dist/production/src/mapper/select/SelectBuilder.js +5 -5
  85. package/dist/production/src/mapper/select/SelectBuilder.js.map +1 -1
  86. package/dist/production/src/mapper/select/handlers/FieldsVisitor.cjs +1 -1
  87. package/dist/production/src/mapper/select/handlers/FieldsVisitor.cjs.map +1 -1
  88. package/dist/production/src/mapper/select/handlers/FieldsVisitor.js +1 -1
  89. package/dist/production/src/mapper/select/handlers/FieldsVisitor.js.map +1 -1
  90. package/dist/production/src/resolvers/MutationResolver.cjs +1 -0
  91. package/dist/production/src/resolvers/MutationResolver.cjs.map +1 -1
  92. package/dist/production/src/resolvers/MutationResolver.js +1 -0
  93. package/dist/production/src/resolvers/MutationResolver.js.map +1 -1
  94. package/dist/production/src/resolvers/ReadHelpers.cjs +1 -0
  95. package/dist/production/src/resolvers/ReadHelpers.cjs.map +1 -1
  96. package/dist/production/src/resolvers/ReadHelpers.js +1 -0
  97. package/dist/production/src/resolvers/ReadHelpers.js.map +1 -1
  98. package/dist/production/src/resolvers/ReadResolver.cjs +21 -7
  99. package/dist/production/src/resolvers/ReadResolver.cjs.map +1 -1
  100. package/dist/production/src/resolvers/ReadResolver.js +21 -7
  101. package/dist/production/src/resolvers/ReadResolver.js.map +1 -1
  102. package/dist/production/src/schema/GraphQlSchemaBuilderFactory.cjs +1 -0
  103. package/dist/production/src/schema/GraphQlSchemaBuilderFactory.cjs.map +1 -1
  104. package/dist/production/src/schema/GraphQlSchemaBuilderFactory.js +1 -0
  105. package/dist/production/src/schema/GraphQlSchemaBuilderFactory.js.map +1 -1
  106. package/dist/tests/cases/unit/mutationSqlError.test.d.ts +2 -0
  107. package/dist/tests/cases/unit/mutationSqlError.test.d.ts.map +1 -0
  108. package/dist/tests/cases/unit/pathAlias.test.d.ts +2 -0
  109. package/dist/tests/cases/unit/pathAlias.test.d.ts.map +1 -0
  110. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  111. package/dist/types/mapper/ErrorUtils.d.ts.map +1 -1
  112. package/dist/types/mapper/select/Path.d.ts +1 -0
  113. package/dist/types/mapper/select/Path.d.ts.map +1 -1
  114. package/dist/types/mapper/select/SelectBuilder.d.ts.map +1 -1
  115. package/dist/types/resolvers/ReadResolver.d.ts.map +1 -1
  116. package/dist/types/tsconfig.tsbuildinfo +1 -1
  117. package/package.json +9 -9
  118. package/src/mapper/ErrorUtils.ts +11 -1
  119. package/src/mapper/Mapper.ts +1 -1
  120. package/src/mapper/select/Path.ts +25 -5
  121. package/src/mapper/select/SelectBuilder.ts +5 -4
  122. package/src/mapper/select/handlers/FieldsVisitor.ts +1 -1
  123. package/src/resolvers/ReadResolver.ts +24 -7
  124. package/tests/cases/bugs/bug102.test.ts +4 -4
  125. package/tests/cases/integration/graphQlRequests/delete/deleteById.test.ts +4 -3
  126. package/tests/cases/integration/graphQlRequests/query/acl/differentRowAndFieldPredicate.test.ts +4 -4
  127. package/tests/cases/integration/graphQlRequests/query/acl/fieldRestrictedByLocale.test.ts +12 -10
  128. package/tests/cases/integration/graphQlRequests/query/acl/metaReadableThroughContext.test.ts +15 -15
  129. package/tests/cases/integration/graphQlRequests/query/acl/nestedPredicate.test.ts +7 -6
  130. package/tests/cases/integration/graphQlRequests/query/acl/nonAdjacentBackReference.test.ts +6 -6
  131. package/tests/cases/integration/graphQlRequests/query/acl/orderByCellPredicate.test.ts +6 -6
  132. package/tests/cases/integration/graphQlRequests/query/acl/predicateElimination.test.ts +25 -23
  133. package/tests/cases/integration/graphQlRequests/query/acl/relationAcl.test.ts +12 -9
  134. package/tests/cases/integration/graphQlRequests/query/acl/rowPredicateSubset.test.ts +6 -6
  135. package/tests/cases/integration/graphQlRequests/query/acl/throughScope.test.ts +14 -12
  136. package/tests/cases/integration/graphQlRequests/query/acl/throughScopeNullability.test.ts +4 -4
  137. package/tests/cases/integration/graphQlRequests/query/filter/backReferencedFilter.test.ts +3 -1
  138. package/tests/cases/integration/graphQlRequests/query/graphqlFeatures/alias.test.ts +2 -2
  139. package/tests/cases/integration/graphQlRequests/query/graphqlFeatures/fragments.test.ts +8 -8
  140. package/tests/cases/integration/graphQlRequests/query/misc/reducedHasMany.test.ts +10 -10
  141. package/tests/cases/integration/graphQlRequests/query/pagination/hasManyLimit.test.ts +8 -8
  142. package/tests/cases/integration/graphQlRequests/query/pagination/paginate.test.ts +5 -4
  143. package/tests/cases/integration/graphQlRequests/query/pagination/paginateAliased.test.ts +5 -4
  144. package/tests/cases/integration/graphQlRequests/query/relations/advanced.test.ts +17 -8
  145. package/tests/cases/integration/graphQlRequests/query/relations/manyHasManyInverse.test.ts +6 -6
  146. package/tests/cases/integration/graphQlRequests/query/relations/manyHasManyOwning.test.ts +6 -6
  147. package/tests/cases/integration/graphQlRequests/query/relations/manyHasOne.test.ts +12 -9
  148. package/tests/cases/integration/graphQlRequests/query/relations/oneHasMany.test.ts +12 -12
  149. package/tests/cases/integration/graphQlRequests/query/relations/oneHasOneInverse.test.ts +6 -4
  150. package/tests/cases/integration/graphQlRequests/query/relations/oneHasOneOwner.test.ts +6 -4
  151. package/tests/cases/integration/graphQlRequests/query/sorting/defaultOrdering.test.ts +4 -4
  152. package/tests/cases/integration/graphQlRequests/query/sorting/randomSort.test.ts +5 -5
  153. package/tests/cases/integration/graphQlRequests/query/sorting/sortedHasMany.test.ts +2 -2
  154. package/tests/cases/integration/graphQlRequests/query/view/view.test.ts +6 -6
  155. package/tests/cases/unit/mutationSqlError.test.ts +30 -0
  156. package/tests/cases/unit/pathAlias.test.ts +26 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-content-api",
3
- "version": "2.2.0-alpha.14",
3
+ "version": "2.2.0-alpha.15",
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.2.0-alpha.14",
30
- "@contember/dic": "2.2.0-alpha.14",
31
- "@contember/graphql-utils": "2.2.0-alpha.14",
32
- "@contember/logger": "2.2.0-alpha.14",
33
- "@contember/schema": "2.2.0-alpha.14",
34
- "@contember/schema-definition": "2.2.0-alpha.14",
35
- "@contember/schema-utils": "2.2.0-alpha.14",
29
+ "@contember/database": "2.2.0-alpha.15",
30
+ "@contember/dic": "2.2.0-alpha.15",
31
+ "@contember/graphql-utils": "2.2.0-alpha.15",
32
+ "@contember/logger": "2.2.0-alpha.15",
33
+ "@contember/schema": "2.2.0-alpha.15",
34
+ "@contember/schema-definition": "2.2.0-alpha.15",
35
+ "@contember/schema-utils": "2.2.0-alpha.15",
36
36
  "fast-deep-equal": "^3.1.3"
37
37
  },
38
38
  "devDependencies": {
39
- "@contember/database-tester": "2.2.0-alpha.14",
39
+ "@contember/database-tester": "2.2.0-alpha.15",
40
40
  "@types/node": "^24.12.0",
41
41
  "graphql": "^16.13.1",
42
42
  "pg": "^8.20.0"
@@ -142,13 +142,23 @@ export const convertError = (
142
142
  if (e instanceof Database.TransactionAbortedError) {
143
143
  return new MutationSqlError([], e.originalMessage, [MutationResultHint.subSequentSqlError])
144
144
  }
145
- if (e instanceof Database.QueryError || e instanceof Database.CannotCommitError) {
145
+ if (e instanceof Database.CannotCommitError) {
146
146
  logger.error(e, { loc: 'convertError' })
147
147
  return new MutationSqlError([], e.originalMessage, [MutationResultHint.sqlError])
148
148
  }
149
+ if (e instanceof Database.QueryError) {
150
+ logger.error(e, { loc: 'convertError' })
151
+ const message = hasClientFacingMessage(e.code) ? e.originalMessage : 'Database query failed'
152
+ return new MutationSqlError([], message, [MutationResultHint.sqlError])
153
+ }
149
154
  throw e
150
155
  }
151
156
 
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.
159
+ const hasClientFacingMessage = (code: string | undefined): boolean =>
160
+ code !== undefined && (code.startsWith('22') || code.startsWith('23') || code === 'P0001')
161
+
152
162
  export const tryMutation = async (
153
163
  schema: Model.Schema,
154
164
  databaseMetadata: DatabaseMetadata,
@@ -79,7 +79,7 @@ export class Mapper<ConnectionType extends Connection.ConnectionLike = Connectio
79
79
  ): Promise<SelectIndexedResultObjects> {
80
80
  const hydrator = new SelectHydrator(this.memoryBudget)
81
81
  const path = this.pathFactory.create([])
82
- const indexByAlias: string = path.for(indexBy).alias
82
+ const indexByAlias: string = path.forInternal('index').for(indexBy).alias
83
83
  const qb: SelectBuilder = SelectBuilder.create()
84
84
  .select([path.alias, getColumnName(this.schema, entity, indexBy)], indexByAlias)
85
85
  const rows = await this.selectRows(hydrator, qb, entity, input, relationPath)
@@ -1,5 +1,8 @@
1
1
  const MAX_IDENTIFIER_LENGTH = 63
2
2
 
3
+ const utf8Encoder = new TextEncoder()
4
+ const byteLength = (value: string): number => utf8Encoder.encode(value).length
5
+
3
6
  export type HopGuards = { readonly row: boolean; readonly field: boolean }
4
7
 
5
8
  /**
@@ -58,14 +61,31 @@ export class Path {
58
61
  return new Path([...this.path, path], this.aliasContext, this.rootAlias)
59
62
  }
60
63
 
64
+ // Segment for a column the engine selects for itself. `$` cannot occur in a GraphQL alias, so these aliases stay
65
+ // distinct from those of selected fields; `kind` must not contain `_`.
66
+ public forInternal(kind: string) {
67
+ return this.for('$' + kind)
68
+ }
69
+
70
+ // PostgreSQL limits identifiers in bytes. Untruncated aliases stay below the limit and truncated ones fill it
71
+ // exactly (padded when a multi-byte character does not fit), so the two can never coincide.
61
72
  private createAlias(): string {
62
73
  const alias = this.fullAlias
63
-
64
- // intentionally not allowing == MAX_IDENTIFIER_LENGTH
65
- if (alias.length < MAX_IDENTIFIER_LENGTH) {
74
+ if (byteLength(alias) < MAX_IDENTIFIER_LENGTH) {
66
75
  return alias
67
76
  }
68
- const index = this.aliasContext.getAliasIndex(alias).toString()
69
- return alias.substring(0, MAX_IDENTIFIER_LENGTH - index.length - 1) + '_' + index
77
+ const suffix = '_' + this.aliasContext.getAliasIndex(alias).toString()
78
+ const prefixBytes = MAX_IDENTIFIER_LENGTH - suffix.length
79
+ let prefix = ''
80
+ let usedBytes = 0
81
+ for (const char of alias) {
82
+ const charBytes = byteLength(char)
83
+ if (usedBytes + charBytes > prefixBytes) {
84
+ break
85
+ }
86
+ prefix += char
87
+ usedBytes += charBytes
88
+ }
89
+ return prefix + '_'.repeat(prefixBytes - usedBytes) + suffix
70
90
  }
71
91
  }
@@ -91,7 +91,7 @@ export class SelectBuilder {
91
91
  if (typeof predicate === 'boolean') {
92
92
  return () => predicate
93
93
  }
94
- const predicatePath = path.for('__predicate').for(predicate)
94
+ const predicatePath = path.forInternal('predicate').for(predicate)
95
95
 
96
96
  if (!fetchedPredicates.has(predicate)) {
97
97
  const primaryPredicate = this.predicateFactory.createReadPredicate(entity, undefined, this.relationPath)
@@ -132,8 +132,8 @@ export class SelectBuilder {
132
132
  }
133
133
  const predicateGetter = predicate !== undefined && predicate !== true ? addPredicate(predicate) : null
134
134
  const columnName = getColumnName(this.schema, entity, field)
135
- const parentValuePath = path.for(field)
136
- let ids = await this.getColumnValues(parentValuePath, columnName, predicateGetter)
135
+ const parentValuePath = path.forInternal('key').for(field)
136
+ let ids = await this.getColumnValues(path, parentValuePath, columnName, predicateGetter)
137
137
 
138
138
  const data = ids.length > 0 ? dataProvider(ids) : Promise.resolve({})
139
139
  this.hydrator.addPromise(
@@ -188,11 +188,12 @@ export class SelectBuilder {
188
188
  }
189
189
 
190
190
  private async getColumnValues(
191
+ tablePath: Path,
191
192
  columnPath: Path,
192
193
  columnName: string,
193
194
  predicateGetter: null | ColumnValueGetter<boolean>,
194
195
  ): Promise<Input.PrimaryValue[]> {
195
- this.qb = this.qb.select([columnPath.back().alias, columnName], columnPath.alias)
196
+ this.qb = this.qb.select([tablePath.alias, columnName], columnPath.alias)
196
197
  const rows = await this.rows
197
198
  const filteredRows = predicateGetter === null ? rows : rows.filter(predicateGetter)
198
199
  const columnAlias = columnPath.alias
@@ -60,7 +60,7 @@ export class FieldsVisitor implements Model.RelationByTypeVisitor<void>, Model.C
60
60
  const aliases: string[] = []
61
61
  for (const source of entity.view.idSource) {
62
62
  const columnName = getColumnName(this.schema, entity, source)
63
- const idSrcPath = columnPath.for(source + '__id_src')
63
+ const idSrcPath = columnPath.forInternal('idSrc').for(source)
64
64
  aliases.push(idSrcPath.alias)
65
65
  this.executionContext.addColumn({
66
66
  query: qb => qb.select([tableAlias, columnName], idSrcPath.alias),
@@ -4,6 +4,8 @@ import { GraphQLResolveInfo } from 'graphql'
4
4
  import { GraphQlQueryAstFactory } from './GraphQlQueryAstFactory.js'
5
5
  import { ObjectNode } from '../inputProcessing/index.js'
6
6
  import { executeReadOperations, paginate } from './ReadHelpers.js'
7
+ import { InvalidDataError } from '@contember/database'
8
+ import { UserError } from '../exception.js'
7
9
 
8
10
  export class ReadResolver {
9
11
  constructor(
@@ -15,31 +17,46 @@ export class ReadResolver {
15
17
  const queryAst = this.queryAstFactory.create(info)
16
18
  const fields = GraphQlQueryAstFactory.resolveObjectType(info.returnType).getFields()
17
19
  const mapper = this.mapperFactory.create()
18
- return executeReadOperations(queryAst, fields, mapper)
20
+ return await withInvalidDataAsUserError(() => executeReadOperations(queryAst, fields, mapper))
19
21
  }
20
22
 
21
23
  public async resolveTransaction(info: GraphQLResolveInfo) {
22
24
  const queryAst = this.queryAstFactory.create(info)
23
25
  const fields = GraphQlQueryAstFactory.resolveObjectType(info.returnType).getFields()
24
26
 
25
- return this.mapperFactory.transaction(async mapper => {
26
- return executeReadOperations(queryAst, fields, mapper)
27
- })
27
+ return await withInvalidDataAsUserError(() =>
28
+ this.mapperFactory.transaction(async mapper => {
29
+ return executeReadOperations(queryAst, fields, mapper)
30
+ })
31
+ )
28
32
  }
29
33
 
30
34
  public async resolveListQuery(entity: Model.Entity, info: GraphQLResolveInfo) {
31
35
  const queryAst: ObjectNode<Input.ListQueryInput> = this.queryAstFactory.create(info)
32
- return await this.mapperFactory.create().select(entity, queryAst, [])
36
+ return await withInvalidDataAsUserError(() => this.mapperFactory.create().select(entity, queryAst, []))
33
37
  }
34
38
 
35
39
  public async resolveGetQuery(entity: Model.Entity, info: GraphQLResolveInfo) {
36
40
  const queryAst: ObjectNode<Input.UniqueQueryInput> = this.queryAstFactory.create(info)
37
- return await this.mapperFactory.create().selectUnique(entity, queryAst, [])
41
+ return await withInvalidDataAsUserError(() => this.mapperFactory.create().selectUnique(entity, queryAst, []))
38
42
  }
39
43
 
40
44
  public async resolvePaginationQuery(entity: Model.Entity, info: GraphQLResolveInfo) {
41
45
  const queryAst: ObjectNode<Input.PaginationQueryInput> = this.queryAstFactory.create(info)
42
46
  const mapper = this.mapperFactory.create()
43
- return await paginate(mapper, entity, queryAst)
47
+ return await withInvalidDataAsUserError(() => paginate(mapper, entity, queryAst))
48
+ }
49
+ }
50
+
51
+ // A filter value PostgreSQL cannot parse is a client input error, as convertError reports it for mutations.
52
+ // Reads issued inside a mutation (its result nodes, `transaction { query }`) do not pass through here.
53
+ const withInvalidDataAsUserError = async <T>(cb: () => Promise<T>): Promise<T> => {
54
+ try {
55
+ return await cb()
56
+ } catch (e) {
57
+ if (e instanceof InvalidDataError) {
58
+ throw new UserError(e.originalMessage, { cause: e })
59
+ }
60
+ throw e
44
61
  }
45
62
  }
@@ -43,23 +43,23 @@ test('Filter by has many with additional join', async () => {
43
43
  executes: [
44
44
  {
45
45
  sql: SQL`
46
- select "root_"."id" as "root_id", "root_"."short_name" as "root_shortName", "root_"."id" as "root_id"
46
+ select "root_"."id" as "root_id", "root_"."short_name" as "root_shortName", "root_"."id" as "root_$key_id"
47
47
  from "public"."person" as "root_"
48
48
  where exists (select 1
49
49
  from "public"."person_locale" as "root_locales"
50
50
  left join "public"."language" as "root_locales_locale" on "root_locales"."locale_id" = "root_locales_locale"."id"
51
51
  where "root_"."id" = "root_locales"."person_id" and "root_locales_locale"."slug" = ? and not("root_locales"."url_slug" is null))`,
52
- response: { rows: [{ root_id: testUuid(1), root_shortName: 'John' }] },
52
+ response: { rows: [{ root_id: testUuid(1), 'root_$key_id': testUuid(1), root_shortName: 'John' }] },
53
53
  parameters: ['cs'],
54
54
  },
55
55
  {
56
- sql: SQL`select "root_"."person_id" as "root_person", "root_"."url_slug" as "root_urlSlug", "root_"."id" as "root_id"
56
+ sql: SQL`select "root_"."person_id" as "root_$index_person", "root_"."url_slug" as "root_urlSlug", "root_"."id" as "root_id"
57
57
  from "public"."person_locale" as "root_"
58
58
  left join "public"."language" as "root_locale" on "root_"."locale_id" = "root_locale"."id"
59
59
  where "root_locale"."slug" = ? and "root_"."person_id" in (?)`,
60
60
  parameters: ['cs', testUuid(1)],
61
61
  response: {
62
- rows: [{ root_person: testUuid(1), root_urlSlug: 'john', root_id: testUuid(2) }],
62
+ rows: [{ 'root_$index_person': testUuid(1), root_urlSlug: 'john', root_id: testUuid(2) }],
63
63
  },
64
64
  },
65
65
  ],
@@ -27,7 +27,7 @@ test('delete post by id', async () => {
27
27
  {
28
28
  sql: SQL`select
29
29
  "root_"."id" as "root_id",
30
- "root_"."author_id" as "root_author"
30
+ "root_"."author_id" as "root_$key_author"
31
31
  from "public"."post" as "root_"
32
32
  where "root_"."id" = ?`,
33
33
  parameters: [testUuid(1)],
@@ -35,14 +35,14 @@ test('delete post by id', async () => {
35
35
  rows: [
36
36
  {
37
37
  root_id: testUuid(1),
38
- root_author: testUuid(2),
38
+ 'root_$key_author': testUuid(2),
39
39
  },
40
40
  ],
41
41
  },
42
42
  },
43
43
  {
44
44
  sql: SQL`select
45
- "root_"."id" as "root_id",
45
+ "root_"."id" as "root_$index_id",
46
46
  "root_"."name" as "root_name",
47
47
  "root_"."id" as "root_id"
48
48
  from "public"."author" as "root_"
@@ -52,6 +52,7 @@ test('delete post by id', async () => {
52
52
  rows: [
53
53
  {
54
54
  root_id: testUuid(2),
55
+ 'root_$index_id': testUuid(2),
55
56
  root_name: 'John',
56
57
  },
57
58
  ],
@@ -43,7 +43,7 @@ test('different predicate on row and field level', async () => {
43
43
  sql: SQL`
44
44
  select
45
45
  "root_"."id" as "root_id",
46
- "root_"."username" = ? as "root___predicate_authorPredicate",
46
+ "root_"."username" = ? as "root_$predicate_authorPredicate",
47
47
  "root_"."username" as "root_username"
48
48
  from "public"."author" as "root_" where not("root_"."username" is null)
49
49
  `,
@@ -52,7 +52,7 @@ test('different predicate on row and field level', async () => {
52
52
  rows: [
53
53
  {
54
54
  root_id: testUuid(1),
55
- root___predicate_authorPredicate: true,
55
+ 'root_$predicate_authorPredicate': true,
56
56
  root_username: 'johndoe',
57
57
  },
58
58
  ],
@@ -89,7 +89,7 @@ test('different predicate on row and field level - false', async () => {
89
89
  sql: SQL`
90
90
  select
91
91
  "root_"."id" as "root_id",
92
- "root_"."username" = ? as "root___predicate_authorPredicate",
92
+ "root_"."username" = ? as "root_$predicate_authorPredicate",
93
93
  "root_"."username" as "root_username"
94
94
  from "public"."author" as "root_" where not("root_"."username" is null)
95
95
  `,
@@ -98,7 +98,7 @@ test('different predicate on row and field level - false', async () => {
98
98
  rows: [
99
99
  {
100
100
  root_id: testUuid(1),
101
- root___predicate_authorPredicate: false,
101
+ 'root_$predicate_authorPredicate': false,
102
102
  root_username: 'johndoe',
103
103
  },
104
104
  ],
@@ -64,7 +64,7 @@ test('querying id and title', async () => {
64
64
  {
65
65
  sql: SQL`SELECT
66
66
  "root_"."id" AS "root_id",
67
- "root_"."locale" IN (?) AS "root___predicate_localePredicate",
67
+ "root_"."locale" IN (?) as "root_$predicate_localePredicate",
68
68
  "root_"."title" AS "root_title"
69
69
  FROM "public"."post_locale" AS "root_"`,
70
70
  parameters: ['cs'],
@@ -73,12 +73,12 @@ test('querying id and title', async () => {
73
73
  {
74
74
  root_id: testUuid(1),
75
75
  root_title: null,
76
- root___predicate_localePredicate: false,
76
+ 'root_$predicate_localePredicate': false,
77
77
  },
78
78
  {
79
79
  root_id: testUuid(2),
80
80
  root_title: 'bar',
81
- root___predicate_localePredicate: true,
81
+ 'root_$predicate_localePredicate': true,
82
82
  },
83
83
  ],
84
84
  },
@@ -131,7 +131,7 @@ test('querying with extra where', async () => {
131
131
  {
132
132
  sql: SQL`select
133
133
  "root_"."id" as "root_id",
134
- "root_"."locale" in (?) as "root___predicate_localePredicate",
134
+ "root_"."locale" in (?) as "root_$predicate_localePredicate",
135
135
  "root_"."title" as "root_title"
136
136
  from "public"."post_locale" as "root_"
137
137
  where "root_"."title" = ? and "root_"."locale" in (?)`,
@@ -141,7 +141,7 @@ test('querying with extra where', async () => {
141
141
  {
142
142
  root_id: testUuid(1),
143
143
  root_title: 'foo',
144
- root___predicate_localePredicate: true,
144
+ 'root_$predicate_localePredicate': true,
145
145
  },
146
146
  ],
147
147
  },
@@ -216,7 +216,7 @@ test('not defined acl variable', async () => {
216
216
  {
217
217
  sql: SQL`
218
218
  select "root_"."id" as "root_id",
219
- false as "root___predicate_localePredicate",
219
+ false as "root_$predicate_localePredicate",
220
220
  "root_"."title" as "root_title"
221
221
  from "public"."post_locale" as "root_"`,
222
222
  parameters: [],
@@ -252,7 +252,7 @@ test('querying on nested field', async () => {
252
252
  executes: [
253
253
  {
254
254
  sql: SQL`select
255
- "root_"."id" as "root_id",
255
+ "root_"."id" as "root_$key_id",
256
256
  "root_"."id" as "root_id"
257
257
  from "public"."post" as "root_"`,
258
258
  parameters: [],
@@ -260,9 +260,11 @@ test('querying on nested field', async () => {
260
260
  rows: [
261
261
  {
262
262
  root_id: testUuid(1),
263
+ 'root_$key_id': testUuid(1),
263
264
  },
264
265
  {
265
266
  root_id: testUuid(2),
267
+ 'root_$key_id': testUuid(2),
266
268
  },
267
269
  ],
268
270
  },
@@ -271,7 +273,7 @@ test('querying on nested field', async () => {
271
273
  sql: SQL`
272
274
  select "root_"."post_id" as "__grouping_key",
273
275
  "root_"."id" as "root_id",
274
- "root_"."locale" in (?) as "root___predicate_localePredicate",
276
+ "root_"."locale" in (?) as "root_$predicate_localePredicate",
275
277
  "root_"."title" as "root_title"
276
278
  from "public"."post_locale" as "root_" where "root_"."post_id" in (?, ?)`,
277
279
  parameters: ['cs', testUuid(1), testUuid(2)],
@@ -280,13 +282,13 @@ test('querying on nested field', async () => {
280
282
  {
281
283
  __grouping_key: testUuid(1),
282
284
  root_id: testUuid(3),
283
- root___predicate_localePredicate: false,
285
+ 'root_$predicate_localePredicate': false,
284
286
  root_title: 'foo',
285
287
  },
286
288
  {
287
289
  __grouping_key: testUuid(2),
288
290
  root_id: testUuid(4),
289
- root___predicate_localePredicate: true,
291
+ 'root_$predicate_localePredicate': true,
290
292
  root_title: 'bar',
291
293
  },
292
294
  ],
@@ -91,12 +91,12 @@ test('_meta.readable and _meta.updatable follow the nested permission set throug
91
91
  }`,
92
92
  executes: [
93
93
  {
94
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
94
+ sql: SQL`select "root_"."id" as "root_$key_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
95
95
  parameters: [],
96
96
  response: {
97
97
  rows: [
98
- { root_id: testUuid(1) },
99
- { root_id: testUuid(2) },
98
+ { root_id: testUuid(1), 'root_$key_id': testUuid(1) },
99
+ { root_id: testUuid(2), 'root_$key_id': testUuid(2) },
100
100
  ],
101
101
  },
102
102
  },
@@ -196,12 +196,12 @@ test('_meta.readable compiles a through-only cell predicate against the nested p
196
196
  `,
197
197
  executes: [
198
198
  {
199
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
199
+ sql: SQL`select "root_"."id" as "root_$key_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
200
200
  parameters: [],
201
201
  response: {
202
202
  rows: [
203
- { root_id: testUuid(1) },
204
- { root_id: testUuid(2) },
203
+ { root_id: testUuid(1), 'root_$key_id': testUuid(1) },
204
+ { root_id: testUuid(2), 'root_$key_id': testUuid(2) },
205
205
  ],
206
206
  },
207
207
  },
@@ -209,14 +209,14 @@ test('_meta.readable compiles a through-only cell predicate against the nested p
209
209
  sql: SQL`
210
210
  select "root_"."post_id" as "__grouping_key",
211
211
  "root_"."id" as "root_id",
212
- "root_"."visible" = ? as "root___predicate_titleVisible"
212
+ "root_"."visible" = ? as "root_$predicate_titleVisible"
213
213
  from "public"."post_locale" as "root_" where "root_"."post_id" in (?, ?)
214
214
  `,
215
215
  parameters: [true, testUuid(1), testUuid(2)],
216
216
  response: {
217
217
  rows: [
218
- { __grouping_key: testUuid(1), root_id: testUuid(3), root___predicate_titleVisible: true },
219
- { __grouping_key: testUuid(2), root_id: testUuid(4), root___predicate_titleVisible: false },
218
+ { __grouping_key: testUuid(1), root_id: testUuid(3), 'root_$predicate_titleVisible': true },
219
+ { __grouping_key: testUuid(2), root_id: testUuid(4), 'root_$predicate_titleVisible': false },
220
220
  ],
221
221
  },
222
222
  },
@@ -277,12 +277,12 @@ test('nested _meta.updatable resolves its predicate against the nested permissio
277
277
  `,
278
278
  executes: [
279
279
  {
280
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
280
+ sql: SQL`select "root_"."id" as "root_$key_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
281
281
  parameters: [],
282
282
  response: {
283
283
  rows: [
284
- { root_id: testUuid(1) },
285
- { root_id: testUuid(2) },
284
+ { root_id: testUuid(1), 'root_$key_id': testUuid(1) },
285
+ { root_id: testUuid(2), 'root_$key_id': testUuid(2) },
286
286
  ],
287
287
  },
288
288
  },
@@ -291,14 +291,14 @@ test('nested _meta.updatable resolves its predicate against the nested permissio
291
291
  sql: SQL`
292
292
  select "root_"."post_id" as "__grouping_key",
293
293
  "root_"."id" as "root_id",
294
- "root_"."visible" = ? as "root___predicate_throughUpdatable"
294
+ "root_"."visible" = ? as "root_$predicate_throughUpdatable"
295
295
  from "public"."post_locale" as "root_" where "root_"."post_id" in (?, ?)
296
296
  `,
297
297
  parameters: [false, testUuid(1), testUuid(2)],
298
298
  response: {
299
299
  rows: [
300
- { __grouping_key: testUuid(1), root_id: testUuid(3), root___predicate_throughUpdatable: true },
301
- { __grouping_key: testUuid(2), root_id: testUuid(4), root___predicate_throughUpdatable: false },
300
+ { __grouping_key: testUuid(1), root_id: testUuid(3), 'root_$predicate_throughUpdatable': true },
301
+ { __grouping_key: testUuid(2), root_id: testUuid(4), 'root_$predicate_throughUpdatable': false },
302
302
  ],
303
303
  },
304
304
  },
@@ -70,26 +70,26 @@ test('querying post + author name', async () => {
70
70
  {
71
71
  sql: SQL`select
72
72
  "root_"."id" as "root_id",
73
- "root_"."author_id" as "root_author"
73
+ "root_"."author_id" as "root_$key_author"
74
74
  from "public"."post" as "root_"`,
75
75
  parameters: [],
76
76
  response: {
77
77
  rows: [
78
78
  {
79
79
  root_id: testUuid(1),
80
- root_author: testUuid(3),
80
+ 'root_$key_author': testUuid(3),
81
81
  },
82
82
  {
83
83
  root_id: testUuid(2),
84
- root_author: testUuid(4),
84
+ 'root_$key_author': testUuid(4),
85
85
  },
86
86
  ],
87
87
  },
88
88
  },
89
89
  {
90
90
  sql: SQL`SELECT
91
- "root_"."id" AS "root_id",
92
- "root_country"."name" IN (?) AS "root___predicate_countryPredicate",
91
+ "root_"."id" as "root_$index_id",
92
+ "root_country"."name" IN (?) as "root_$predicate_countryPredicate",
93
93
  "root_"."name" AS "root_name",
94
94
  "root_"."id" AS "root_id"
95
95
  FROM "public"."author" AS "root_"
@@ -100,7 +100,8 @@ test('querying post + author name', async () => {
100
100
  rows: [
101
101
  {
102
102
  root_id: testUuid(3),
103
- root___predicate_countryPredicate: true,
103
+ 'root_$index_id': testUuid(3),
104
+ 'root_$predicate_countryPredicate': true,
104
105
  root_name: 'John',
105
106
  },
106
107
  ],
@@ -90,27 +90,27 @@ test('filter and order by in a nested relation fetch keep the read guard of a no
90
90
  }`,
91
91
  executes: [
92
92
  {
93
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."author" as "root_" where "root_"."is_public" = ?`,
93
+ sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_$key_id" from "public"."author" as "root_" where "root_"."is_public" = ?`,
94
94
  parameters: [true],
95
95
  response: {
96
- rows: [{ root_id: testUuid(1) }],
96
+ rows: [{ root_id: testUuid(1), 'root_$key_id': testUuid(1) }],
97
97
  },
98
98
  },
99
99
  {
100
- sql: SQL`select "root_"."author_id" as "__grouping_key", "root_"."id" as "root_id", "root_"."category_id" as "root_category"
100
+ sql: SQL`select "root_"."author_id" as "__grouping_key", "root_"."id" as "root_id", "root_"."category_id" as "root_$key_category"
101
101
  from "public"."post" as "root_"
102
102
  where "root_"."author_id" in (?)`,
103
103
  parameters: [testUuid(1)],
104
104
  response: {
105
- rows: [{ __grouping_key: testUuid(1), root_id: testUuid(10), root_category: testUuid(20) }],
105
+ rows: [{ __grouping_key: testUuid(1), root_id: testUuid(10), 'root_$key_category': testUuid(20) }],
106
106
  },
107
107
  },
108
108
  {
109
109
  sql:
110
- SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."category" as "root_" where "root_"."id" in (?)`,
110
+ SQL`select "root_"."id" as "root_$index_id", "root_"."id" as "root_id", "root_"."id" as "root_$key_id" from "public"."category" as "root_" where "root_"."id" in (?)`,
111
111
  parameters: [testUuid(20)],
112
112
  response: {
113
- rows: [{ root_id: testUuid(20) }],
113
+ rows: [{ root_id: testUuid(20), 'root_$key_id': testUuid(20), 'root_$index_id': testUuid(20) }],
114
114
  },
115
115
  },
116
116
  {
@@ -43,7 +43,7 @@ test('order by a cell-level field guards the order key with the field read predi
43
43
  sql: SQL`
44
44
  select
45
45
  "root_"."id" as "root_id",
46
- "root_"."username" = ? as "root___predicate_authorPredicate",
46
+ "root_"."username" = ? as "root_$predicate_authorPredicate",
47
47
  "root_"."username" as "root_username"
48
48
  from "public"."author" as "root_"
49
49
  where not("root_"."username" is null)
@@ -54,7 +54,7 @@ test('order by a cell-level field guards the order key with the field read predi
54
54
  rows: [
55
55
  {
56
56
  root_id: testUuid(1),
57
- root___predicate_authorPredicate: true,
57
+ 'root_$predicate_authorPredicate': true,
58
58
  root_username: 'johndoe',
59
59
  },
60
60
  ],
@@ -412,10 +412,10 @@ test('order by on a many-has-many junction fetch guards the order key against th
412
412
  }`,
413
413
  executes: [
414
414
  {
415
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."post" as "root_"`,
415
+ sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_$key_id" from "public"."post" as "root_"`,
416
416
  parameters: [],
417
417
  response: {
418
- rows: [{ root_id: testUuid(1) }],
418
+ rows: [{ root_id: testUuid(1), 'root_$key_id': testUuid(1) }],
419
419
  },
420
420
  },
421
421
  {
@@ -476,10 +476,10 @@ test('order by a cell-level field guards the order key in the window-function (l
476
476
  }`,
477
477
  executes: [
478
478
  {
479
- sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_id" from "public"."author" as "root_"`,
479
+ sql: SQL`select "root_"."id" as "root_id", "root_"."id" as "root_$key_id" from "public"."author" as "root_"`,
480
480
  parameters: [],
481
481
  response: {
482
- rows: [{ root_id: testUuid(1) }],
482
+ rows: [{ root_id: testUuid(1), 'root_$key_id': testUuid(1) }],
483
483
  },
484
484
  },
485
485
  {