|
| 1 | +/* eslint-disable no-param-reassign */ |
| 2 | + |
| 3 | +import { schemaComposer, graphql } from 'graphql-compose'; |
| 4 | +import { composeWithMongoose } from '../../index'; |
| 5 | +import { mongoose } from '../../__mocks__/mongooseCommon'; |
| 6 | + |
| 7 | +const EventSchema = new mongoose.Schema( |
| 8 | + { |
| 9 | + timestamp: { type: Date, required: true }, |
| 10 | + level: { type: String, required: true }, |
| 11 | + message: { type: String, required: true }, |
| 12 | + meta: { |
| 13 | + topic: { type: String }, |
| 14 | + subjects: { type: [String] }, |
| 15 | + variables: { type: Map, of: String }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + { collection: 'events' } |
| 19 | +); |
| 20 | + |
| 21 | +const EventModel = mongoose.model('Event', EventSchema); |
| 22 | +const EventTC = composeWithMongoose(EventModel); |
| 23 | +EventTC.getResolvers().forEach((resolver) => { |
| 24 | + const newResolver = resolver.addFilterArg({ |
| 25 | + name: 'subjects', |
| 26 | + filterTypeNameFallback: 'FilterEventInput', |
| 27 | + type: '[String]', |
| 28 | + query: (query, value) => { |
| 29 | + query['meta.subjects'] = { $elemMatch: { $in: value } }; |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + EventTC.setResolver(resolver.name, newResolver); |
| 34 | +}); |
| 35 | +schemaComposer.Query.addFields({ |
| 36 | + eventMany: EventTC.getResolver('findMany'), |
| 37 | +}); |
| 38 | +const schema = schemaComposer.buildSchema(); |
| 39 | + |
| 40 | +beforeAll(async () => { |
| 41 | + await EventModel.base.createConnection(); |
| 42 | + await EventModel.create({ |
| 43 | + timestamp: new Date(), |
| 44 | + level: 'status', |
| 45 | + message: 'event1', |
| 46 | + meta: { |
| 47 | + topic: 'topic', |
| 48 | + subjects: ['metaValue'], |
| 49 | + }, |
| 50 | + }); |
| 51 | + await EventModel.create({ |
| 52 | + timestamp: new Date(), |
| 53 | + level: 'status', |
| 54 | + message: 'event2', |
| 55 | + meta: { |
| 56 | + topic: 'topic', |
| 57 | + subjects: ['notMetaValue'], |
| 58 | + }, |
| 59 | + }); |
| 60 | +}); |
| 61 | +afterAll(() => EventModel.base.disconnect()); |
| 62 | + |
| 63 | +describe('issue #282 - Filter nested array by string', () => { |
| 64 | + it('correct request', async () => { |
| 65 | + expect( |
| 66 | + await graphql.graphql( |
| 67 | + schema, |
| 68 | + `query { |
| 69 | + eventMany(filter: { subjects: ["notMetaValue"] }) { |
| 70 | + message |
| 71 | + meta { |
| 72 | + subjects |
| 73 | + } |
| 74 | + } |
| 75 | + }` |
| 76 | + ) |
| 77 | + ).toEqual({ |
| 78 | + data: { eventMany: [{ message: 'event2', meta: { subjects: ['notMetaValue'] } }] }, |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments