Skip to content

Commit

Permalink
fix(filterHelper): if filter type already exists in schema then it …
Browse files Browse the repository at this point in the history
…will be reused

relates #288
  • Loading branch information
nodkz committed Jul 21, 2021
1 parent 1b13d4e commit 4b99564
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/resolvers/helpers/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ObjectTypeComposer,
InterfaceTypeComposer,
ObjectTypeComposerArgumentConfigMap,
InputTypeComposer,
} from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import { isObject, toMongoFilterDottedObject, getIndexedFieldNamesForGraphQL } from '../../utils';
Expand Down Expand Up @@ -102,24 +103,30 @@ export function filterHelperArgs<TDoc extends Document = any>(

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: {
Expand Down

0 comments on commit 4b99564

Please sign in to comment.