|
| 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 | +}); |
0 commit comments