Skip to content

Commit da008d5

Browse files
authored
Merge pull request #94 from cap-js/conjunction-support
[FEATURE!] Disable Conjunction for Certain Operators
2 parents 4275da5 + 8b4cba5 commit da008d5

File tree

10 files changed

+156
-125
lines changed

10 files changed

+156
-125
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
- Don't generate fields that represent compositions of aspects within mutation types that represent services
1515
- Moved the `GraphQLAdapter` module to the root directory (`index.js`), simplifying the import process and reducing the required typing.
16+
- Disabled conjunction on the same field for the following operators:
17+
+ `eq` (Equal)
18+
+ `gt` (Greater Than)
19+
+ `ge` (Greater Than or Equal)
20+
+ `le` (Less Than or Equal)
21+
+ `lt` (Less Than)
22+
+ `startswith`
23+
+ `endswith`
1624

1725
### Fixed
1826

lib/constants.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,22 @@ const STRING_OPERATIONS = {
2626
contains: 'contains'
2727
}
2828

29+
const OPERATOR_CONJUNCTION_SUPPORT = {
30+
[RELATIONAL_OPERATORS.eq]: false,
31+
[RELATIONAL_OPERATORS.ne]: true,
32+
[RELATIONAL_OPERATORS.gt]: false,
33+
[RELATIONAL_OPERATORS.ge]: false,
34+
[RELATIONAL_OPERATORS.le]: false,
35+
[RELATIONAL_OPERATORS.lt]: false,
36+
[STRING_OPERATIONS.startswith]: false,
37+
[STRING_OPERATIONS.endswith]: false,
38+
[STRING_OPERATIONS.contains]: true
39+
}
40+
2941
module.exports = {
3042
CONNECTION_FIELDS,
3143
ARGS,
3244
RELATIONAL_OPERATORS,
33-
STRING_OPERATIONS
45+
STRING_OPERATIONS,
46+
OPERATOR_CONJUNCTION_SUPPORT
3447
}

lib/resolvers/parse/ast/fromObject.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ const _arrayToListValue = array => ({
3232
const _variableToValue = variable => {
3333
if (Array.isArray(variable)) {
3434
return _arrayToListValue(variable)
35-
} else if (isPlainObject(variable)) {
35+
}
36+
37+
if (isPlainObject(variable)) {
3638
return _objectToObjectValue(variable)
37-
} else if (variable === null) {
39+
}
40+
41+
if (variable === null) {
3842
return _nullValue
39-
} else if (variable === undefined) {
43+
}
44+
45+
if (variable === undefined) {
4046
return undefined
4147
}
48+
4249
return _valueToGenericScalarValue(variable)
4350
}
4451

lib/resolvers/parse/ast2cqn/where.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const _to_xpr = (ref, gqlOperator, value) => {
2424
const val = { val: value }
2525

2626
if (STRING_OPERATIONS[gqlOperator]) return [{ func: cdsOperator, args: [ref, val] }]
27-
2827
return [ref, cdsOperator, val]
2928
}
3029

lib/schema/args/filter.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
const { gqlName } = require('../../utils')
1111
const { hasScalarFields, shouldElementBeIgnored } = require('../util')
1212
const { cdsToGraphQLScalarType } = require('../types/scalar')
13-
const { RELATIONAL_OPERATORS, STRING_OPERATIONS } = require('../../constants')
13+
const { RELATIONAL_OPERATORS, STRING_OPERATIONS, OPERATOR_CONJUNCTION_SUPPORT } = require('../../constants')
1414
const {
1515
GraphQLBinary,
1616
GraphQLDate,
@@ -83,10 +83,14 @@ module.exports = cache => {
8383
const _generateFilterType = (gqlType, operations) => {
8484
const filterName = gqlType.name + '_filter'
8585

86-
const cacheFilterType = cache.get(filterName)
87-
if (cacheFilterType) return cacheFilterType
86+
const cachedFilterType = cache.get(filterName)
87+
if (cachedFilterType) return cachedFilterType
8888

89-
const fields = Object.fromEntries(operations.map(op => [[op], { type: new GraphQLList(gqlType) }]))
89+
const ops = operations.map(op => [
90+
[op],
91+
{ type: OPERATOR_CONJUNCTION_SUPPORT[op] ? new GraphQLList(gqlType) : gqlType }
92+
])
93+
const fields = Object.fromEntries(ops)
9094
const newFilterType = new GraphQLInputObjectType({ name: filterName, fields })
9195
cache.set(filterName, newFilterType)
9296

test/schemas/bookshop-graphql.gql

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ The `Binary` scalar type represents binary values as `base64url` encoded strings
635635
scalar Binary
636636

637637
input Binary_filter {
638-
eq: [Binary]
638+
eq: Binary
639639
ne: [Binary]
640640
}
641641

@@ -1277,11 +1277,11 @@ The `Date` scalar type represents date values as strings in the ISO 8601 format
12771277
scalar Date
12781278

12791279
input Date_filter {
1280-
eq: [Date]
1281-
ge: [Date]
1282-
gt: [Date]
1283-
le: [Date]
1284-
lt: [Date]
1280+
eq: Date
1281+
ge: Date
1282+
gt: Date
1283+
le: Date
1284+
lt: Date
12851285
ne: [Date]
12861286
}
12871287

@@ -1291,11 +1291,11 @@ The `Decimal` scalar type represents exact signed decimal values. Decimal repres
12911291
scalar Decimal
12921292

12931293
input Decimal_filter {
1294-
eq: [Decimal]
1295-
ge: [Decimal]
1296-
gt: [Decimal]
1297-
le: [Decimal]
1298-
lt: [Decimal]
1294+
eq: Decimal
1295+
ge: Decimal
1296+
gt: Decimal
1297+
le: Decimal
1298+
lt: Decimal
12991299
ne: [Decimal]
13001300
}
13011301

@@ -1305,20 +1305,20 @@ The `Int16` scalar type represents 16-bit non-fractional signed whole numeric va
13051305
scalar Int16
13061306

13071307
input Int16_filter {
1308-
eq: [Int16]
1309-
ge: [Int16]
1310-
gt: [Int16]
1311-
le: [Int16]
1312-
lt: [Int16]
1308+
eq: Int16
1309+
ge: Int16
1310+
gt: Int16
1311+
le: Int16
1312+
lt: Int16
13131313
ne: [Int16]
13141314
}
13151315

13161316
input Int_filter {
1317-
eq: [Int]
1318-
ge: [Int]
1319-
gt: [Int]
1320-
le: [Int]
1321-
lt: [Int]
1317+
eq: Int
1318+
ge: Int
1319+
gt: Int
1320+
le: Int
1321+
lt: Int
13221322
ne: [Int]
13231323
}
13241324

@@ -1339,14 +1339,14 @@ enum SortDirection {
13391339

13401340
input String_filter {
13411341
contains: [String]
1342-
endswith: [String]
1343-
eq: [String]
1344-
ge: [String]
1345-
gt: [String]
1346-
le: [String]
1347-
lt: [String]
1342+
endswith: String
1343+
eq: String
1344+
ge: String
1345+
gt: String
1346+
le: String
1347+
lt: String
13481348
ne: [String]
1349-
startswith: [String]
1349+
startswith: String
13501350
}
13511351

13521352
"""
@@ -1355,10 +1355,10 @@ The `Timestamp` scalar type represents timestamp values as strings in the ISO 86
13551355
scalar Timestamp
13561356

13571357
input Timestamp_filter {
1358-
eq: [Timestamp]
1359-
ge: [Timestamp]
1360-
gt: [Timestamp]
1361-
le: [Timestamp]
1362-
lt: [Timestamp]
1358+
eq: Timestamp
1359+
ge: Timestamp
1360+
gt: Timestamp
1361+
le: Timestamp
1362+
lt: Timestamp
13631363
ne: [Timestamp]
13641364
}

test/schemas/edge-cases/field-named-localized.gql

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ input FieldNamedLocalizedService_localized_orderBy {
114114
}
115115

116116
input Int_filter {
117-
eq: [Int]
118-
ge: [Int]
119-
gt: [Int]
120-
le: [Int]
121-
lt: [Int]
117+
eq: Int
118+
ge: Int
119+
gt: Int
120+
le: Int
121+
lt: Int
122122
ne: [Int]
123123
}
124124

@@ -137,12 +137,12 @@ enum SortDirection {
137137

138138
input String_filter {
139139
contains: [String]
140-
endswith: [String]
141-
eq: [String]
142-
ge: [String]
143-
gt: [String]
144-
le: [String]
145-
lt: [String]
140+
endswith: String
141+
eq: String
142+
ge: String
143+
gt: String
144+
le: String
145+
lt: String
146146
ne: [String]
147-
startswith: [String]
147+
startswith: String
148148
}

test/schemas/model-structure/composition-of-aspect.gql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ type CompositionOfAspectService_input {
107107
}
108108

109109
input ID_filter {
110-
eq: [ID]
111-
ge: [ID]
112-
gt: [ID]
113-
le: [ID]
114-
lt: [ID]
110+
eq: ID
111+
ge: ID
112+
gt: ID
113+
le: ID
114+
lt: ID
115115
ne: [ID]
116116
}
117117

0 commit comments

Comments
 (0)