Skip to content

Commit ff41fd7

Browse files
committed
Add to README: context and super.initialize()
1 parent a297be0 commit ff41fd7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
2929

3030
## Usage
3131

32+
### Basic
33+
3234
The basic setup is subclassing `MongoDataSource`, setting your collections in the constructor, and then using the [API methods](#API) on your collections:
3335

3436
```js
@@ -46,6 +48,38 @@ class MyMongo extends MongoDataSource {
4648
}
4749
```
4850

51+
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`:
52+
53+
```js
54+
class MyMongo extends MongoDataSource {
55+
...
56+
57+
async getPrivateUserData(userId) {
58+
const isAuthorized = this.context.currentUserId === userId
59+
if (isAuthorized) {
60+
const user = await users.findOneById(userId)
61+
return user && user.privateData
62+
}
63+
}
64+
}
65+
```
66+
67+
If you want to implement an initialize method, it must call the parent method:
68+
69+
```js
70+
class MyMongo extends MongoDataSource {
71+
constructor() {
72+
super()
73+
this.collections = [users, posts]
74+
}
75+
76+
initialize(config) {
77+
super.initialize(config)
78+
...
79+
}
80+
}
81+
```
82+
4983
### Batching
5084

5185
This is the main feature, and is always enabled. Here's a full example:

src/__tests__/datasource.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class MyMongo extends MongoDataSource {
88
super()
99
this.collections = [users, posts]
1010
}
11+
12+
initialize(config) {
13+
super.initialize(config)
14+
}
1115
}
1216

1317
describe('MongoDataSource', () => {

0 commit comments

Comments
 (0)