https://github.com/recodive/mongodatasource
An Apollo GraphQL Datasource using MongoDB.
https://github.com/recodive/mongodatasource
apollo mongodb nodejs typescript
Last synced: 10 months ago
JSON representation
An Apollo GraphQL Datasource using MongoDB.
- Host: GitHub
- URL: https://github.com/recodive/mongodatasource
- Owner: Recodive
- License: mpl-2.0
- Created: 2021-10-30T14:52:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T05:09:12.000Z (over 2 years ago)
- Last Synced: 2025-08-09T01:34:45.957Z (10 months ago)
- Topics: apollo, mongodb, nodejs, typescript
- Language: TypeScript
- Size: 75.2 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MongoDataSource
An Apollo GraphQL Datasource using MongoDB.
## Why
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.
This DataSource is currently in use by [PreMiD/API](https://github.com/PreMiD/API) at a large scale.
## Installation
```Shell
# NPM
npm i apollo-mongodb-datasource
# YARN
yarn add apollo-mongodb-datasource
```
## Usage
### TypeScript
index.ts
```TS
import { MongoClient } from 'mongodb'
import Users from './dataSources/Users'
const client = new MongoClient('mongodb://localhost:27017/test');
client.connect()
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
users: new Users(client.db().collection('users'))
})
})
```
Users.ts
```TS
import MongoDataSource from 'apollo-mongodb-datasource'
export default class Users extends MongoDataSource {
getUser(userId: string) {
return this.findOne({userId})
}
}
```