|
6 | 6 | GraphQLInputObjectType, |
7 | 7 | GraphQLScalarType, |
8 | 8 | GraphQLEnumType, |
| 9 | + GraphQLString, |
9 | 10 | } from 'graphql-compose/lib/graphql'; |
10 | 11 | import type { Model, Schema, SchemaType, VirtualType } from 'mongoose'; |
11 | 12 | import type { InputTypeComposer } from 'graphql-compose'; |
@@ -79,35 +80,32 @@ export function _availableOperatorsFields( |
79 | 80 | : Object.values(availableOperators); |
80 | 81 |
|
81 | 82 | operators.forEach((operatorName: string) => { |
82 | | - const fc = itc.getFieldConfig(fieldName); |
83 | 83 | // unwrap from GraphQLNonNull and GraphQLList, if present |
84 | | - let fieldType: GraphQLInputType | InputTypeComposer | string = getNamedType( |
85 | | - fc.type |
| 84 | + let fieldType: GraphQLInputType | string = getNamedType( |
| 85 | + itc.getFieldType(fieldName) |
86 | 86 | ) as GraphQLInputType; |
87 | 87 |
|
88 | 88 | // just treat enums as strings |
89 | | - if (getNamedType(fc.type) instanceof GraphQLEnumType) { |
| 89 | + if (fieldType instanceof GraphQLEnumType) { |
90 | 90 | fieldType = 'String'; |
91 | 91 | } |
92 | 92 |
|
93 | 93 | if (fieldType) { |
94 | 94 | if (['in', 'nin', 'in[]', 'nin[]'].includes(operatorName)) { |
95 | 95 | // wrap with GraphQLList, if operator required this with `[]` |
96 | 96 | const newName = operatorName.slice(-2) === '[]' ? operatorName.slice(0, -2) : operatorName; |
97 | | - fields[newName] = { |
98 | | - ...fc, |
99 | | - type: [fieldType], |
100 | | - } as any; |
| 97 | + fields[newName] = { type: [fieldType] }; |
101 | 98 | } else { |
102 | 99 | if (operatorName === 'exists') { |
103 | | - fieldType = 'Boolean'; |
| 100 | + fields[operatorName] = { type: 'Boolean' }; |
104 | 101 | } else if (operatorName === 'regex') { |
105 | | - fieldType = GraphQLRegExpAsString; |
| 102 | + // Only for fields with type String allow regex operator |
| 103 | + if (fieldType === GraphQLString) { |
| 104 | + fields[operatorName] = { type: GraphQLRegExpAsString }; |
| 105 | + } |
| 106 | + } else { |
| 107 | + fields[operatorName] = { type: fieldType } as any; |
106 | 108 | } |
107 | | - fields[operatorName] = { |
108 | | - ...fc, |
109 | | - type: fieldType, |
110 | | - } as any; |
111 | 109 | } |
112 | 110 | } |
113 | 111 | }); |
@@ -234,11 +232,7 @@ export const _recurseFields = (fields: SelectorOptions): SelectorOptions => { |
234 | 232 | Object.keys(fields).forEach((fieldName) => { |
235 | 233 | const operators: string[] = Object.values(availableOperators); |
236 | 234 | if (operators.includes(fieldName)) { |
237 | | - if (fieldName === 'regex') { |
238 | | - selectors[`$${fieldName}`] = fields[fieldName]; |
239 | | - } else { |
240 | | - selectors[`$${fieldName}`] = fields[fieldName]; |
241 | | - } |
| 235 | + selectors[`$${fieldName}`] = fields[fieldName]; |
242 | 236 | } else { |
243 | 237 | selectors[fieldName] = _recurseFields(fields[fieldName] as SelectorOptions); |
244 | 238 | } |
|
0 commit comments