Skip to content

Commit 204eee2

Browse files
committed
Release 0.2.0
1 parent aea6860 commit 204eee2

8 files changed

+251
-63
lines changed

README.md

+8-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
2020
- [Basic](#basic)
2121
- [Batching](#batching)
2222
- [Caching](#caching)
23-
- [Mongoose](#mongoose)
2423
- [API](#api)
2524
- [findOneById](#findonebyid)
2625
- [findManyByIds](#findmanybyids)
@@ -33,7 +32,7 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
3332

3433
### Basic
3534

36-
The basic setup is subclassing `MongoDataSource`, passing your collection to the constructor, and using the [API methods](#API):
35+
The basic setup is subclassing `MongoDataSource`, passing your collection or Mongoose model to the constructor, and using the [API methods](#API):
3736

3837
```js
3938
import { MongoDataSource } from 'apollo-datasource-mongodb'
@@ -50,18 +49,18 @@ and:
5049
```js
5150
import Users from './data-sources/Users.js'
5251

53-
const users = db.collection('users')
54-
5552
const server = new ApolloServer({
5653
typeDefs,
5754
resolvers,
5855
dataSources: () => ({
59-
db: new Users({ users })
56+
users: new Users(db.collection('users'))
57+
// OR
58+
// users: new Users(UserModel)
6059
})
6160
})
6261
```
6362

64-
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`:
63+
Inside the data source, the collection is available at `this.collection` (e.g. `this.collection.update({_id: 'foo, { $set: { name: 'me' }}})`). The model (if applicable) 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`:
6564

6665
```js
6766
class Users extends MongoDataSource {
@@ -88,26 +87,6 @@ class Users extends MongoDataSource {
8887
}
8988
```
9089

91-
### Mongoose
92-
93-
You can use mongoose the same way as with the native mongodb client
94-
95-
```js
96-
import mongoose from 'mongoose'
97-
import Users from './data-sources/Users.js'
98-
99-
const userSchema = new mongoose.Schema({ name: 'string'});
100-
const UsersModel = mongoose.model('users', userSchema);
101-
102-
const server = new ApolloServer({
103-
typeDefs,
104-
resolvers,
105-
dataSources: () => ({
106-
db: new Users({ users: UsersModel })
107-
})
108-
})
109-
```
110-
11190
### Batching
11291

11392
This is the main feature, and is always enabled. Here's a full example:
@@ -134,15 +113,12 @@ const resolvers = {
134113
}
135114
}
136115

137-
const users = db.collection('users')
138-
const posts = db.collection('posts')
139-
140116
const server = new ApolloServer({
141117
typeDefs,
142118
resolvers,
143119
dataSources: () => ({
144-
users: new Users({ users }),
145-
posts: new Posts({ posts })
120+
users: new Users(db.collection('users')),
121+
posts: new Posts(db.collection('posts'))
146122
})
147123
})
148124
```
@@ -161,7 +137,7 @@ class Users extends MongoDataSource {
161137

162138
updateUserName(userId, newName) {
163139
this.deleteFromCacheById(userId)
164-
return this.users.updateOne({
140+
return this.collection.updateOne({
165141
_id: userId
166142
}, {
167143
$set: { name: newName }

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: 'node'
3+
}

package-lock.json

+129-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apollo-datasource-mongodb",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Apollo data source for MongoDB",
55
"main": "dist/index.js",
66
"scripts": {
@@ -27,6 +27,8 @@
2727
"babel-jest": "^24.7.1",
2828
"graphql": "^14.2.1",
2929
"jest": "^24.7.1",
30+
"mongodb": "^3.3.2",
31+
"mongoose": "^5.7.4",
3032
"prettier": "^1.16.4",
3133
"waait": "^1.0.4"
3234
},

0 commit comments

Comments
 (0)