@@ -7,115 +7,151 @@ const schemaComposer = new SchemaComposer<{ req: any }>();
7
7
8
8
// mongoose.set('debug', true);
9
9
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 > (
11
18
{
12
- name : {
19
+ _id : {
13
20
type : String ,
14
21
required : true ,
15
- trim : true ,
16
22
} ,
17
- orgId : {
18
- type : mongoose . Schema . Types . ObjectId ,
19
- ref : 'Org' ,
23
+ emailAddress : {
24
+ type : String ,
20
25
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 ,
27
27
} ,
28
- } ,
29
- {
30
- collection : 'users' ,
31
- timestamps : {
32
- createdAt : 'created' ,
33
- updatedAt : 'modified' ,
28
+ name : {
29
+ type : String ,
30
+ required : true ,
31
+ trim : true ,
34
32
} ,
35
33
}
36
34
) ;
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
+ } ) ;
39
43
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 > (
41
52
{
53
+ _id : {
54
+ type : String ,
55
+ required : true ,
56
+ } ,
42
57
name : {
43
58
type : String ,
44
59
required : true ,
45
60
trim : true ,
46
61
unique : true ,
47
62
} ,
48
- Users : [
49
- {
50
- type : mongoose . Schema . Types . ObjectId ,
51
- ref : 'User' ,
52
- } ,
53
- ] ,
63
+ ownerId : String ,
54
64
} ,
55
- {
56
- collection : 'orgs' ,
57
- timestamps : {
58
- createdAt : 'created' ,
59
- updatedAt : 'modified' ,
60
- } ,
61
- }
62
65
) ;
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 } ) ;
65
68
66
- UserTC . addRelation ( 'org ' , {
67
- resolver : ( ) => OrgTC . mongooseResolvers . findById ( ) ,
69
+ OrgTC . addRelation ( 'owner ' , {
70
+ resolver : ( ) => PublicProfileTC . mongooseResolvers . findById ( { lean : true } ) ,
68
71
prepareArgs : {
69
72
// Define the args passed to the resolver (eg what the _id value should be)
70
73
// 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 ;
74
77
} ,
75
78
} ,
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
77
80
} ) ;
78
81
79
82
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 ( ) ,
81
90
} ) ;
82
91
83
92
const schema = schemaComposer . buildSchema ( ) ;
84
93
85
94
beforeAll ( async ( ) => {
86
95
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 } ,
95
105
] ) ;
96
106
} ) ;
97
107
afterAll ( ( ) => {
98
108
OrgModel . base . disconnect ( ) ;
99
109
} ) ;
100
110
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' , ( ) => {
102
137
it ( 'check' , async ( ) => {
103
138
const result = await graphql . graphql ( {
104
139
schema,
105
140
source : `query {
106
- users(sort: _ID_ASC) {
141
+ orgs {
107
142
name
108
- org {
143
+ owner {
109
144
name
110
145
}
111
146
}
112
147
}` ,
113
148
} ) ;
114
149
expect ( result ) . toEqual ( {
115
150
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' } } ,
119
155
] ,
120
156
} ,
121
157
} ) ;
0 commit comments