Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Register `aliasFieldResolver` during schema generation instead of passing it to the GraphQL server
- The filters `contains`, `startswith`, and `endswith` now generate CQN function calls instead of generating `like` expressions directly

### Fixed

Expand Down
25 changes: 9 additions & 16 deletions lib/resolvers/parse/ast2cqn/where.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { RELATIONAL_OPERATORS, STRING_OPERATIONS } = require('../../../constants'
const { Kind } = require('graphql')

const GQL_TO_CDS_STRING_OPERATIONS = {
[STRING_OPERATIONS.startswith]: 'like',
[STRING_OPERATIONS.endswith]: 'like',
[STRING_OPERATIONS.contains]: 'like'
[STRING_OPERATIONS.startswith]: 'startswith',
[STRING_OPERATIONS.endswith]: 'endswith',
[STRING_OPERATIONS.contains]: 'contains'
}

const GQL_TO_CDS_QL_OPERATOR = {
Expand All @@ -16,26 +16,19 @@ const GQL_TO_CDS_QL_OPERATOR = {
[RELATIONAL_OPERATORS.lt]: '<'
}

const _stringOperationToLikeString = (operator, string) =>
({
[STRING_OPERATIONS.startswith]: `${string}%`,
[STRING_OPERATIONS.endswith]: `%${string}`,
[STRING_OPERATIONS.contains]: `%${string}%`
}[operator])

const _gqlValueToCdsValue = (cdsOperator, gqlOperator, gqlValue) =>
cdsOperator === 'like' ? _stringOperationToLikeString(gqlOperator, gqlValue) : gqlValue

const _gqlOperatorToCdsOperator = gqlOperator =>
GQL_TO_CDS_QL_OPERATOR[gqlOperator] || GQL_TO_CDS_STRING_OPERATIONS[gqlOperator]

const _objectFieldTo_xpr = (objectField, columnName) => {
const gqlOperator = objectField.name.value
const cdsOperator = _gqlOperatorToCdsOperator(gqlOperator)
const gqlValue = objectField.value.value
const cdsValue = _gqlValueToCdsValue(cdsOperator, gqlOperator, gqlValue)

return [{ ref: [columnName] }, cdsOperator, { val: cdsValue }]
const ref = { ref: [columnName] }
const val = { val: objectField.value.value }

if (STRING_OPERATIONS[gqlOperator]) return [{ func: cdsOperator, args: [ref, val] }]

return [ref, cdsOperator, val]
}

const _parseObjectField = (objectField, columnName) => {
Expand Down