-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdatasource.test.js
246 lines (193 loc) · 6.9 KB
/
datasource.test.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { MongoClient, ObjectId } from 'mongodb'
import mongoose, { Schema, model } from 'mongoose'
import { MongoDataSource } from '../datasource'
import { isModel, isCollectionOrModel, getCollection } from '../helpers'
mongoose.set('useFindAndModify', false)
class Users extends MongoDataSource {
initialize(config) {
super.initialize(config)
}
}
describe('MongoDataSource', () => {
it('sets up caching functions', () => {
const users = {}
const source = new Users(users)
source.initialize()
expect(source.findOneById).toBeDefined()
expect(source.findByFields).toBeDefined()
expect(source.deleteFromCacheById).toBeDefined()
expect(source.deleteFromCacheByFields).toBeDefined()
expect(source.collection).toEqual(users)
})
})
const URL = 'mongodb://localhost:27017/test-apollo-datasource'
const connectArgs = [
URL,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
]
const connect = async () => {
const client = new MongoClient(...connectArgs)
await mongoose.connect(...connectArgs)
await client.connect()
return client.db()
}
const hexId = '5cf82e14a220a607eb64a7d4'
const objectID = ObjectId(hexId)
describe('Mongoose', () => {
let UserModel
let userCollection
let alice
let nestedBob
let nestedCharlie
let nestedDan
beforeAll(async () => {
const userSchema = new Schema({ name: 'string' })
UserModel = model('User', userSchema)
const db = await connect()
userCollection = db.collection('users')
alice = await UserModel.findOneAndUpdate(
{ name: 'Alice' },
{ name: 'Alice' },
{ upsert: true, new: true }
)
nestedBob = await userCollection.findOneAndReplace(
{ name: 'Bob' },
{
name: 'Bob',
nested: { _id: objectID, field1: 'value1', field2: 'value1' }
},
{ new: true, upsert: true }
)
nestedCharlie = await userCollection.findOneAndReplace(
{ name: 'Charlie' },
{
name: 'Charlie',
nested: { field1: 'value2', field2: 'value2' }
},
{ new: true, upsert: true }
)
nestedDan = await userCollection.findOneAndReplace(
{ name: 'Dan' },
{
name: 'Dan',
nested: { field1: 'value1', field2: 'value2' }
},
{ new: true, upsert: true }
)
})
test('isCollectionOrModel', () => {
expect(isCollectionOrModel(userCollection)).toBe(true)
expect(isCollectionOrModel(UserModel)).toBe(true)
expect(isCollectionOrModel(Function.prototype)).toBe(false)
expect(isCollectionOrModel(undefined)).toBe(false)
})
test('isModel', () => {
expect(isModel(userCollection)).toBe(false)
expect(isModel(UserModel)).toBe(true)
expect(isCollectionOrModel(Function.prototype)).toBe(false)
expect(isCollectionOrModel(undefined)).toBe(false)
})
test('mongoose class-based components', () => {
/**
* @see https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/51
*/
const ClassModel = mongoose.model(
class ClassModel extends mongoose.Model {},
new Schema({ name: 'string' })
)
expect(isModel(ClassModel)).toBe(true)
expect(isCollectionOrModel(ClassModel)).toBe(true)
})
test('getCollectionName', () => {
expect(getCollection(userCollection).collectionName).toBe('users')
expect(getCollection(UserModel).collectionName).toBe('users')
})
test('Data Source with Model', async () => {
const users = new Users(UserModel)
users.initialize()
const user = await users.findOneById(alice._id)
expect(user.name).toBe('Alice')
expect(user.id).toBe(alice._id.toString())
})
test('Data Source with Collection', async () => {
const users = new Users(userCollection)
users.initialize()
const user = await users.findOneById(alice._id)
expect(user.name).toBe('Alice')
expect(user.id).toBeUndefined()
})
test('nested findByFields', async () => {
const users = new Users(userCollection)
users.initialize()
const [user] = await users.findByFields({ 'nested._id': objectID })
expect(user).toBeDefined()
expect(user.name).toBe('Bob')
const res1 = await users.findByFields({ 'nested.field1': 'value1' })
const res2 = await users.findByFields({ 'nested.field2': '' })
expect(res1[0].name).toBe('Bob')
expect(res2[0]).toBeUndefined()
})
test('nested findByFields with single filter and batching', async () => {
const users = new Users(userCollection)
users.initialize()
let pendingDocs = []
for (var i = 1; i <= 3; i++) {
pendingDocs.push(users.findByFields({ 'nested.field1': `value${i}` }))
}
/*
Intent here, with Promise.All, is to force batching to happen in the underlying dataloader library.
This results in the following optimized filter to be passed to MongoDb:
filter: {
'nested.field1': { '$in': [ 'value1', 'value2', 'value3' ] }
}
This in turn correctly matches Bob, Charlie and Dan records.
Bob and Dan match filters passed to the first invocation of findByFields function: { 'nested.field1': [ 'value1' ] }
Charlie matches filters passed to the second invocation of findByFields function: { 'nested.field1': [ 'value2' ] }
*/
const docs = await Promise.all(pendingDocs)
expect(docs[0][0].name).toBe('Bob')
expect(docs[0][1].name).toBe('Dan')
expect(docs[0].length).toBe(2)
expect(docs[1][0].name).toBe('Charlie')
expect(docs[1].length).toBe(1)
expect(docs[2][0]).toBeUndefined()
expect(docs[2].length).toBe(0)
expect(docs.length).toBe(3)
})
test('nested findByFields with multiple filters and batching', async () => {
const users = new Users(userCollection)
users.initialize()
let pendingDocs = []
for (var i = 1; i <= 3; i++) {
pendingDocs.push(
users.findByFields({
'nested.field1': `value${i}`,
'nested.field2': `value${i}`
})
)
}
/*
Intent here, with Promise.All, is to force batching to happen in the underlying dataloader library.
This results in the following optimized filter to be passed to MongoDb:
filter: {
'nested.field1': { '$in': [ 'value1', 'value2', 'value3' ] },
'nested.field2': { '$in': [ 'value1', 'value2', 'value3' ] }
}
This in turn correctly matches Bob, Charlie and Dan records.
However, only Bob and Charlie match original filters passed to findByFields function, so only those should be returned.
{ 'nested.field1': [ 'value1' ], 'nested.field2': [ 'value1' ] },
{ 'nested.field1': [ 'value2' ], 'nested.field2': [ 'value2' ] }
*/
const docs = await Promise.all(pendingDocs)
expect(docs[0][0].name).toBe('Bob')
expect(docs[0].length).toBe(1)
expect(docs[1][0].name).toBe('Charlie')
expect(docs[1].length).toBe(1)
expect(docs[2][0]).toBeUndefined()
expect(docs[2].length).toBe(0)
expect(docs.length).toBe(3)
})
})