Skip to content

Commit 4fb16e5

Browse files
committed
Pass collections to constructor
1 parent d3cae61 commit 4fb16e5

File tree

3 files changed

+26
-54
lines changed

3 files changed

+26
-54
lines changed

README.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,27 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
3232

3333
### Basic
3434

35-
The basic setup is subclassing `MongoDataSource`, setting your collections in the constructor, and then using the [API methods](#API):
35+
The basic setup is subclassing `MongoDataSource`, passing your collection(s) to the constructor, and using the [API methods](#API):
3636

3737
```js
3838
import { MongoDataSource } from 'apollo-datasource-mongodb'
3939

4040
class MyMongo extends MongoDataSource {
41-
constructor() {
42-
super()
43-
this.collections = { users, posts }
44-
}
45-
4641
getUser(userId) {
4742
return this.users.findOneById(userId)
4843
}
4944
}
45+
46+
const server = new ApolloServer({
47+
typeDefs,
48+
resolvers,
49+
dataSources: () => ({
50+
db: new MyMongo({ users, posts })
51+
})
52+
})
5053
```
5154

52-
The request's context is available at `this.context`. For example, if you put the logged-in user's ID on context as `context.currentUserId`:
55+
The collections are available at `this.collections` (e.g. `this.collections.users.update({_id: 'foo, { $set: { name: 'me' }}})`). The request's context is available at `this.context`. For example, if you put the logged-in user's ID on context as `context.currentUserId`:
5356

5457
```js
5558
class MyMongo extends MongoDataSource {
@@ -69,11 +72,6 @@ If you want to implement an initialize method, it must call the parent method:
6972

7073
```js
7174
class MyMongo extends MongoDataSource {
72-
constructor() {
73-
super()
74-
this.collections = { users, posts }
75-
}
76-
7775
initialize(config) {
7876
super.initialize(config)
7977
...
@@ -101,11 +99,6 @@ client.connect(e => {
10199
})
102100

103101
class MyMongo extends MongoDataSource {
104-
constructor() {
105-
super()
106-
this.collections = { users, posts }
107-
}
108-
109102
getUser(userId) {
110103
return this.users.findOneById(userId)
111104
}
@@ -128,7 +121,7 @@ const server = new ApolloServer({
128121
typeDefs,
129122
resolvers,
130123
dataSources: () => ({
131-
db: new MyMongo()
124+
db: new MyMongo({ users, posts })
132125
})
133126
})
134127
```

src/__tests__/datasource.test.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,16 @@ const users = {}
44
const posts = {}
55

66
class MyMongo extends MongoDataSource {
7-
constructor() {
8-
super()
9-
this.collections = { users, posts }
10-
}
11-
127
initialize(config) {
138
super.initialize(config)
149
}
1510
}
1611

17-
class SingleCollection extends MongoDataSource {
18-
constructor() {
19-
super()
20-
this.collection = users
21-
}
22-
}
23-
2412
describe('MongoDataSource', () => {
2513
it('sets up caching functions', () => {
26-
const source = new MyMongo()
14+
const source = new MyMongo({ users, posts })
2715
source.initialize({})
2816
expect(source.users.findOneById).toBeDefined()
2917
expect(source.posts.findOneById).toBeDefined()
3018
})
31-
32-
it('sets up caching functions for single collection', () => {
33-
const source = new SingleCollection()
34-
source.initialize({})
35-
expect(source.findOneById).toBeDefined()
36-
})
3719
})

src/datasource.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@ import { InMemoryLRUCache } from 'apollo-server-caching'
55
import { createCachingMethods } from './cache'
66

77
class MongoDataSource extends DataSource {
8-
// https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource/src/index.ts
9-
initialize(config) {
10-
this.context = config.context
8+
constructor(collections) {
9+
super()
1110

12-
const setUpCorrectly =
13-
typeof this.collections === 'object' || this.collection
11+
const setUpCorrectly = typeof collections === 'object'
1412
if (!setUpCorrectly) {
1513
throw new ApolloError(
16-
'Child class of MongoDataSource must set this.collections or this.collection in constructor'
14+
'MongoDataSource constructor must be given an object with collection(s)'
1715
)
1816
}
1917

18+
this.collections = collections
19+
}
20+
21+
// https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource/src/index.ts
22+
initialize(config) {
23+
this.context = config.context
24+
2025
const cache = config.cache || new InMemoryLRUCache()
2126

22-
if (this.collections) {
23-
for (const key in this.collections) {
24-
this[key] = createCachingMethods({
25-
collection: this.collections[key],
26-
cache
27-
})
28-
}
29-
} else {
30-
const methods = createCachingMethods({
31-
collection: this.collection,
27+
for (const key in this.collections) {
28+
this[key] = createCachingMethods({
29+
collection: this.collections[key],
3230
cache
3331
})
34-
Object.assign(this, methods)
3532
}
3633
}
3734
}

0 commit comments

Comments
 (0)