|
| 1 | +import { SchemaComposer, graphql } from 'graphql-compose'; |
| 2 | +import { composeMongoose } from '../../index'; |
| 3 | +import { mongoose } from '../../__mocks__/mongooseCommon'; |
| 4 | +import { Document } from 'mongoose'; |
| 5 | + |
| 6 | +const schemaComposer = new SchemaComposer<{ req: any }>(); |
| 7 | + |
| 8 | +// mongoose.set('debug', true); |
| 9 | + |
| 10 | +const AuthorSchema = new mongoose.Schema({ |
| 11 | + name: { type: String }, |
| 12 | + age: { type: Number }, |
| 13 | +}); |
| 14 | + |
| 15 | +interface IAuthor extends Document { |
| 16 | + name: string; |
| 17 | + age: number; |
| 18 | +} |
| 19 | + |
| 20 | +const AuthorModel = mongoose.model<IAuthor>('Author', AuthorSchema); |
| 21 | +const AuthorTC = composeMongoose(AuthorModel, { schemaComposer }); |
| 22 | + |
| 23 | +schemaComposer.Query.addFields({ |
| 24 | + authorMany: AuthorTC.mongooseResolvers.findMany().addFilterArg({ |
| 25 | + name: 'test', |
| 26 | + type: 'String', |
| 27 | + query: (query, value) => { |
| 28 | + query.name = new RegExp(value, 'i'); |
| 29 | + }, |
| 30 | + }), |
| 31 | +}); |
| 32 | + |
| 33 | +const schema = schemaComposer.buildSchema(); |
| 34 | + |
| 35 | +beforeAll(async () => { |
| 36 | + await AuthorModel.base.createConnection(); |
| 37 | + await AuthorModel.create({ |
| 38 | + name: 'Ayn Rand', |
| 39 | + age: 115, |
| 40 | + }); |
| 41 | +}); |
| 42 | +afterAll(() => { |
| 43 | + AuthorModel.base.disconnect(); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('check addFilterArg - issue #289', () => { |
| 47 | + it('check SDL', async () => { |
| 48 | + expect(schemaComposer.getITC('FilterFindManyAuthorInput').toSDL({ omitDescriptions: true })) |
| 49 | + .toMatchInlineSnapshot(` |
| 50 | + "input FilterFindManyAuthorInput { |
| 51 | + name: String |
| 52 | + age: Float |
| 53 | + _id: MongoID |
| 54 | + _operators: FilterFindManyAuthorOperatorsInput |
| 55 | + OR: [FilterFindManyAuthorInput!] |
| 56 | + AND: [FilterFindManyAuthorInput!] |
| 57 | + test: String |
| 58 | + }" |
| 59 | + `); |
| 60 | + }); |
| 61 | + |
| 62 | + it('check runtime', async () => { |
| 63 | + const result = await graphql.graphql({ |
| 64 | + schema, |
| 65 | + source: `query { |
| 66 | + authorMany(filter: { test: "ayn" }) { |
| 67 | + name |
| 68 | + age |
| 69 | + } |
| 70 | + }`, |
| 71 | + contextValue: {}, |
| 72 | + }); |
| 73 | + expect(result).toEqual({ data: { authorMany: [{ age: 115, name: 'Ayn Rand' }] } }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments