Skip to content

Commit

Permalink
Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Oct 17, 2019
1 parent aea6860 commit 204eee2
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 63 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi
- [Basic](#basic)
- [Batching](#batching)
- [Caching](#caching)
- [Mongoose](#mongoose)
- [API](#api)
- [findOneById](#findonebyid)
- [findManyByIds](#findmanybyids)
Expand All @@ -33,7 +32,7 @@ This package uses [DataLoader](https://github.com/graphql/dataloader) for batchi

### Basic

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

```js
import { MongoDataSource } from 'apollo-datasource-mongodb'
Expand All @@ -50,18 +49,18 @@ and:
```js
import Users from './data-sources/Users.js'

const users = db.collection('users')

const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
db: new Users({ users })
users: new Users(db.collection('users'))
// OR
// users: new Users(UserModel)
})
})
```

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`:
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`:

```js
class Users extends MongoDataSource {
Expand All @@ -88,26 +87,6 @@ class Users extends MongoDataSource {
}
```

### Mongoose

You can use mongoose the same way as with the native mongodb client

```js
import mongoose from 'mongoose'
import Users from './data-sources/Users.js'

const userSchema = new mongoose.Schema({ name: 'string'});
const UsersModel = mongoose.model('users', userSchema);

const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
db: new Users({ users: UsersModel })
})
})
```

### Batching

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

const users = db.collection('users')
const posts = db.collection('posts')

const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
users: new Users({ users }),
posts: new Posts({ posts })
users: new Users(db.collection('users')),
posts: new Posts(db.collection('posts'))
})
})
```
Expand All @@ -161,7 +137,7 @@ class Users extends MongoDataSource {

updateUserName(userId, newName) {
this.deleteFromCacheById(userId)
return this.users.updateOne({
return this.collection.updateOne({
_id: userId
}, {
$set: { name: newName }
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'node'
}
130 changes: 129 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-datasource-mongodb",
"version": "0.1.0",
"version": "0.2.0",
"description": "Apollo data source for MongoDB",
"main": "dist/index.js",
"scripts": {
Expand All @@ -27,6 +27,8 @@
"babel-jest": "^24.7.1",
"graphql": "^14.2.1",
"jest": "^24.7.1",
"mongodb": "^3.3.2",
"mongoose": "^5.7.4",
"prettier": "^1.16.4",
"waait": "^1.0.4"
},
Expand Down
Loading

0 comments on commit 204eee2

Please sign in to comment.