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
Apollo [data source](https://www.apollographql.com/docs/apollo-server/features/data-sources) for MongoDB
3
+
Apollo [data source](https://www.apollographql.com/docs/apollo-server/data/fetching-data) for MongoDB
4
4
5
-
```
6
-
npm i apollo-datasource-mongodb
7
-
```
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 for the following methods:
5
+
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/performance/cache-backends#configuring-external-caching)). It does this for the following methods:
10
6
11
7
-[`findOneById(id, options)`](#findonebyid)
12
8
-[`findManyByIds(ids, options)`](#findmanybyids)
13
9
-[`findByFields(fields, options)`](#findbyfields)
14
10
15
-
16
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
17
12
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
18
-
**Contents:**
19
13
14
+
-[Compatibility](#compatibility)
20
15
-[Usage](#usage)
16
+
-[Install](#install)
21
17
-[Basic](#basic)
22
18
-[Batching](#batching)
23
19
-[Caching](#caching)
@@ -28,12 +24,25 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
// users: new Users({ modelOrCollection: UserModel })
85
+
}
86
+
}),
71
87
})
72
88
```
73
89
74
-
Inside the data source, the collection is available at `this.collection` (e.g. `this.collection.update({_id: 'foo, { $set: { name: 'me' }}})`). The model (if you're using Mongoose) is available at `this.model` (`new this.model({ name: 'Alice' })`). 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`:
90
+
Inside the data source, the collection is available at `this.collection` (e.g. `this.collection.update({_id: 'foo, { $set: { name: 'me' }}})`). The model (if you're using Mongoose) is available at `this.model` (`new this.model({ name: 'Alice' })`). By default, the API classes you create will not have access to the context. You can either choose to add the data that your API class needs on a case-by-case basis as members of the class, or you can add the entire context as a member of the class if you wish. All you need to do is add the field(s) to the options argument of the constructor and call super passing in options. For example, if you put the logged-in user's ID on context as `context.currentUserId` and you want your Users class to have access to `currentUserId`:
If you want your data source to have access to the entire context at `this.context`, you need to create a `Context` class so the context can refer to itself as `this` in the constructor for the data source.
132
+
See [dataSources](https://www.apollographql.com/docs/apollo-server/migration/#datasources) for more information regarding how data sources changed from Apollo Server 3 to Apollo Server 4.
If you're passing a Mongoose model rather than a collection, Mongoose will be used for data fetching. All transformations defined on that model (virtuals, plugins, etc.) will be applied to your data before caching, just like you would expect it. If you're using reference fields, you might be interested in checking out [mongoose-autopopulate](https://www.npmjs.com/package/mongoose-autopopulate).
@@ -119,7 +188,8 @@ class Posts extends MongoDataSource {
@@ -150,11 +225,14 @@ class Users extends MongoDataSource {
150
225
151
226
updateUserName(userId, newName) {
152
227
this.deleteFromCacheById(userId)
153
-
returnthis.collection.updateOne({
154
-
_id: userId
155
-
}, {
156
-
$set: { name: newName }
157
-
})
228
+
returnthis.collection.updateOne(
229
+
{
230
+
_id: userId
231
+
},
232
+
{
233
+
$set: { name: newName }
234
+
}
235
+
)
158
236
}
159
237
}
160
238
@@ -173,7 +251,7 @@ Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the
173
251
174
252
### TypeScript
175
253
176
-
Since we are using a typed language, we want the provided methods to be correctly typed as well. This requires us to make the `MongoDataSource` class polymorphic. It requires 1-2 template arguments. The first argumentis the type of the document in our collection. The second argument is the type of context in our GraphQL server, which defaults to `any`. For example:
254
+
Since we are using a typed language, we want the provided methods to be correctly typed as well. This requires us to make the `MongoDataSource` class polymorphic. It requires 1 template argument, which is the type of the document in our collection. If you wish to add additional fields to your data source class, you can extend the typing on constructor options argument to include any fields that you need. For example:
0 commit comments