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
Copy file name to clipboardExpand all lines: README.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Apollo [data source](https://www.apollographql.com/docs/apollo-server/features/d
6
6
npm i apollo-datasource-mongodb
7
7
```
8
8
9
-
This package uses [DataLoader](https://github.com/graphql/dataloader) for batching and per-request memoization caching. It also optionally (if you provide a `ttl`), does shared application-level caching (using either the default Apollo `InMemoryLRUCache` or the [cache you provide to ApolloServer()](https://www.apollographql.com/docs/apollo-server/features/data-sources#using-memcachedredis-as-a-cache-storage-backend)). It does this only for these two methods, which are added to your collections:
9
+
This package uses [DataLoader](https://github.com/graphql/dataloader) for batching and per-request memoization caching. It also optionally (if you provide a `ttl`), does shared application-level caching (using either the default Apollo `InMemoryLRUCache` or the [cache you provide to ApolloServer()](https://www.apollographql.com/docs/apollo-server/features/data-sources#using-memcachedredis-as-a-cache-storage-backend)). It does this only for these two methods:
10
10
11
11
-[`findOneById(id, options)`](#findonebyid)
12
12
-[`findManyByIds(ids, options)`](#findmanybyids)
@@ -32,19 +32,19 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
32
32
33
33
### Basic
34
34
35
-
The basic setup is subclassing `MongoDataSource`, setting your collections in the constructor, and then using the [API methods](#API) on your collections:
35
+
The basic setup is subclassing `MongoDataSource`, setting your collections in the constructor, and then using the [API methods](#API):
@@ -71,7 +71,7 @@ If you want to implement an initialize method, it must call the parent method:
71
71
classMyMongoextendsMongoDataSource {
72
72
constructor() {
73
73
super()
74
-
this.collections=[users, posts]
74
+
this.collections={ users, posts }
75
75
}
76
76
77
77
initialize(config) {
@@ -103,15 +103,15 @@ client.connect(e => {
103
103
classMyMongoextendsMongoDataSource {
104
104
constructor() {
105
105
super()
106
-
this.collections=[users, posts]
106
+
this.collections={ users, posts }
107
107
}
108
108
109
109
getUser(userId) {
110
-
returnusers.findOneById(userId)
110
+
returnthis.users.findOneById(userId)
111
111
}
112
112
113
113
getPosts(postIds) {
114
-
returnposts.findManyByIds(postIds)
114
+
returnthis.posts.findManyByIds(postIds)
115
115
}
116
116
}
117
117
@@ -133,28 +133,28 @@ const server = new ApolloServer({
133
133
})
134
134
```
135
135
136
-
You might prefer to structure it as one data source per collection, in which case you'd do:
136
+
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`:
@@ -223,18 +223,18 @@ Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the
223
223
224
224
### findOneById
225
225
226
-
`collection.findOneById(id, { ttl })`
226
+
`findOneById(id, { ttl })`
227
227
228
228
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).
229
229
230
230
### findManyByIds
231
231
232
-
`collection.findManyByIds(ids, { ttl })`
232
+
`findManyByIds(ids, { ttl })`
233
233
234
234
Calls [`findOneById()`](#findonebyid) for each id. Resolves to an array of documents.
0 commit comments