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
+
MongoDB[data source](https://www.apollographql.com/docs/apollo-server/data/fetching-data) for Apollo Server 4
4
4
5
5
```
6
-
npm i apollo-datasource-mongodb
6
+
npm i apollo-mongo-datasource
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 for the following methods:
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/performance/cache-backends#configuring-external-caching)). It does this for the following methods:
10
10
11
11
-[`findOneById(id, options)`](#findonebyid)
12
12
-[`findManyByIds(ids, options)`](#findmanybyids)
13
13
-[`findByFields(fields, options)`](#findbyfields)
14
14
15
-
16
15
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
17
16
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
17
+
18
18
**Contents:**
19
19
20
20
-[Usage](#usage)
@@ -31,7 +31,6 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
31
31
32
32
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
33
33
34
-
35
34
## Usage
36
35
37
36
### Basic
@@ -41,7 +40,7 @@ The basic setup is subclassing `MongoDataSource`, passing your collection or Mon
// users: new Users({ modelOrCollection: UserModel })
75
+
}
76
+
}),
71
77
})
72
78
```
73
79
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`:
80
+
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' })`). In Apollo Server 3, the context was automatically handled by the abstract DataSource class from apollo-datasource. This package has been deprecated, so the DataSource class has been removed from this package, as well as the initialize method. 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 as private 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 to the options argument of the constructor and call super. Then, 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`:
If you want your data source to have access to the entire context, you need to create a Context class so the context can refer to itself as this in the constructor for the data source.
118
+
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.
@@ -150,11 +187,14 @@ class Users extends MongoDataSource {
150
187
151
188
updateUserName(userId, newName) {
152
189
this.deleteFromCacheById(userId)
153
-
returnthis.collection.updateOne({
154
-
_id: userId
155
-
}, {
156
-
$set: { name: newName }
157
-
})
190
+
returnthis.collection.updateOne(
191
+
{
192
+
_id: userId
193
+
},
194
+
{
195
+
$set: { name: newName }
196
+
}
197
+
)
158
198
}
159
199
}
160
200
@@ -173,12 +213,77 @@ Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the
173
213
174
214
### TypeScript
175
215
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 argument is 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:
216
+
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 argument is the type of the document in our collection. The second argument is the type of context in our GraphQL server, which defaults to `any`. We can choose to either pass the necessary data from context to the data source by field, or give the data source class access to the entire context. For example:
0 commit comments