-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy path376-test.ts
More file actions
159 lines (146 loc) · 3.8 KB
/
376-test.ts
File metadata and controls
159 lines (146 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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);
export interface Profile {
_id: string;
emailAddress: string;
name: string;
}
type ProfileDocType = Profile & mongoose.Document;
const ProfileSchema = new Schema<ProfileDocType>(
{
_id: {
type: String,
required: true,
},
emailAddress: {
type: String,
required: true,
trim: true,
},
name: {
type: String,
required: true,
trim: true,
},
}
);
const ProfileModel = mongoose.model<ProfileDocType>('Profile', ProfileSchema);
composeMongoose<ProfileDocType>(ProfileModel, { schemaComposer });
const PublicProfileTC = composeMongoose<ProfileDocType>(ProfileModel, {
name: "PublicProfile",
onlyFields: ["_id", "name"],
schemaComposer
});
interface Org {
_id: string;
name: string;
ownerId?: string;
}
type OrgDocType = Org & mongoose.Document;
const OrgSchema = new Schema<OrgDocType>(
{
_id: {
type: String,
required: true,
},
name: {
type: String,
required: true,
trim: true,
unique: true,
},
ownerId: String,
},
);
const OrgModel = mongoose.model<OrgDocType>('Org', OrgSchema);
const OrgTC = composeMongoose<OrgDocType>(OrgModel, { schemaComposer });
OrgTC.addRelation('owner', {
resolver: () => PublicProfileTC.mongooseResolvers.findById({ lean: true }),
prepareArgs: {
// Define the args passed to the resolver (eg what the _id value should be)
// Source is the filter passed to the user query
_id: (org) => {
// console.log(org);
return org.ownerId;
},
},
projection: { ownerId: 1 }, // Additional fields from UserSchema we need to pass to the Org resolver
});
schemaComposer.Query.addFields({
orgFindOne: OrgTC.mongooseResolvers.findOne({
lean: true,
filter: {
onlyIndexed: true,
},
}),
orgs: OrgTC.mongooseResolvers.findMany(),
});
const schema = schemaComposer.buildSchema();
beforeAll(async () => {
await OrgModel.base.createConnection();
const profiles = [
{ _id: "1", emailAddress: "user1@example.com", name: 'User1' },
{ _id: "2", emailAddress: "user2@example.com", name: 'User2' },
];
await ProfileModel.create(profiles);
await OrgModel.create([
{ _id: "1", name: 'Organization1' },
{ _id: "2", name: 'Organization2', ownerId: profiles[0]._id },
{ _id: "3", name: 'Organization3', ownerId: profiles[1]._id },
]);
});
afterAll(() => {
OrgModel.base.disconnect();
});
describe('issue #376 - Projection not being added to query in relation with findOne', () => {
it('check', async () => {
const result = await graphql.graphql({
schema,
source: `query($filter: FilterFindOneOrgInput) {
orgFindOne(filter: $filter) {
name
owner {
name
}
}
}`,
variableValues: {
_id: "2",
},
});
expect(result).toEqual({
data: {
orgFindOne:
{ name: 'Organization2', owner: { name: 'User1' } },
},
});
});
});
describe('issue #376 - Projection IS added to query in relation with findMany', () => {
it('check', async () => {
const result = await graphql.graphql({
schema,
source: `query {
orgs {
name
owner {
name
}
}
}`,
});
expect(result).toEqual({
data: {
orgs: [
{ name: 'Organization1', owner: null },
{ name: 'Organization2', owner: { name: 'User1' } },
{ name: 'Organization3', owner: { name: 'User2' } },
],
},
});
});
});