From 2c334fe7a70251735a744fcdc5b494d5848b7304 Mon Sep 17 00:00:00 2001 From: nodkz Date: Tue, 14 Sep 2021 12:10:36 +0600 Subject: [PATCH] test: check 377 issue that `projection` works relates #377 --- src/__tests__/github_issues/377-test.ts | 99 +++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/__tests__/github_issues/377-test.ts diff --git a/src/__tests__/github_issues/377-test.ts b/src/__tests__/github_issues/377-test.ts new file mode 100644 index 00000000..89d26a36 --- /dev/null +++ b/src/__tests__/github_issues/377-test.ts @@ -0,0 +1,99 @@ +import { SchemaComposer, graphql } from 'graphql-compose'; +import { composeMongoose } from '../../index'; +import { mongoose } from '../../__mocks__/mongooseCommon'; +import { Schema } from 'mongoose'; + +const schemaComposer = new SchemaComposer<{ req: any }>(); + +// mongoose.set('debug', true); + +const PrevisitSchema = new Schema( + { + name: { + type: String, + required: true, + trim: true, + }, + project_id: { type: Schema.Types.ObjectId, required: true }, + }, + { + collection: 'previsits', + } +); +const PrevisitModel = mongoose.model('Previsit', PrevisitSchema); +const PrevisitTC = composeMongoose(PrevisitModel, { schemaComposer }); + +const ProjectSchema = new Schema( + { + name: { + type: String, + required: true, + trim: true, + unique: true, + }, + }, + { + collection: 'projects', + } +); +const ProjectModel = mongoose.model('Project', ProjectSchema); +const ProjectTC = composeMongoose(ProjectModel, { schemaComposer }); + +PrevisitTC.addRelation('project', { + resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }), + prepareArgs: { + _id: (source) => source.project_id || null, + }, + projection: { project_id: 1 }, +}); + +schemaComposer.Query.addFields({ + previsitMany: PrevisitTC.mongooseResolvers.findMany(), +}); + +const schema = schemaComposer.buildSchema(); + +beforeAll(async () => { + await ProjectModel.base.createConnection(); + const projects = await ProjectModel.create([ + { name: 'Project1' }, + { name: 'Project2' }, + { name: 'Project3' }, + ]); + await PrevisitModel.create([ + { name: 'Previsit1', project_id: projects[0] }, + { name: 'Previsit2', project_id: projects[1] }, + { name: 'Previsit3', project_id: projects[2] }, + ]); +}); +afterAll(() => { + ProjectModel.base.disconnect(); +}); + +describe('issue #377 - Missing fields from projection in addRelation', () => { + it('check', async () => { + const result = await graphql.graphql({ + schema, + contextValue: {}, + source: ` + { + previsitMany { + name + project { + name + } + } + } + `, + }); + expect(result).toEqual({ + data: { + previsitMany: [ + { name: 'Previsit1', project: { name: 'Project1' } }, + { name: 'Previsit2', project: { name: 'Project2' } }, + { name: 'Previsit3', project: { name: 'Project3' } }, + ], + }, + }); + }); +});