Skip to content

Commit f49fda3

Browse files
committed
Update Test for Issue 376 to Produce Error
* Updated the test case for issue 376 so that it exposes at least one scenario where this error is occurring. I restructured the schema a bit and added further typings to mirror the schema I have been testing with. * Apparently the projection is added in some simple cases with findMany(), but when using findOne() (at a minimum), it appears to not work. There are now two tests: the first one fails (with findOne(), and the second one succeeds (with findMany()). * Updated to graphql-compose 9.0.8 to ensure testing is against the latest version. graphql-compose#376 graphql-compose/graphql-compose#382
1 parent b640824 commit f49fda3

File tree

3 files changed

+98
-62
lines changed

3 files changed

+98
-62
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"eslint-plugin-import": "2.25.3",
4343
"eslint-plugin-prettier": "4.0.0",
4444
"graphql": "16.1.0",
45-
"graphql-compose": "9.0.5",
45+
"graphql-compose": "9.0.8",
4646
"jest": "27.4.5",
4747
"mongodb-memory-server": "8.0.4",
4848
"mongoose": "6.1.2",

src/__tests__/github_issues/376-test.ts

+93-57
Original file line numberDiff line numberDiff line change
@@ -7,115 +7,151 @@ const schemaComposer = new SchemaComposer<{ req: any }>();
77

88
// mongoose.set('debug', true);
99

10-
const UserSchema = new Schema(
10+
export interface Profile {
11+
_id: string;
12+
emailAddress: string;
13+
name: string;
14+
}
15+
16+
type ProfileDocType = Profile & mongoose.Document;
17+
const ProfileSchema = new Schema<ProfileDocType>(
1118
{
12-
name: {
19+
_id: {
1320
type: String,
1421
required: true,
15-
trim: true,
1622
},
17-
orgId: {
18-
type: mongoose.Schema.Types.ObjectId,
19-
ref: 'Org',
23+
emailAddress: {
24+
type: String,
2025
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-
},
26+
trim: true,
2727
},
28-
},
29-
{
30-
collection: 'users',
31-
timestamps: {
32-
createdAt: 'created',
33-
updatedAt: 'modified',
28+
name: {
29+
type: String,
30+
required: true,
31+
trim: true,
3432
},
3533
}
3634
);
37-
const UserModel = mongoose.model<any>('User', UserSchema);
38-
const UserTC = composeMongoose(UserModel, { schemaComposer });
35+
const ProfileModel = mongoose.model<ProfileDocType>('Profile', ProfileSchema);
36+
composeMongoose<ProfileDocType>(ProfileModel, { schemaComposer });
37+
38+
const PublicProfileTC = composeMongoose<ProfileDocType>(ProfileModel, {
39+
name: "PublicProfile",
40+
onlyFields: ["_id", "name"],
41+
schemaComposer
42+
});
3943

40-
const OrgSchema = new Schema(
44+
interface Org {
45+
_id: string;
46+
name: string;
47+
ownerId?: string;
48+
}
49+
50+
type OrgDocType = Org & mongoose.Document;
51+
const OrgSchema = new Schema<OrgDocType>(
4152
{
53+
_id: {
54+
type: String,
55+
required: true,
56+
},
4257
name: {
4358
type: String,
4459
required: true,
4560
trim: true,
4661
unique: true,
4762
},
48-
Users: [
49-
{
50-
type: mongoose.Schema.Types.ObjectId,
51-
ref: 'User',
52-
},
53-
],
63+
ownerId: String,
5464
},
55-
{
56-
collection: 'orgs',
57-
timestamps: {
58-
createdAt: 'created',
59-
updatedAt: 'modified',
60-
},
61-
}
6265
);
63-
const OrgModel = mongoose.model<any>('Org', OrgSchema);
64-
const OrgTC = composeMongoose(OrgModel, { schemaComposer });
66+
const OrgModel = mongoose.model<OrgDocType>('Org', OrgSchema);
67+
const OrgTC = composeMongoose<OrgDocType>(OrgModel, { schemaComposer });
6568

66-
UserTC.addRelation('org', {
67-
resolver: () => OrgTC.mongooseResolvers.findById(),
69+
OrgTC.addRelation('owner', {
70+
resolver: () => PublicProfileTC.mongooseResolvers.findById({ lean: true }),
6871
prepareArgs: {
6972
// Define the args passed to the resolver (eg what the _id value should be)
7073
// Source is the filter passed to the user query
71-
_id: (source) => {
72-
// console.log(source);
73-
return source.orgId;
74+
_id: (org) => {
75+
// console.log(org);
76+
return org.ownerId;
7477
},
7578
},
76-
projection: { orgId: true }, // Additional fields from UserSchema we need to pass to the Org resolver
79+
projection: { ownerId: 1 }, // Additional fields from UserSchema we need to pass to the Org resolver
7780
});
7881

7982
schemaComposer.Query.addFields({
80-
users: UserTC.mongooseResolvers.findMany(),
83+
orgFindOne: OrgTC.mongooseResolvers.findOne({
84+
lean: true,
85+
filter: {
86+
onlyIndexed: true,
87+
},
88+
}),
89+
orgs: OrgTC.mongooseResolvers.findMany(),
8190
});
8291

8392
const schema = schemaComposer.buildSchema();
8493

8594
beforeAll(async () => {
8695
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 },
96+
const profiles = [
97+
{ _id: "1", emailAddress: "[email protected]", name: 'User1' },
98+
{ _id: "2", emailAddress: "[email protected]", name: 'User2' },
99+
];
100+
await ProfileModel.create(profiles);
101+
await OrgModel.create([
102+
{ _id: "1", name: 'Organization1' },
103+
{ _id: "2", name: 'Organization2', ownerId: profiles[0]._id },
104+
{ _id: "3", name: 'Organization3', ownerId: profiles[1]._id },
95105
]);
96106
});
97107
afterAll(() => {
98108
OrgModel.base.disconnect();
99109
});
100110

101-
describe('issue #376 - Projection not being added to query in relation', () => {
111+
describe('issue #376 - Projection not being added to query in relation with findOne', () => {
112+
it('check', async () => {
113+
const result = await graphql.graphql({
114+
schema,
115+
source: `query($filter: FilterFindOneOrgInput) {
116+
orgFindOne(filter: $filter) {
117+
name
118+
owner {
119+
name
120+
}
121+
}
122+
}`,
123+
variableValues: {
124+
_id: "2",
125+
},
126+
});
127+
expect(result).toEqual({
128+
data: {
129+
orgFindOne:
130+
{ name: 'Organization2', owner: { name: 'User1' } },
131+
},
132+
});
133+
});
134+
});
135+
136+
describe('issue #376 - Projection IS added to query in relation with findMany', () => {
102137
it('check', async () => {
103138
const result = await graphql.graphql({
104139
schema,
105140
source: `query {
106-
users(sort: _ID_ASC) {
141+
orgs {
107142
name
108-
org {
143+
owner {
109144
name
110145
}
111146
}
112147
}`,
113148
});
114149
expect(result).toEqual({
115150
data: {
116-
users: [
117-
{ name: 'User1', org: { name: 'Organization2' } },
118-
{ name: 'User2', org: { name: 'Organization3' } },
151+
orgs: [
152+
{ name: 'Organization1', owner: null },
153+
{ name: 'Organization2', owner: { name: 'User1' } },
154+
{ name: 'Organization3', owner: { name: 'User2' } },
119155
],
120156
},
121157
});

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2960,10 +2960,10 @@ [email protected]:
29602960
resolved "https://registry.yarnpkg.com/graphql-compose-pagination/-/graphql-compose-pagination-8.3.0.tgz#a58c786256b4dba1843d495ae81cb0fc67ac1369"
29612961
integrity sha512-zZoBhlWD9KnAJ9AUOeFNeoE4Q/zMxZaxg0tR+HFMVNFnw7ENUnIChMWnD2uvmvik+k8Yv304hMF4n81okGVsJw==
29622962

2963-
2964-
version "9.0.5"
2965-
resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-9.0.5.tgz#673f5ea0bc2f75b797095d93778cc0ce2adefd26"
2966-
integrity sha512-u4QujGRgk2u1Y+LmKxVDJ7AQB7gj2RBI3GPPUFJRXOZOc5DDUUVugSaTEtjo8otGnYtrNbmqjB2xzYMUkQ2/ZQ==
2963+
2964+
version "9.0.8"
2965+
resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-9.0.8.tgz#920b6b0584f5e3784a532138974063d3807f7455"
2966+
integrity sha512-I3zvygpVz5hOWk2cYL6yhbgfKbNWbiZFNXlWkv/55U+lX6Y3tL+SyY3zunw7QWrN/qtwG2DqZb13SHTv2MgdEQ==
29672967
dependencies:
29682968
graphql-type-json "0.3.2"
29692969

0 commit comments

Comments
 (0)