Skip to content

Commit 2c334fe

Browse files
committed
test: check 377 issue that projection works
relates #377
1 parent 8e629fa commit 2c334fe

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { SchemaComposer, graphql } from 'graphql-compose';
2+
import { composeMongoose } from '../../index';
3+
import { mongoose } from '../../__mocks__/mongooseCommon';
4+
import { Schema } from 'mongoose';
5+
6+
const schemaComposer = new SchemaComposer<{ req: any }>();
7+
8+
// mongoose.set('debug', true);
9+
10+
const PrevisitSchema = new Schema(
11+
{
12+
name: {
13+
type: String,
14+
required: true,
15+
trim: true,
16+
},
17+
project_id: { type: Schema.Types.ObjectId, required: true },
18+
},
19+
{
20+
collection: 'previsits',
21+
}
22+
);
23+
const PrevisitModel = mongoose.model<any>('Previsit', PrevisitSchema);
24+
const PrevisitTC = composeMongoose(PrevisitModel, { schemaComposer });
25+
26+
const ProjectSchema = new Schema(
27+
{
28+
name: {
29+
type: String,
30+
required: true,
31+
trim: true,
32+
unique: true,
33+
},
34+
},
35+
{
36+
collection: 'projects',
37+
}
38+
);
39+
const ProjectModel = mongoose.model<any>('Project', ProjectSchema);
40+
const ProjectTC = composeMongoose(ProjectModel, { schemaComposer });
41+
42+
PrevisitTC.addRelation('project', {
43+
resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
44+
prepareArgs: {
45+
_id: (source) => source.project_id || null,
46+
},
47+
projection: { project_id: 1 },
48+
});
49+
50+
schemaComposer.Query.addFields({
51+
previsitMany: PrevisitTC.mongooseResolvers.findMany(),
52+
});
53+
54+
const schema = schemaComposer.buildSchema();
55+
56+
beforeAll(async () => {
57+
await ProjectModel.base.createConnection();
58+
const projects = await ProjectModel.create([
59+
{ name: 'Project1' },
60+
{ name: 'Project2' },
61+
{ name: 'Project3' },
62+
]);
63+
await PrevisitModel.create([
64+
{ name: 'Previsit1', project_id: projects[0] },
65+
{ name: 'Previsit2', project_id: projects[1] },
66+
{ name: 'Previsit3', project_id: projects[2] },
67+
]);
68+
});
69+
afterAll(() => {
70+
ProjectModel.base.disconnect();
71+
});
72+
73+
describe('issue #377 - Missing fields from projection in addRelation', () => {
74+
it('check', async () => {
75+
const result = await graphql.graphql({
76+
schema,
77+
contextValue: {},
78+
source: `
79+
{
80+
previsitMany {
81+
name
82+
project {
83+
name
84+
}
85+
}
86+
}
87+
`,
88+
});
89+
expect(result).toEqual({
90+
data: {
91+
previsitMany: [
92+
{ name: 'Previsit1', project: { name: 'Project1' } },
93+
{ name: 'Previsit2', project: { name: 'Project2' } },
94+
{ name: 'Previsit3', project: { name: 'Project3' } },
95+
],
96+
},
97+
});
98+
});
99+
});

0 commit comments

Comments
 (0)