From 4b9956480d1e6755cd0b29b97ca4096f9838a041 Mon Sep 17 00:00:00 2001 From: nodkz Date: Wed, 21 Jul 2021 22:47:13 +0600 Subject: [PATCH] fix(filterHelper): if `filter` type already exists in schema then it will be reused relates #288 --- src/resolvers/helpers/filter.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/resolvers/helpers/filter.ts b/src/resolvers/helpers/filter.ts index 7513724f..db33d2cd 100644 --- a/src/resolvers/helpers/filter.ts +++ b/src/resolvers/helpers/filter.ts @@ -4,6 +4,7 @@ import { ObjectTypeComposer, InterfaceTypeComposer, ObjectTypeComposerArgumentConfigMap, + InputTypeComposer, } from 'graphql-compose'; import type { Model, Document } from 'mongoose'; import { isObject, toMongoFilterDottedObject, getIndexedFieldNamesForGraphQL } from '../../utils'; @@ -102,24 +103,30 @@ export function filterHelperArgs( const { prefix, suffix } = opts; const filterTypeName: string = `${prefix}${typeComposer.getTypeName()}${suffix}`; - const itc = typeComposer.getInputTypeComposer().clone(filterTypeName); - makeFieldsRecursiveNullable(itc, { prefix, suffix }); + let itc; + if (typeComposer.schemaComposer.hasInstance(filterTypeName, InputTypeComposer)) { + itc = typeComposer.schemaComposer.getITC(filterTypeName); + } else { + itc = typeComposer.getInputTypeComposer().clone(filterTypeName); - itc.removeField(removeFields); + makeFieldsRecursiveNullable(itc, { prefix, suffix }); - if (opts.requiredFields) { - itc.makeFieldNonNull(opts.requiredFields); - } + itc.removeField(removeFields); - if (itc.getFieldNames().length === 0) { - return {} as any; - } + if (opts.requiredFields) { + itc.makeFieldNonNull(opts.requiredFields); + } - if (!opts.baseTypeName) { - opts.baseTypeName = typeComposer.getTypeName(); + if (itc.getFieldNames().length === 0) { + return {} as any; + } + + if (!opts.baseTypeName) { + opts.baseTypeName = typeComposer.getTypeName(); + } + addFilterOperators(itc, model, opts); } - addFilterOperators(itc, model, opts); return { filter: {