Skip to content

Commit 938e875

Browse files
authored
🗒️ Have something to read
1 parent 9e385db commit 938e875

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# MongoDataSource
2+
An Apollo GraphQL Datasource using MongoDB.
3+
4+
## Why
5+
While there already is a somewhat good MongoDB DataSource package available ([apollo-datasource-mongodb](https://github.com/GraphQLGuide/apollo-datasource-mongodb)), it still lacks some features / functions that we needed in our projects, while it doesn't *yet* support the main feature of the existing package ([Batching](https://github.com/GraphQLGuide/apollo-datasource-mongodb#batching)), it fixes other small issues like the prevention of spamming MongoDB with requests when there's already a query running for it.
6+
7+
This DataSource is currently in use by [PreMiD/API](https://github.com/PreMiD/API) at a large scale.
8+
9+
## Installation
10+
```Shell
11+
# NPM
12+
npm i @recodive/mongodbdatasource
13+
14+
# YARN
15+
yarn add @recodive/mongodbdatasource
16+
```
17+
18+
## Usage
19+
### TypeScript
20+
index.ts
21+
```TS
22+
import { MongoClient } from 'mongodb'
23+
24+
import Users from './dataSources/Users'
25+
26+
const client = new MongoClient('mongodb://localhost:27017/test');
27+
client.connect()
28+
29+
const server = new ApolloServer({
30+
typeDefs,
31+
resolvers,
32+
dataSources: () => ({
33+
users: new Users(client.db().collection('users'))
34+
})
35+
})
36+
```
37+
38+
Users.ts
39+
```TS
40+
import MongoDataSource from '@Recodive/mongodatasource'
41+
42+
export default class Users extends MongoDataSource {
43+
getUser(userId: string) {
44+
return this.findOne({userId})
45+
}
46+
}
47+
```

0 commit comments

Comments
 (0)