@sap/cds 5.5.5 → 5.6.3

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 (207) hide show
  1. package/CHANGELOG.md +139 -1
  2. package/apis/services.d.ts +31 -1
  3. package/app/index.js +22 -11
  4. package/bin/build/buildTaskFactory.js +1 -1
  5. package/bin/build/provider/buildTaskProviderInternal.js +1 -1
  6. package/bin/build/provider/fiori/index.js +1 -1
  7. package/bin/build/provider/hana/2migration.js +8 -7
  8. package/bin/build/provider/java-cf/index.js +1 -1
  9. package/bin/deploy/to-hana/hana.js +1 -17
  10. package/common.cds +8 -0
  11. package/lib/compile/to/sql.js +22 -2
  12. package/lib/connect/bindings.js +2 -1
  13. package/lib/core/reflect.js +4 -1
  14. package/lib/env/index.js +180 -42
  15. package/lib/env/requires.js +16 -1
  16. package/lib/i18n/localize.js +33 -5
  17. package/lib/index.js +3 -3
  18. package/lib/log/format/kibana.js +6 -2
  19. package/lib/ql/Query.js +1 -0
  20. package/lib/ql/SELECT.js +15 -8
  21. package/lib/ql/Whereable.js +5 -0
  22. package/lib/req/context.js +13 -5
  23. package/lib/serve/Service-dispatch.js +8 -1
  24. package/lib/utils/axios.js +7 -0
  25. package/lib/utils/data.js +1 -1
  26. package/lib/utils/tests.js +1 -1
  27. package/libx/_runtime/audit/Service.js +18 -18
  28. package/libx/_runtime/audit/generic/personal/access.js +1 -1
  29. package/libx/_runtime/audit/generic/personal/modification.js +3 -2
  30. package/libx/_runtime/audit/generic/personal/utils.js +23 -63
  31. package/libx/_runtime/cds-services/adapter/odata-v4/Dispatcher.js +4 -0
  32. package/libx/_runtime/cds-services/adapter/odata-v4/OData.js +37 -35
  33. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/create.js +3 -1
  34. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/error.js +5 -5
  35. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js +13 -7
  36. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/update.js +84 -34
  37. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/ExpressionToCQN.js +12 -4
  38. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/applyToCQN.js +9 -3
  39. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/expandToCQN.js +8 -6
  40. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/index.js +1 -3
  41. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/readToCQN.js +13 -11
  42. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/selectHelper.js +11 -95
  43. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/uri/ResourcePathParser.js +17 -11
  44. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/uri/UriParser.js +2 -1
  45. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/SetResponseHeadersCommand.js +1 -0
  46. package/libx/_runtime/cds-services/adapter/odata-v4/to.js +6 -2
  47. package/libx/_runtime/cds-services/adapter/odata-v4/utils/data.js +3 -34
  48. package/libx/_runtime/cds-services/adapter/odata-v4/utils/handlerUtils.js +3 -3
  49. package/libx/_runtime/cds-services/adapter/odata-v4/utils/result.js +64 -31
  50. package/libx/_runtime/cds-services/adapter/odata-v4/utils/stream.js +10 -5
  51. package/libx/_runtime/cds-services/adapter/rest/handlers/operation.js +1 -1
  52. package/libx/_runtime/cds-services/adapter/rest/handlers/update.js +1 -1
  53. package/libx/_runtime/cds-services/adapter/rest/rest-to-cqn/index.js +1 -3
  54. package/libx/_runtime/cds-services/adapter/rest/utils/validation-checks.js +20 -21
  55. package/libx/_runtime/cds-services/services/utils/columns.js +6 -1
  56. package/libx/_runtime/cds-services/services/utils/compareJson.js +1 -8
  57. package/libx/_runtime/cds-services/services/utils/differ.js +7 -26
  58. package/libx/_runtime/cds-services/services/utils/handlerUtils.js +2 -4
  59. package/libx/_runtime/cds-services/util/assert.js +29 -13
  60. package/libx/_runtime/cds.js +2 -1
  61. package/libx/_runtime/common/aspects/Association.js +72 -0
  62. package/libx/_runtime/common/aspects/any.js +8 -45
  63. package/libx/_runtime/common/aspects/entity.js +0 -1
  64. package/libx/_runtime/common/aspects/relation.js +40 -0
  65. package/libx/_runtime/common/aspects/utils.js +73 -1
  66. package/libx/_runtime/common/auth/strategies/utils/uaa.js +10 -14
  67. package/libx/_runtime/common/composition/data.js +3 -2
  68. package/libx/_runtime/common/composition/delete.js +3 -1
  69. package/libx/_runtime/common/composition/tree.js +23 -18
  70. package/libx/_runtime/common/composition/update.js +9 -1
  71. package/libx/_runtime/common/composition/utils.js +34 -8
  72. package/libx/_runtime/common/error/frontend.js +6 -1
  73. package/libx/_runtime/common/generic/auth.js +5 -9
  74. package/libx/_runtime/common/generic/crud.js +2 -2
  75. package/libx/_runtime/common/generic/etag.js +11 -8
  76. package/libx/_runtime/common/generic/input.js +3 -3
  77. package/libx/_runtime/common/generic/paging.js +9 -5
  78. package/libx/_runtime/common/generic/put.js +3 -2
  79. package/libx/_runtime/common/generic/sorting.js +3 -3
  80. package/libx/_runtime/common/generic/temporal.js +3 -3
  81. package/libx/_runtime/common/utils/cqn.js +20 -1
  82. package/libx/_runtime/common/utils/cqn2cqn4sql.js +125 -139
  83. package/libx/_runtime/common/utils/csn.js +50 -52
  84. package/libx/_runtime/common/utils/foreignKeyPropagations.js +41 -176
  85. package/libx/_runtime/common/utils/generateOnCond.js +40 -70
  86. package/libx/_runtime/common/utils/{enrichWithKeysFromWhere.js → keys.js} +29 -28
  87. package/libx/_runtime/common/utils/postProcessing.js +3 -0
  88. package/libx/_runtime/common/utils/propagateForeignKeys.js +84 -0
  89. package/libx/_runtime/common/utils/resolveStructured.js +1 -1
  90. package/libx/_runtime/common/utils/resolveView.js +7 -5
  91. package/libx/_runtime/common/utils/rewriteAsterisks.js +94 -0
  92. package/libx/_runtime/common/utils/search2cqn4sql.js +9 -8
  93. package/libx/_runtime/common/utils/template.js +54 -46
  94. package/libx/_runtime/db/Service.js +9 -2
  95. package/libx/_runtime/db/expand/expandCQNToJoin.js +54 -33
  96. package/libx/_runtime/db/expand/rawToExpanded.js +2 -1
  97. package/libx/_runtime/db/generic/arrayed.js +13 -28
  98. package/libx/_runtime/db/generic/create.js +1 -0
  99. package/libx/_runtime/db/generic/input.js +7 -11
  100. package/libx/_runtime/db/generic/integrity.js +2 -2
  101. package/libx/_runtime/db/generic/rewrite.js +2 -5
  102. package/libx/_runtime/db/generic/update.js +1 -0
  103. package/libx/_runtime/db/query/read.js +9 -4
  104. package/libx/_runtime/db/sql-builder/SelectBuilder.js +7 -2
  105. package/libx/_runtime/db/sql-builder/annotations.js +1 -0
  106. package/libx/_runtime/db/utils/columns.js +14 -43
  107. package/libx/_runtime/fiori/generic/activate.js +3 -2
  108. package/libx/_runtime/fiori/generic/before.js +2 -2
  109. package/libx/_runtime/fiori/generic/cancel.js +3 -2
  110. package/libx/_runtime/fiori/generic/delete.js +3 -2
  111. package/libx/_runtime/fiori/generic/edit.js +3 -3
  112. package/libx/_runtime/fiori/generic/new.js +2 -2
  113. package/libx/_runtime/fiori/generic/patch.js +2 -2
  114. package/libx/_runtime/fiori/generic/prepare.js +2 -2
  115. package/libx/_runtime/fiori/generic/read.js +45 -63
  116. package/libx/_runtime/fiori/generic/readOverDraft.js +4 -4
  117. package/libx/_runtime/fiori/uiflex/extensibility/index.cds +15 -0
  118. package/libx/_runtime/fiori/uiflex/extensibility/index.js +148 -0
  119. package/libx/_runtime/fiori/uiflex/handler/transformREAD.js +119 -0
  120. package/libx/_runtime/fiori/uiflex/handler/transformRESULT.js +43 -0
  121. package/libx/_runtime/fiori/uiflex/handler/transformWRITE.js +62 -0
  122. package/libx/_runtime/fiori/uiflex/index.js +35 -0
  123. package/libx/_runtime/fiori/uiflex/utils.js +78 -0
  124. package/libx/_runtime/fiori/utils/handler.js +3 -13
  125. package/libx/_runtime/fiori/utils/where.js +6 -1
  126. package/libx/_runtime/hana/pool.js +12 -11
  127. package/libx/_runtime/hana/search2cqn4sql.js +34 -43
  128. package/libx/_runtime/hana/searchToContains.js +3 -3
  129. package/libx/_runtime/index.js +5 -2
  130. package/libx/_runtime/messaging/AMQPWebhookMessaging.js +1 -1
  131. package/libx/_runtime/messaging/common-utils/AMQPClient.js +16 -3
  132. package/libx/_runtime/messaging/common-utils/connections.js +11 -14
  133. package/libx/_runtime/messaging/common-utils/naming-conventions.js +1 -1
  134. package/libx/_runtime/messaging/enterprise-messaging-utils/registerEndpoints.js +2 -1
  135. package/libx/_runtime/messaging/message-queuing.js +18 -0
  136. package/libx/_runtime/remote/Service.js +20 -4
  137. package/libx/_runtime/remote/utils/client-types.d.ts +7 -0
  138. package/libx/_runtime/remote/utils/client.js +117 -23
  139. package/libx/_runtime/sqlite/Service.js +2 -2
  140. package/libx/_runtime/sqlite/convertAssocToOneManaged.js +1 -3
  141. package/libx/gql/GraphQLAdapter.js +33 -0
  142. package/libx/gql/constants/adapter.js +69 -0
  143. package/libx/gql/constants/cds.js +18 -0
  144. package/libx/gql/constants/graphql.js +33 -0
  145. package/libx/gql/resolvers/crud/create.js +15 -0
  146. package/libx/gql/resolvers/crud/delete.js +24 -0
  147. package/libx/gql/resolvers/crud/index.js +6 -0
  148. package/libx/gql/resolvers/crud/read.js +25 -0
  149. package/libx/gql/resolvers/crud/update.js +31 -0
  150. package/libx/gql/resolvers/crud/utils/index.js +36 -0
  151. package/libx/gql/resolvers/field.js +5 -0
  152. package/libx/gql/resolvers/index.js +7 -0
  153. package/libx/gql/resolvers/mutation.js +23 -0
  154. package/libx/gql/resolvers/parse/ast/enrich.js +51 -0
  155. package/libx/gql/resolvers/parse/ast/fragment.js +11 -0
  156. package/libx/gql/resolvers/parse/ast/fromObject.js +39 -0
  157. package/libx/gql/resolvers/parse/ast/index.js +3 -0
  158. package/libx/gql/resolvers/parse/ast/meta.js +4 -0
  159. package/libx/gql/resolvers/parse/ast/variable.js +7 -0
  160. package/libx/gql/resolvers/parse/ast2cqn/columns.js +42 -0
  161. package/libx/gql/resolvers/parse/ast2cqn/entries.js +31 -0
  162. package/libx/gql/resolvers/parse/ast2cqn/index.js +8 -0
  163. package/libx/gql/resolvers/parse/ast2cqn/limit.js +6 -0
  164. package/libx/gql/resolvers/parse/ast2cqn/orderBy.js +24 -0
  165. package/libx/gql/resolvers/parse/ast2cqn/utils/index.js +3 -0
  166. package/libx/gql/resolvers/parse/ast2cqn/where.js +70 -0
  167. package/libx/gql/resolvers/parse/utils/index.js +8 -0
  168. package/libx/gql/resolvers/query.js +13 -0
  169. package/libx/gql/resolvers/root.js +34 -0
  170. package/libx/gql/schema/generate.js +18 -0
  171. package/libx/gql/schema/index.js +5 -0
  172. package/libx/gql/schema/mutation.js +76 -0
  173. package/libx/gql/schema/query.js +108 -0
  174. package/libx/gql/schema/typeDefMap.js +45 -0
  175. package/libx/gql/schema/utils/index.js +54 -0
  176. package/libx/gql/utils/index.js +12 -0
  177. package/libx/{_runtime/odata/cqn2odata.js → odata/cqn2odata/index.js} +39 -100
  178. package/libx/odata/index.js +80 -0
  179. package/libx/odata/odata2cqn/afterburner.js +170 -0
  180. package/libx/{_runtime/odata/odata2cqn.pegjs → odata/odata2cqn/grammar.pegjs} +102 -123
  181. package/libx/odata/odata2cqn/index.js +3 -0
  182. package/libx/odata/odata2cqn/parser.js +1 -0
  183. package/libx/odata/utils/index.js +64 -0
  184. package/libx/rest/RestAdapter.js +101 -0
  185. package/libx/rest/RestRequest.js +30 -0
  186. package/libx/rest/index.js +3 -0
  187. package/libx/rest/middleware/auth.js +22 -0
  188. package/libx/rest/middleware/content.js +15 -0
  189. package/libx/rest/middleware/create.js +40 -0
  190. package/libx/rest/middleware/delete.js +20 -0
  191. package/libx/rest/middleware/error.js +56 -0
  192. package/libx/rest/middleware/operation.js +39 -0
  193. package/libx/rest/middleware/parse.js +90 -0
  194. package/libx/rest/middleware/read.js +29 -0
  195. package/libx/rest/middleware/update.js +42 -0
  196. package/libx/rest/utils/data.js +65 -0
  197. package/package.json +4 -1
  198. package/server.js +29 -7
  199. package/libx/_runtime/cds-services/services/utils/diff.js +0 -53
  200. package/libx/_runtime/cds-services/util/auditlog.js +0 -247
  201. package/libx/_runtime/cds-services/util/xsenv.js +0 -51
  202. package/libx/_runtime/common/utils/backlinks.js +0 -83
  203. package/libx/_runtime/common/utils/rewriteAsterisk.js +0 -72
  204. package/libx/_runtime/odata/index.js +0 -55
  205. package/libx/_runtime/odata/odata2cqn.js +0 -1
  206. package/libx/_runtime/odata/readToCqn.js +0 -129
  207. package/libx/_runtime/remote/cqn2odata/index.js +0 -2
@@ -0,0 +1,39 @@
1
+ const { AST_NODE_KIND } = require('../../../constants/graphql')
2
+
3
+ const valueToGenericScalarValue = value => ({
4
+ kind: 'GenericScalarValue',
5
+ value
6
+ })
7
+
8
+ const keyToName = key => ({
9
+ kind: AST_NODE_KIND.Name,
10
+ value: key
11
+ })
12
+
13
+ const keyValueToObjectField = (k, v) => ({
14
+ kind: AST_NODE_KIND.ObjectField,
15
+ name: keyToName(k),
16
+ value: variableToValue(v)
17
+ })
18
+
19
+ const objectToObjectValue = object => ({
20
+ kind: AST_NODE_KIND.ObjectValue,
21
+ fields: Object.entries(object).map(([k, v]) => keyValueToObjectField(k, v))
22
+ })
23
+
24
+ const arrayToListValue = array => ({
25
+ kind: AST_NODE_KIND.ListValue,
26
+ values: array.map(a => objectToObjectValue(a))
27
+ })
28
+
29
+ const variableToValue = variable => {
30
+ if (Array.isArray(variable)) {
31
+ return arrayToListValue(variable)
32
+ } else if (typeof variable === 'object') {
33
+ return objectToObjectValue(variable)
34
+ } else {
35
+ return valueToGenericScalarValue(variable)
36
+ }
37
+ }
38
+
39
+ module.exports = variableValue => variableToValue(variableValue)
@@ -0,0 +1,3 @@
1
+ const enrichAST = require('./enrich')
2
+
3
+ module.exports = { enrichAST }
@@ -0,0 +1,4 @@
1
+ const removeMetaFieldsFromSelections = selections =>
2
+ selections.filter(selection => selection.name.value !== '__typename')
3
+
4
+ module.exports = removeMetaFieldsFromSelections
@@ -0,0 +1,7 @@
1
+ const objectToAST = require('./fromObject')
2
+
3
+ const getVariableValueForVariable = (info, variable) => info.variableValues[variable.name.value]
4
+
5
+ const substituteVariable = (info, variable) => objectToAST(getVariableValueForVariable(info, variable))
6
+
7
+ module.exports = substituteVariable
@@ -0,0 +1,42 @@
1
+ const { getArgumentByName } = require('./utils')
2
+ const astToWhere = require('./where')
3
+ const astToOrderBy = require('./orderBy')
4
+ const astToLimit = require('./limit')
5
+ const { ARGUMENT } = require('../../../constants/adapter')
6
+
7
+ const astToColumns = selections => {
8
+ let columns = []
9
+
10
+ for (const selection of selections) {
11
+ const column = { ref: [selection.name.value] }
12
+ if (selection.alias) {
13
+ column.as = selection.alias.value
14
+ }
15
+
16
+ if (selection.selectionSet && selection.selectionSet.selections) {
17
+ column.expand = astToColumns(selection.selectionSet.selections)
18
+ }
19
+
20
+ const filter = getArgumentByName(selection.arguments, ARGUMENT.FILTER)
21
+ if (filter) {
22
+ column.where = astToWhere(filter).xpr
23
+ }
24
+
25
+ const orderBy = getArgumentByName(selection.arguments, ARGUMENT.ORDER_BY)
26
+ if (orderBy) {
27
+ column.orderBy = astToOrderBy(orderBy)
28
+ }
29
+
30
+ const top = getArgumentByName(selection.arguments, ARGUMENT.TOP)
31
+ const skip = getArgumentByName(selection.arguments, ARGUMENT.SKIP)
32
+ if (top) {
33
+ column.limit = astToLimit(top, skip)
34
+ }
35
+
36
+ columns.push(column)
37
+ }
38
+
39
+ return columns
40
+ }
41
+
42
+ module.exports = astToColumns
@@ -0,0 +1,31 @@
1
+ const { isListValue, isObjectValue } = require('../utils')
2
+
3
+ const parseObjectField = objectField => {
4
+ const value = objectField.value
5
+ if (isListValue(value)) {
6
+ return parseListValue(value)
7
+ } else if (isObjectValue(value)) {
8
+ return parseObjectValue(value)
9
+ } else {
10
+ return value.value
11
+ }
12
+ }
13
+
14
+ const parseObjectValue = objectValue =>
15
+ objectValue.fields.reduce((entry, objectField) => {
16
+ entry[objectField.name.value] = parseObjectField(objectField)
17
+ return entry
18
+ }, {})
19
+
20
+ const parseListValue = listValue => listValue.values.map(value => parseObjectValue(value))
21
+
22
+ const astToEntries = inputArg => {
23
+ const value = inputArg.value
24
+ if (isListValue(value)) {
25
+ return parseListValue(value)
26
+ } else if (isObjectValue(value)) {
27
+ return parseObjectValue(value)
28
+ }
29
+ }
30
+
31
+ module.exports = astToEntries
@@ -0,0 +1,8 @@
1
+ const { getArgumentByName } = require('./utils')
2
+ const astToColumns = require('./columns')
3
+ const astToWhere = require('./where')
4
+ const astToOrderBy = require('./orderBy')
5
+ const astToLimit = require('./limit')
6
+ const astToEntries = require('./entries')
7
+
8
+ module.exports = { getArgumentByName, astToColumns, astToWhere, astToOrderBy, astToLimit, astToEntries }
@@ -0,0 +1,6 @@
1
+ const astToLimit = (topArg, skipArg) => ({
2
+ rows: { val: topArg.value.value },
3
+ offset: { val: (skipArg && skipArg.value.value) || 0 }
4
+ })
5
+
6
+ module.exports = astToLimit
@@ -0,0 +1,24 @@
1
+ const { isListValue, isObjectValue } = require('../utils')
2
+
3
+ const objectFieldToOrderBy = objectField => ({
4
+ ref: [objectField.name.value],
5
+ sort: objectField.value.value
6
+ })
7
+
8
+ const parseObjectValue = objectValue => {
9
+ // OrderBy objects are supposed to contain only a single field
10
+ return objectFieldToOrderBy(objectValue.fields[0])
11
+ }
12
+
13
+ const parseListValue = listValue => listValue.values.map(value => parseObjectValue(value))
14
+
15
+ const astToOrderBy = orderByArg => {
16
+ const value = orderByArg.value
17
+ if (isListValue(value)) {
18
+ return parseListValue(value)
19
+ } else if (isObjectValue(value)) {
20
+ return [parseObjectValue(value)]
21
+ }
22
+ }
23
+
24
+ module.exports = astToOrderBy
@@ -0,0 +1,3 @@
1
+ const getArgumentByName = (args, name) => args.find(arg => arg.name.value === name)
2
+
3
+ module.exports = { getArgumentByName }
@@ -0,0 +1,70 @@
1
+ const { STRING_MATCH_OPERATOR } = require('../../../constants/adapter')
2
+ const { GQL_TO_CDS_QL_OPERATOR, GQL_TO_CDS_STRING_MATCH_OPERATOR } = require('../../../constants/cds')
3
+ const { isListValue, isObjectValue } = require('../utils')
4
+
5
+ const stringMatchOperatorToLikeString = (operator, string) => {
6
+ switch (operator) {
7
+ case STRING_MATCH_OPERATOR.STARTSWITH:
8
+ return `${string}%`
9
+ case STRING_MATCH_OPERATOR.ENDSWITH:
10
+ return `%${string}`
11
+ case STRING_MATCH_OPERATOR.CONTAINS:
12
+ return `%${string}%`
13
+ }
14
+ }
15
+
16
+ const arrayInsertBetweenFlat = (array, element) => {
17
+ return [...array].flatMap((e, index) => (index < array.length - 1 ? [e, element] : [e])).flat()
18
+ }
19
+
20
+ const joinedXprFrom_xprs = (_xprs, operator) => {
21
+ return { xpr: arrayInsertBetweenFlat(_xprs, operator) }
22
+ }
23
+
24
+ const gqlOperatorToCdsOperator = gqlOperator =>
25
+ GQL_TO_CDS_QL_OPERATOR[gqlOperator] || GQL_TO_CDS_STRING_MATCH_OPERATOR[gqlOperator]
26
+
27
+ const gqlValueToCdsValue = (cdsOperator, gqlOperator, gqlValue) =>
28
+ cdsOperator === 'like' ? stringMatchOperatorToLikeString(gqlOperator, gqlValue) : gqlValue
29
+
30
+ const objectFieldTo_xpr = (objectField, columnName) => {
31
+ const gqlOperator = objectField.name.value
32
+ const cdsOperator = gqlOperatorToCdsOperator(gqlOperator)
33
+ const gqlValue = objectField.value.value
34
+ const cdsValue = gqlValueToCdsValue(cdsOperator, gqlOperator, gqlValue)
35
+
36
+ return [{ ref: [columnName] }, cdsOperator, { val: cdsValue }]
37
+ }
38
+
39
+ const parseObjectField = (objectField, columnName) => {
40
+ const value = objectField.value
41
+ const name = objectField.name.value
42
+ if (isListValue(value)) {
43
+ return parseListValue(value, name)
44
+ } else if (isObjectValue(value)) {
45
+ return parseObjectValue(value, name)
46
+ } else {
47
+ return objectFieldTo_xpr(objectField, columnName)
48
+ }
49
+ }
50
+
51
+ const parseObjectValue = (objectValue, columnName) => {
52
+ const _xprs = objectValue.fields.map(field => parseObjectField(field, columnName))
53
+ return _xprs.length === 1 ? _xprs[0] : joinedXprFrom_xprs(_xprs, 'and')
54
+ }
55
+
56
+ const parseListValue = (listValue, columnName) => {
57
+ const _xprs = listValue.values.map(value => parseObjectValue(value, columnName))
58
+ return _xprs.length === 1 ? _xprs[0] : joinedXprFrom_xprs(_xprs, 'or')
59
+ }
60
+
61
+ const astToWhere = filterArg => {
62
+ const value = filterArg.value
63
+ if (isListValue(value)) {
64
+ return parseListValue(value)
65
+ } else if (isObjectValue(value)) {
66
+ return parseObjectValue(value)
67
+ }
68
+ }
69
+
70
+ module.exports = astToWhere
@@ -0,0 +1,8 @@
1
+ const { AST_NODE_KIND } = require('../../../constants/graphql')
2
+
3
+ const isFragmentSpread = value => value.kind === AST_NODE_KIND.FragmentSpread
4
+ const isListValue = value => value.kind === AST_NODE_KIND.ListValue
5
+ const isObjectValue = value => value.kind === AST_NODE_KIND.ObjectValue
6
+ const isVariable = value => value.kind === AST_NODE_KIND.Variable
7
+
8
+ module.exports = { isFragmentSpread, isListValue, isObjectValue, isVariable }
@@ -0,0 +1,13 @@
1
+ const cds = require('../../_runtime/cds')
2
+ const LOG = cds.log('graphql')
3
+
4
+ const { cdsName } = require('../utils')
5
+ const { executeRead } = require('./crud')
6
+
7
+ module.exports = async (service, gqlName, field) => {
8
+ const entityFQN = `${service.name}.${cdsName(gqlName)}`
9
+
10
+ LOG.log(`query on ${entityFQN}`)
11
+
12
+ return await executeRead(service, entityFQN, field)
13
+ }
@@ -0,0 +1,34 @@
1
+ const { gqlName } = require('../utils')
2
+ const resolveQuery = require('./query')
3
+ const resolveMutation = require('./mutation')
4
+ const { enrichAST } = require('./parse/ast')
5
+
6
+ const wrapResolver = (service, resolver) => (root, args, context, info) => {
7
+ const response = {}
8
+
9
+ const enrichedFieldNodes = enrichAST(info)
10
+
11
+ for (const fieldNode of enrichedFieldNodes) {
12
+ for (const field of fieldNode.selectionSet.selections) {
13
+ const gqlName = field.name.value
14
+ const responseKey = field.alias ? field.alias.value : gqlName
15
+
16
+ response[responseKey] = resolver(service, gqlName, field)
17
+ }
18
+ }
19
+
20
+ return response
21
+ }
22
+
23
+ module.exports = services => {
24
+ const Query = {}
25
+ const Mutation = {}
26
+
27
+ for (const service of services) {
28
+ const gqlServiceName = gqlName(service.name)
29
+ Query[gqlServiceName] = wrapResolver(service, resolveQuery)
30
+ Mutation[gqlServiceName] = wrapResolver(service, resolveMutation)
31
+ }
32
+
33
+ return { Query, Mutation }
34
+ }
@@ -0,0 +1,18 @@
1
+ const { typeDefMapToMutationStringArray } = require('./mutation')
2
+ const { typeDefMapToQueryStringArray } = require('./query')
3
+ const { servicesToTypeDefMap } = require('./typeDefMap')
4
+
5
+ const typeDefMapToSchemaString = typeDefs => {
6
+ let schema = []
7
+
8
+ schema.push(...typeDefMapToQueryStringArray(typeDefs))
9
+
10
+ schema.push(...typeDefMapToMutationStringArray(typeDefs))
11
+
12
+ return schema.join('\n')
13
+ }
14
+
15
+ module.exports = services => {
16
+ const typeDefMap = servicesToTypeDefMap(services)
17
+ return typeDefMapToSchemaString(typeDefMap)
18
+ }
@@ -0,0 +1,5 @@
1
+ const generate = require('./generate')
2
+
3
+ module.exports = {
4
+ generate
5
+ }
@@ -0,0 +1,76 @@
1
+ const { ARGUMENT, MUTATION_PREFIX, INPUT_OBJECT_SUFFIX } = require('../constants/adapter')
2
+ const { GQL_ROOT, GQL_KEYWORDS, SCALAR_TYPES } = require('../constants/graphql')
3
+ const { generateSchemaObject, typeToArgumentType, isTypeScalar, appendSuffixToType } = require('./utils')
4
+
5
+ const typeDefMapToMutationStringArray = typeDefs => {
6
+ let schema = []
7
+
8
+ // Create root mutation (containing services)
9
+ schema.push(
10
+ ...generateSchemaObject(
11
+ GQL_KEYWORDS.TYPE,
12
+ GQL_ROOT.MUTATION,
13
+ Object.keys(typeDefs).reduce((fields, serviceName) => {
14
+ fields[serviceName] = `${serviceName}_${INPUT_OBJECT_SUFFIX.INPUT}`
15
+ return fields
16
+ }, {})
17
+ )
18
+ )
19
+
20
+ // Create types for mutations from services (each containing entities)
21
+ for (const [serviceName, entities] of Object.entries(typeDefs)) {
22
+ const fields = {}
23
+ for (const entityName of Object.keys(entities)) {
24
+ const entityNameWithoutServicePrefix = entityName.replace(`${serviceName}_`, '')
25
+
26
+ const createMutationName = `${MUTATION_PREFIX.CREATE}_${entityNameWithoutServicePrefix}`
27
+ const createMutationArgs = `(${ARGUMENT.INPUT}: [${entityName}_${INPUT_OBJECT_SUFFIX.CREATE}]!)`
28
+ fields[`${createMutationName}${createMutationArgs}`] = `[${entityName}]`
29
+
30
+ const updateMutationName = `${MUTATION_PREFIX.UPDATE}_${entityNameWithoutServicePrefix}`
31
+ const updateMutationArgs = `(${ARGUMENT.FILTER}: ${typeToArgumentType(entityName, ARGUMENT.FILTER)}, ${
32
+ ARGUMENT.INPUT
33
+ }: ${entityName}_${INPUT_OBJECT_SUFFIX.UPDATE}!)`
34
+ fields[`${updateMutationName}${updateMutationArgs}`] = `[${entityName}]`
35
+
36
+ const deleteMutationName = `${MUTATION_PREFIX.DELETE}_${entityNameWithoutServicePrefix}`
37
+ const deleteMutationArgs = `(${ARGUMENT.FILTER}: ${typeToArgumentType(entityName, ARGUMENT.FILTER)})`
38
+ fields[`${deleteMutationName}${deleteMutationArgs}`] = SCALAR_TYPES.INT
39
+ }
40
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.TYPE, `${serviceName}_${INPUT_OBJECT_SUFFIX.INPUT}`, fields))
41
+ }
42
+
43
+ // Create input types for create mutations from entities (each containing elements)
44
+ for (const entities of Object.values(typeDefs)) {
45
+ for (const [entityName, elements] of Object.entries(entities)) {
46
+ const fields = {}
47
+ for (const [elementName, elementType] of Object.entries(elements)) {
48
+ if (isTypeScalar(elementType)) {
49
+ fields[elementName] = elementType
50
+ } else {
51
+ fields[elementName] = appendSuffixToType(elementType, INPUT_OBJECT_SUFFIX.CREATE)
52
+ }
53
+ }
54
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.INPUT, `${entityName}_${INPUT_OBJECT_SUFFIX.CREATE}`, fields))
55
+ }
56
+ }
57
+
58
+ // Create input types for update mutations from entities (each containing elements)
59
+ for (const entities of Object.values(typeDefs)) {
60
+ for (const [entityName, elements] of Object.entries(entities)) {
61
+ const fields = {}
62
+ for (const [elementName, elementType] of Object.entries(elements)) {
63
+ if (isTypeScalar(elementType)) {
64
+ fields[elementName] = elementType
65
+ } else {
66
+ fields[elementName] = appendSuffixToType(elementType, INPUT_OBJECT_SUFFIX.UPDATE)
67
+ }
68
+ }
69
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.INPUT, `${entityName}_${INPUT_OBJECT_SUFFIX.UPDATE}`, fields))
70
+ }
71
+ }
72
+
73
+ return schema
74
+ }
75
+
76
+ module.exports = { typeDefMapToMutationStringArray }
@@ -0,0 +1,108 @@
1
+ const { ARGUMENT, HELPER_TYPES, EQUALITY_OPERATOR, STRING_MATCH_OPERATOR } = require('../constants/adapter')
2
+ const { GQL_ROOT, GQL_KEYWORDS, SCALAR_TYPES } = require('../constants/graphql')
3
+ const {
4
+ generateSchemaObject,
5
+ typeToArgumentType,
6
+ generateArgumentsForType,
7
+ isTypeScalar,
8
+ typeWithoutListBrackets
9
+ } = require('./utils')
10
+
11
+ const generateScalarFilterTypes = () => Object.values(SCALAR_TYPES).flatMap(scalarType => fullFilter(scalarType))
12
+
13
+ const fullFilter = type => {
14
+ const operations = Object.values(EQUALITY_OPERATOR)
15
+ if (type === SCALAR_TYPES.STRING) {
16
+ operations.push(...Object.values(STRING_MATCH_OPERATOR))
17
+ }
18
+ const entries = operations.reduce((acc, operation) => {
19
+ acc[operation] = type
20
+ return acc
21
+ }, {})
22
+ return generateSchemaObject(GQL_KEYWORDS.INPUT, `${type}_${ARGUMENT.FILTER}`, entries)
23
+ }
24
+
25
+ const typeDefMapToQueryStringArray = typeDefs => {
26
+ let schema = []
27
+
28
+ // Create root query (containing services)
29
+ schema.push(
30
+ ...generateSchemaObject(
31
+ GQL_KEYWORDS.TYPE,
32
+ GQL_ROOT.QUERY,
33
+ Object.keys(typeDefs).reduce((fields, serviceName) => {
34
+ fields[serviceName] = serviceName
35
+ return fields
36
+ }, {})
37
+ )
38
+ )
39
+
40
+ // Create types from services (each containing entities)
41
+ for (const [serviceName, entities] of Object.entries(typeDefs)) {
42
+ const fields = {}
43
+ for (const entityName of Object.keys(entities)) {
44
+ const entityNameWithoutServicePrefix = entityName.replace(`${serviceName}_`, '')
45
+ const argsString = generateArgumentsForType(entityName, [
46
+ ARGUMENT.FILTER,
47
+ ARGUMENT.ORDER_BY,
48
+ ARGUMENT.TOP,
49
+ ARGUMENT.SKIP
50
+ ])
51
+ fields[`${entityNameWithoutServicePrefix}${argsString}`] = `[${entityName}]`
52
+ }
53
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.TYPE, serviceName, fields))
54
+ }
55
+
56
+ // Create types from entities (each containing elements)
57
+ for (const entities of Object.values(typeDefs)) {
58
+ for (const [entityName, elements] of Object.entries(entities)) {
59
+ const fields = {}
60
+ for (const [elementName, elementType] of Object.entries(elements)) {
61
+ const argsString = generateArgumentsForType(elementType, [
62
+ ARGUMENT.FILTER,
63
+ ARGUMENT.ORDER_BY,
64
+ ARGUMENT.TOP,
65
+ ARGUMENT.SKIP
66
+ ])
67
+ fields[`${elementName}${argsString}`] = elementType
68
+ }
69
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.TYPE, entityName, fields))
70
+ }
71
+ }
72
+
73
+ // Create filter input types for scalars
74
+ schema.push(...generateScalarFilterTypes())
75
+
76
+ // Create filter input types for entity types
77
+ for (const entities of Object.values(typeDefs)) {
78
+ for (const [entityName, elements] of Object.entries(entities)) {
79
+ const fields = {}
80
+ for (const [elementName, elementType] of Object.entries(elements)) {
81
+ if (isTypeScalar(typeWithoutListBrackets(elementType))) {
82
+ fields[elementName] = typeToArgumentType(elementType, ARGUMENT.FILTER)
83
+ }
84
+ }
85
+ if (Object.keys(fields).length > 0) {
86
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.INPUT, `${entityName}_${ARGUMENT.FILTER}`, fields))
87
+ }
88
+ }
89
+ }
90
+
91
+ // Create sort direction enum types for order by
92
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.ENUM, HELPER_TYPES.SORT_DIRECTION, ['asc', 'desc']))
93
+
94
+ // Create orderBy input types for entity types
95
+ for (const entities of Object.values(typeDefs)) {
96
+ for (const [entityName, elements] of Object.entries(entities)) {
97
+ const fields = {}
98
+ for (const [elementName, elementType] of Object.entries(elements)) {
99
+ fields[elementName] = typeToArgumentType(elementType, ARGUMENT.ORDER_BY)
100
+ }
101
+ schema.push(...generateSchemaObject(GQL_KEYWORDS.INPUT, `${entityName}_${ARGUMENT.ORDER_BY}`, fields))
102
+ }
103
+ }
104
+
105
+ return schema
106
+ }
107
+
108
+ module.exports = { typeDefMapToQueryStringArray }
@@ -0,0 +1,45 @@
1
+ const { CDS_TO_GRAPHQL_TYPES } = require('../constants/adapter')
2
+ const { gqlName } = require('../utils')
3
+
4
+ const servicesToTypeDefMap = services => {
5
+ const typeDefs = {}
6
+
7
+ // Create nested map of services, their entities, and their respective elements
8
+ for (const service of services) {
9
+ const serviceDefs = (typeDefs[gqlName(service.name)] = {})
10
+
11
+ const serviceNamePrefix = `${service.name}.`
12
+ const entitiesKV = Object.entries(service.model.definitions).filter(
13
+ // eslint-disable-next-line no-unused-vars
14
+ ([k, _]) => k.startsWith(serviceNamePrefix) && service.model.definitions[k].kind === 'entity'
15
+ )
16
+
17
+ // eslint-disable-next-line no-unused-vars
18
+ for (const [_, entity] of entitiesKV) {
19
+ const def = (serviceDefs[gqlName(entity.name)] = {})
20
+ for (const ele of Object.values(entity.elements)) {
21
+ if (ele.name.startsWith('up_') || ele.name === 'localized' || ele.name === 'texts') {
22
+ continue
23
+ } else if (ele.isAssociation || ele.isComposition) {
24
+ if (!ele.target.startsWith(serviceNamePrefix)) {
25
+ // TODO entities in other namespaces
26
+ continue
27
+ }
28
+ def[ele.name] = ele.is2one ? gqlName(ele.target) : `[${gqlName(ele.target)}]`
29
+ } else if (ele.elements) {
30
+ // TODO structured types
31
+ continue
32
+ } else {
33
+ if (CDS_TO_GRAPHQL_TYPES[ele.type]) {
34
+ def[ele.name] = CDS_TO_GRAPHQL_TYPES[ele.type]
35
+ }
36
+ // TODO aspects
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ return typeDefs
43
+ }
44
+
45
+ module.exports = { servicesToTypeDefMap }
@@ -0,0 +1,54 @@
1
+ const { ARGUMENT, HELPER_TYPES } = require('../../constants/adapter')
2
+ const { GQL_LIST_REGEX, SCALAR_TYPES } = require('../../constants/graphql')
3
+
4
+ const generateSchemaObject = (type, name, fields) => {
5
+ const schema = [`${type} ${name} {`]
6
+ if (Array.isArray(fields)) {
7
+ schema.push(...fields.map(e => ` ${e}`))
8
+ } else {
9
+ schema.push(...Object.entries(fields).map(([k, v]) => ` ${k}: ${v}`))
10
+ }
11
+ schema.push('}', '')
12
+ return schema
13
+ }
14
+
15
+ const generateArgumentsForType = (type, args) =>
16
+ `(${args.map(action => `${action}: ${typeToArgumentType(type, action)}`).join(', ')})`
17
+
18
+ const typeToArgumentType = (type, action) => {
19
+ switch (action) {
20
+ case ARGUMENT.FILTER:
21
+ return appendSuffixToType(type, ARGUMENT.FILTER, true)
22
+ case ARGUMENT.ORDER_BY:
23
+ return isTypeScalar(type) ? HELPER_TYPES.SORT_DIRECTION : appendSuffixToType(type, ARGUMENT.ORDER_BY, true)
24
+ case ARGUMENT.TOP:
25
+ return SCALAR_TYPES.INT
26
+ case ARGUMENT.SKIP:
27
+ return SCALAR_TYPES.INT
28
+ }
29
+ }
30
+
31
+ const isTypeScalar = type => Object.values(SCALAR_TYPES).includes(type)
32
+
33
+ const isTypeList = type => type.match(GQL_LIST_REGEX)
34
+
35
+ // Type without list brackets even if type isn't a list
36
+ const typeWithoutListBrackets = type => type.replace(GQL_LIST_REGEX, '$1')
37
+
38
+ const appendSuffixToType = (type, suffix, forceToList) => {
39
+ if (isTypeList(type) || forceToList) {
40
+ return `[${typeWithoutListBrackets(type)}_${suffix}]`
41
+ } else {
42
+ return `${type}_${suffix}`
43
+ }
44
+ }
45
+
46
+ module.exports = {
47
+ generateSchemaObject,
48
+ generateArgumentsForType,
49
+ typeToArgumentType,
50
+ isTypeScalar,
51
+ isTypeList,
52
+ typeWithoutListBrackets,
53
+ appendSuffixToType
54
+ }
@@ -0,0 +1,12 @@
1
+ const cdsName = gqlName => {
2
+ return gqlName.replace(/_/g, '.')
3
+ }
4
+
5
+ const gqlName = cdsName => {
6
+ return cdsName.replace(/\./g, '_')
7
+ }
8
+
9
+ module.exports = {
10
+ cdsName,
11
+ gqlName
12
+ }