Skip to content

Commit 5a4a9f7

Browse files
committed
test: check that projection works in addRelation()
relates #376
1 parent 7d062a6 commit 5a4a9f7

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 UserSchema = new Schema(
11+
{
12+
name: {
13+
type: String,
14+
required: true,
15+
trim: true,
16+
},
17+
orgId: {
18+
type: mongoose.Schema.Types.ObjectId,
19+
ref: 'Org',
20+
required: true,
21+
set: function (this: any, newOrgId: any) {
22+
// temporarily store previous org so
23+
// assignment to new org will work.
24+
this._prevOrg = this.orgId;
25+
return newOrgId;
26+
},
27+
},
28+
},
29+
{
30+
collection: 'users',
31+
timestamps: {
32+
createdAt: 'created',
33+
updatedAt: 'modified',
34+
},
35+
}
36+
);
37+
const UserModel = mongoose.model<any>('User', UserSchema);
38+
const UserTC = composeMongoose(UserModel, { schemaComposer });
39+
40+
const OrgSchema = new Schema(
41+
{
42+
name: {
43+
type: String,
44+
required: true,
45+
trim: true,
46+
unique: true,
47+
},
48+
Users: [
49+
{
50+
type: mongoose.Schema.Types.ObjectId,
51+
ref: 'User',
52+
},
53+
],
54+
},
55+
{
56+
collection: 'orgs',
57+
timestamps: {
58+
createdAt: 'created',
59+
updatedAt: 'modified',
60+
},
61+
}
62+
);
63+
const OrgModel = mongoose.model<any>('Org', OrgSchema);
64+
const OrgTC = composeMongoose(OrgModel, { schemaComposer });
65+
66+
UserTC.addRelation('org', {
67+
resolver: () => OrgTC.mongooseResolvers.findById(),
68+
prepareArgs: {
69+
// Define the args passed to the resolver (eg what the _id value should be)
70+
// Source is the filter passed to the user query
71+
_id: (source) => {
72+
// console.log(source);
73+
return source.orgId;
74+
},
75+
},
76+
projection: { orgId: true }, // Additional fields from UserSchema we need to pass to the Org resolver
77+
});
78+
79+
schemaComposer.Query.addFields({
80+
users: UserTC.mongooseResolvers.findMany(),
81+
});
82+
83+
const schema = schemaComposer.buildSchema();
84+
85+
beforeAll(async () => {
86+
await OrgModel.base.createConnection();
87+
const orgs = await OrgModel.create([
88+
{ name: 'Organization1' },
89+
{ name: 'Organization2' },
90+
{ name: 'Organization3' },
91+
]);
92+
await UserModel.create([
93+
{ name: 'User1', orgId: orgs[1]._id },
94+
{ name: 'User2', orgId: orgs[2]._id },
95+
]);
96+
});
97+
afterAll(() => {
98+
OrgModel.base.disconnect();
99+
});
100+
101+
describe('issue #376 - Projection not being added to query in relation', () => {
102+
it('check', async () => {
103+
const result = await graphql.graphql({
104+
schema,
105+
source: `query {
106+
users(sort: _ID_ASC) {
107+
name
108+
org {
109+
name
110+
}
111+
}
112+
}`,
113+
});
114+
expect(result).toEqual({
115+
data: {
116+
users: [
117+
{ name: 'User1', org: { name: 'Organization2' } },
118+
{ name: 'User2', org: { name: 'Organization3' } },
119+
],
120+
},
121+
});
122+
});
123+
});

0 commit comments

Comments
 (0)