1.0.22 • Published 4 years ago
@parameter1/dataloader-mongodb v1.0.22
MongoDB Dataloader
Provides a GraphQL dataloader for MongoDB with field projection support.
Installation
yarn add @parameter1/dataloader-mongodb
Usage
Dataloaders should only live per request. For example, using Express...
const express = require('express');
const MongoDBLoader = require('@parameter1/dataloader-mongodb');
const db = require('./your-mongo-client');
const app = express();
app.get('/', (req, res, next) => {
// get the user collection from mongo.
const collection = db.collection('users');
// create the loader during the request
const userLoader = new MongoDBLoader({ collection });
// return the user with an id of `1` and only project the `name` field.
userLoader.load({ id: 1, projection: { name: 1 } }).then((user) => {
res.send(user);
}).catch(next);
});
app.listen(2112);
When using with GraphQL, context is usually the best bet...
const { ApolloServer } = require('apollo-server-express');
const express = require('express');
const schema = require('./your-schema');
const db = require('./your-mongo-client');
const app = express();
const server = new ApolloServer({
context: () => {
const loaders = {
user: new MongoDBLoader({ collection: db.collection('users') });
}
// now `loaders.user` will be available in the resolver context
return { loaders };
},
schema,
});
server.applyMiddleware({ app, path });
app.listen(2112);
1.0.22
4 years ago
1.0.21
4 years ago
1.0.20
4 years ago
1.0.19
4 years ago
1.0.18
4 years ago
1.0.17
4 years ago
1.0.16
4 years ago
1.0.15
4 years ago
1.0.14
4 years ago
1.0.13
4 years ago
1.0.12
4 years ago
1.0.11
4 years ago
1.0.10
4 years ago
1.0.9
4 years ago
1.0.8
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago