Skip to content

Commit eb84abd

Browse files
committed
fix: custom filters breaks mongo queries #315
closes #315
1 parent 1310e5d commit eb84abd

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { SchemaComposer, graphql } from 'graphql-compose';
2+
import { composeMongoose } from '../../index';
3+
import { mongoose } from '../../__mocks__/mongooseCommon';
4+
5+
const schemaComposer = new SchemaComposer<{ req: any }>();
6+
7+
// mongoose.set('debug', true);
8+
9+
const BookSchema = new mongoose.Schema({
10+
_id: { type: Number },
11+
title: { type: String },
12+
date: { type: Date },
13+
});
14+
15+
const BookModel = mongoose.model('Book', BookSchema);
16+
17+
const BookTC = composeMongoose(BookModel, { schemaComposer });
18+
const booksFindMany = BookTC.mongooseResolvers.findMany().addFilterArg({
19+
name: 'from',
20+
type: 'Date',
21+
description: 'Appointment date should be after the provided date.',
22+
query: (rawQuery: any, value: Date) => {
23+
rawQuery.date = {
24+
$gte: value,
25+
...(rawQuery.date && typeof rawQuery.date != 'object'
26+
? { $eq: rawQuery.date }
27+
: rawQuery.date ?? {}),
28+
};
29+
},
30+
});
31+
32+
schemaComposer.Query.addFields({
33+
booksMany: booksFindMany,
34+
});
35+
36+
const schema = schemaComposer.buildSchema();
37+
38+
beforeAll(async () => {
39+
await BookModel.base.createConnection();
40+
await BookModel.create({
41+
_id: 1,
42+
title: 'Atlas Shrugged',
43+
date: new Date('2020-01-01'),
44+
});
45+
await BookModel.create({
46+
_id: 2,
47+
title: 'Atlas Shrugged vol 2',
48+
date: new Date('2021-03-30'),
49+
});
50+
});
51+
afterAll(() => {
52+
mongoose.set('debug', false);
53+
BookModel.base.disconnect();
54+
});
55+
56+
describe('Custom filters breaks mongo queries with 9.0.1 - issue #315', () => {
57+
it('check custom filter', async () => {
58+
const result = await graphql.graphql({
59+
schema,
60+
source: `query {
61+
booksMany(filter: { from: "2021-01-01T00:00:00" }) {
62+
title
63+
}
64+
}`,
65+
});
66+
expect(result).toEqual({
67+
data: {
68+
booksMany: [{ title: 'Atlas Shrugged vol 2' }],
69+
},
70+
});
71+
});
72+
});

src/resolvers/helpers/filter.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ function convertFilterFields(
162162
clearedFilter[key] = Array.isArray(value)
163163
? value.map((v) => toMongoFilterDottedObject(v, aliases))
164164
: toMongoFilterDottedObject(value, aliases);
165-
} else if (schemaFields[key] || aliases?.[key] || isObject(value)) {
165+
} else if (
166+
schemaFields[key] ||
167+
aliases?.[key] ||
168+
isNestedFilterField(key, value, schemaFields)
169+
) {
166170
const alias = aliases?.[key];
167171
let newKey;
168172
let subAlias: NestedAliasesMap | undefined;
@@ -180,3 +184,13 @@ function convertFilterFields(
180184

181185
return clearedFilter;
182186
}
187+
188+
function isNestedFilterField(
189+
key: string,
190+
value: any,
191+
schemaFields: { [key: string]: mongoose.SchemaType }
192+
): boolean {
193+
if (!isObject(value)) return false;
194+
195+
return Object.keys(schemaFields).some((dottedPath) => dottedPath.startsWith(`${key}.`));
196+
}

0 commit comments

Comments
 (0)