1- import {
2- getNamedType ,
3- GraphQLInputObjectType ,
4- GraphQLScalarType ,
5- GraphQLEnumType ,
6- GraphQLString ,
7- } from 'graphql-compose/lib/graphql' ;
81import type { Model } from 'mongoose' ;
9- import { InputTypeComposer , inspect } from 'graphql-compose' ;
2+ import { EnumTypeComposer , InputTypeComposer , inspect , ScalarTypeComposer } from 'graphql-compose' ;
103import type { InputTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose' ;
114
125import { upperFirst , getIndexesFromModel } from '../../utils' ;
@@ -89,24 +82,23 @@ export function _availableOperatorsFields(
8982 : availableOperators ;
9083
9184 operators . forEach ( ( operatorName : string ) => {
92- // unwrap from GraphQLNonNull and GraphQLList, if present
93- const fieldType = getNamedType ( itc . getFieldType ( fieldName ) ) ;
85+ const fieldTC = itc . getFieldTC ( fieldName ) ;
9486
95- if ( fieldType ) {
87+ if ( fieldTC ) {
9688 if ( [ 'in' , 'nin' , 'in[]' , 'nin[]' ] . includes ( operatorName ) ) {
9789 // wrap with GraphQLList, if operator required this with `[]`
9890 const newName = operatorName . slice ( - 2 ) === '[]' ? operatorName . slice ( 0 , - 2 ) : operatorName ;
99- fields [ newName ] = { type : [ fieldType ] as any } ;
91+ fields [ newName ] = { type : [ fieldTC ] } ;
10092 } else {
10193 if ( operatorName === 'exists' ) {
10294 fields [ operatorName ] = { type : 'Boolean' } ;
10395 } else if ( operatorName === 'regex' ) {
10496 // Only for fields with type String allow regex operator
105- if ( fieldType === GraphQLString ) {
97+ if ( fieldTC . getTypeName ( ) === 'String' ) {
10698 fields [ operatorName ] = { type : GraphQLRegExpAsString } ;
10799 }
108100 } else {
109- fields [ operatorName ] = { type : fieldType as any } ;
101+ fields [ operatorName ] = { type : fieldTC } ;
110102 }
111103 }
112104 }
@@ -148,15 +140,14 @@ export function _recurseSchema(
148140 }
149141
150142 const fieldTC = sourceITC . getFieldTC ( fieldName ) ;
151- const fieldType = fieldTC . getType ( ) ;
152143
153144 // prevent infinite recursion
154- if ( sourceITC . getType ( ) === fieldType ) return ;
145+ if ( sourceITC === fieldTC ) return ;
155146
156147 const baseTypeName = `${ opts . baseTypeName } ${ upperFirst ( fieldName ) } ` ;
157148 const inputFieldTypeName = `${ opts . prefix || '' } ${ baseTypeName } ${ opts . suffix || '' } ` ;
158149
159- if ( fieldType instanceof GraphQLScalarType || fieldType instanceof GraphQLEnumType ) {
150+ if ( fieldTC instanceof ScalarTypeComposer || fieldTC instanceof EnumTypeComposer ) {
160151 if (
161152 fieldOperatorsConfig &&
162153 ! Array . isArray ( fieldOperatorsConfig ) &&
@@ -181,7 +172,7 @@ export function _recurseSchema(
181172 [ fieldName ] : fieldOperatorsITC ,
182173 } ) ;
183174 }
184- } else if ( fieldType instanceof GraphQLInputObjectType ) {
175+ } else if ( fieldTC instanceof InputTypeComposer ) {
185176 const fieldOperatorsITC = schemaComposer . createInputTC ( inputFieldTypeName ) ;
186177 _recurseSchema (
187178 fieldOperatorsITC ,
@@ -194,9 +185,12 @@ export function _recurseSchema(
194185 indexedFields ,
195186 fieldPath
196187 ) ;
197- inputITC . addFields ( {
198- [ fieldName ] : fieldOperatorsITC ,
199- } ) ;
188+
189+ if ( fieldOperatorsITC . getFieldNames ( ) . length > 0 ) {
190+ inputITC . addFields ( {
191+ [ fieldName ] : fieldOperatorsITC ,
192+ } ) ;
193+ }
200194 }
201195 } ) ;
202196}
0 commit comments