apollo-mongodb-datasource
v1.3.3
Published
An Apollo GraphQL Datasource using MongoDB.
Downloads
20
Readme
MongoDataSource
An Apollo GraphQL Datasource using MongoDB.
Why
While there already is a somewhat good MongoDB DataSource package available (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), 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 at a large scale.
Installation
# NPM
npm i apollo-mongodb-datasource
# YARN
yarn add apollo-mongodb-datasource
Usage
TypeScript
index.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
import MongoDataSource from 'apollo-mongodb-datasource'
export default class Users extends MongoDataSource {
getUser(userId: string) {
return this.findOne({userId})
}
}