You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`:
63
+
The collection is available at `this.users` (e.g. `this.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`:
author: (post, _, { db }) =>db.getUser(post.authorId)
114
-
},
115
-
User: {
116
-
posts: (user, _, { db }) =>db.getPosts(user.postIds)
117
-
}
118
-
}
119
-
120
-
constserver=newApolloServer({
121
-
typeDefs,
122
-
resolvers,
123
-
dataSources: () => ({
124
-
db:newMyMongo({ users, posts })
125
-
})
126
-
})
127
-
```
128
-
129
-
You might prefer to structure it as one data source per collection, in which case you'd set `this.collection` instead of `this.collections`, and the [API methods](#api) would be available directly on `this`:
130
-
131
94
```js
132
95
classUsersextendsMongoDataSource {
133
-
constructor() {
134
-
super()
135
-
this.collection= users
136
-
}
137
-
138
96
getUser(userId) {
139
97
returnthis.findOneById(userId)
140
98
}
141
99
}
142
100
143
101
classPostsextendsMongoDataSource {
144
-
constructor() {
145
-
super()
146
-
this.collection= posts
147
-
}
148
-
149
102
getPosts(postIds) {
150
103
returnthis.findManyByIds(postIds)
151
104
}
@@ -160,38 +113,34 @@ const resolvers = {
160
113
}
161
114
}
162
115
116
+
constusers=db.collection('users')
117
+
constposts=db.collection('posts')
118
+
163
119
constserver=newApolloServer({
164
120
typeDefs,
165
121
resolvers,
166
122
dataSources: () => ({
167
-
users:newUsers(),
168
-
posts:newPosts()
123
+
users:newUsers({ users }),
124
+
posts:newPosts({ posts })
169
125
})
170
126
})
171
127
```
172
128
173
-
This is purely a code structure choice—it doesn't affect batching or caching. The latter option probably makes more sense if you have more than a few methods in your class.
174
-
175
129
### Caching
176
130
177
131
To enable shared application-level caching, you do everything from the above section, and you add the `ttl` option to `findOneById()`:
@@ -216,18 +165,18 @@ Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the
216
165
217
166
### findOneById
218
167
219
-
`findOneById(id, { ttl })`
168
+
`this.findOneById(id, { ttl })`
220
169
221
170
Resolves to the found document. Uses DataLoader to load `id`. DataLoader uses `collection.find({ _id: { $in: ids } })`. Optionally caches the document if `ttl` is set (in whole seconds).
222
171
223
172
### findManyByIds
224
173
225
-
`findManyByIds(ids, { ttl })`
174
+
`this.findManyByIds(ids, { ttl })`
226
175
227
176
Calls [`findOneById()`](#findonebyid) for each id. Resolves to an array of documents.
0 commit comments