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
* Removed apollo datasource and initialize method from MongoDataSource
* Added type for constructor options argument to include modelOrCollection and optionally a cache
* Updated the tests for datasource to accomodate the recent changes
* Removed deprecated packages and installed new cache package, updated package info
* Added two tests to ensure that you can add a context to the constructor of the api class you extend MongoDataSource to
* Updated README and package version
* Fixed some mistakes and missing info in the README
* Minor fixes to the README
* Final changes
* Final commit
* Fixed typing issue
* Updated README and some package.json info
* Removed npm version comment
* Fixed typo in readme
* Fixed module name
* Added previous README and added note on versioning to both
* Fixed README links
* Updated mongoose from v5 to v7 and updated mongodb from v3 to v4
* Updated bson to 5.4.0 and mongodb to 5.7.0
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
+
Note: This README applies to the current version 0.6.0 and is meant to be paired with Apollo Server 4.
6
+
See the old [README](README.old.md) for versions 0.5.4 and below, if you are using Apollo Server 3.
7
+
8
+
**Installation**
5
9
```
6
10
npm i apollo-datasource-mongodb
7
11
```
8
12
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:
13
+
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
14
11
15
-[`findOneById(id, options)`](#findonebyid)
12
16
-[`findManyByIds(ids, options)`](#findmanybyids)
13
17
-[`findByFields(fields, options)`](#findbyfields)
14
18
15
-
16
19
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
17
20
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
21
+
18
22
**Contents:**
19
23
20
24
-[Usage](#usage)
@@ -31,7 +35,6 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
31
35
32
36
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
// users: new Users({ modelOrCollection: UserModel })
79
+
}
80
+
}),
71
81
})
72
82
```
73
83
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`:
84
+
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.
126
+
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 +182,8 @@ class Posts extends MongoDataSource {
@@ -150,11 +219,14 @@ class Users extends MongoDataSource {
150
219
151
220
updateUserName(userId, newName) {
152
221
this.deleteFromCacheById(userId)
153
-
returnthis.collection.updateOne({
154
-
_id: userId
155
-
}, {
156
-
$set: { name: newName }
157
-
})
222
+
returnthis.collection.updateOne(
223
+
{
224
+
_id: userId
225
+
},
226
+
{
227
+
$set: { name: newName }
228
+
}
229
+
)
158
230
}
159
231
}
160
232
@@ -173,7 +245,7 @@ Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the
173
245
174
246
### TypeScript
175
247
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:
248
+
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