1.0.22 • Published 3 years ago

@parameter1/dataloader-mongodb v1.0.22

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago