|
| 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 UserSchema = new mongoose.Schema({ |
| 8 | + name: { type: String, required: true }, |
| 9 | +}); |
| 10 | +const PostSchema = new mongoose.Schema({ |
| 11 | + title: { type: String, required: true }, |
| 12 | + authorId: { type: mongoose.Types.ObjectId }, |
| 13 | + reviewerIds: { type: [mongoose.Types.ObjectId] }, |
| 14 | +}); |
| 15 | + |
| 16 | +const UserModel = mongoose.model('User', UserSchema); |
| 17 | +const PostModel = mongoose.model('Post', PostSchema); |
| 18 | + |
| 19 | +const UserTC = composeWithMongoose(UserModel); |
| 20 | +const PostTC = composeWithMongoose(PostModel); |
| 21 | + |
| 22 | +PostTC.addRelation('author', { |
| 23 | + // resolver: UserTC.getResolver('dataLoader'), // <---- DataLoader for getting one mongoose doc by id (slower than `dataLoaderLean` but more functional) |
| 24 | + resolver: UserTC.getResolver('dataLoaderLean'), // <---- `Lean` loads record from DB without support of mongoose getters & virtuals |
| 25 | + prepareArgs: { |
| 26 | + _id: (s) => s.authorId, |
| 27 | + }, |
| 28 | + projection: { authorId: true }, |
| 29 | +}); |
| 30 | + |
| 31 | +PostTC.addRelation('reviewers', { |
| 32 | + // resolver: UserTC.getResolver('dataLoaderMany'), // <---- DataLoader for getting many mongoose docs by ids (slower than `dataLoaderManyLean` but more functional) |
| 33 | + resolver: UserTC.getResolver('dataLoaderManyLean'), // <---- `Lean` loads records from DB without support of mongoose getters & virtuals |
| 34 | + prepareArgs: { |
| 35 | + _ids: (s) => s.reviewerIds, |
| 36 | + }, |
| 37 | + projection: { reviewerIds: true }, |
| 38 | +}); |
| 39 | + |
| 40 | +schemaComposer.Query.addFields({ |
| 41 | + posts: PostTC.getResolver('findMany'), |
| 42 | +}); |
| 43 | +const schema = schemaComposer.buildSchema(); |
| 44 | + |
| 45 | +// console.log(schemaComposer.toSDL()); |
| 46 | + |
| 47 | +beforeAll(async () => { |
| 48 | + await UserModel.base.createConnection(); |
| 49 | + const User1 = await UserModel.create({ name: 'User1' }); |
| 50 | + const User2 = await UserModel.create({ name: 'User2' }); |
| 51 | + const User3 = await UserModel.create({ name: 'User3' }); |
| 52 | + |
| 53 | + await PostModel.create({ title: 'Post1', authorId: User1._id }); |
| 54 | + await PostModel.create({ title: 'Post2', authorId: User1._id, reviewerIds: [] }); |
| 55 | + await PostModel.create({ title: 'Post3', authorId: User1._id, reviewerIds: [User2._id] }); |
| 56 | + await PostModel.create({ title: 'Post4', authorId: User2._id, reviewerIds: [User1._id] }); |
| 57 | + await PostModel.create({ title: 'Post5', authorId: User2._id, reviewerIds: [User1._id] }); |
| 58 | + await PostModel.create({ |
| 59 | + title: 'Post6', |
| 60 | + authorId: User3._id, |
| 61 | + reviewerIds: [User1._id, User2._id], |
| 62 | + }); |
| 63 | +}); |
| 64 | +afterAll(() => UserModel.base.disconnect()); |
| 65 | + |
| 66 | +describe('issue #260 - new resolvers which works via DataLoader', () => { |
| 67 | + it('check response', async () => { |
| 68 | + // 👀 uncomment next line if you want to see real mongoose queries |
| 69 | + // mongoose.set('debug', true); |
| 70 | + |
| 71 | + expect( |
| 72 | + await graphql.graphql({ |
| 73 | + schema, |
| 74 | + source: `query { |
| 75 | + posts { |
| 76 | + title |
| 77 | + author { name } |
| 78 | + reviewers { name } |
| 79 | + } |
| 80 | + }`, |
| 81 | + contextValue: {}, |
| 82 | + }) |
| 83 | + ).toEqual({ |
| 84 | + data: { |
| 85 | + posts: [ |
| 86 | + { title: 'Post1', author: { name: 'User1' }, reviewers: [] }, |
| 87 | + { title: 'Post2', author: { name: 'User1' }, reviewers: [] }, |
| 88 | + { title: 'Post3', author: { name: 'User1' }, reviewers: [{ name: 'User2' }] }, |
| 89 | + { title: 'Post4', author: { name: 'User2' }, reviewers: [{ name: 'User1' }] }, |
| 90 | + { title: 'Post5', author: { name: 'User2' }, reviewers: [{ name: 'User1' }] }, |
| 91 | + { |
| 92 | + title: 'Post6', |
| 93 | + author: { name: 'User3' }, |
| 94 | + reviewers: [{ name: 'User1' }, { name: 'User2' }], |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments