Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/woovibr/graphql-mongoose-loader
GraphQL Mongoose Loader helpers
https://github.com/woovibr/graphql-mongoose-loader
entria graphql loader mongoose resolver
Last synced: 3 months ago
JSON representation
GraphQL Mongoose Loader helpers
- Host: GitHub
- URL: https://github.com/woovibr/graphql-mongoose-loader
- Owner: woovibr
- License: mit
- Created: 2017-06-22T10:50:06.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-06-16T09:57:32.000Z (8 months ago)
- Last Synced: 2024-11-01T08:32:56.305Z (3 months ago)
- Topics: entria, graphql, loader, mongoose, resolver
- Language: TypeScript
- Size: 1.45 MB
- Stars: 115
- Watchers: 16
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL Mongoose Loader [![CircleCI](https://circleci.com/gh/entria/graphql-mongoose-loader/tree/master.svg?style=svg)](https://circleci.com/gh/entria/graphql-mongoose-loader/tree/master)
## Install
```
npm i @entria/graphql-mongoose-loader --save
yarn add @entria/graphql-mongoose-loader
```## Mongoose Dataloader Batch
Add batch to your GraphQL resolvers/loaders
Define a mongoose schema for your model
```jsx
import mongoose from 'mongoose';const Schema = new mongoose.Schema(
{
name: {
type: String,
},
email: {
type: String,
required: true,
index: true,
},
password: {
type: String,
hidden: true,
},
},
{
collection: 'User',
},
);export default mongoose.model('User', Schema);
```Create a Dataloader for it
```jsx
import { mongooseLoader } from '@entria/graphql-mongoose-loader';
import UserModel from './User';export const getLoader = () => new DataLoader(ids => mongooseLoader(UserModel, ids));
```## Connection from Mongoose Cursor
Create a connection from mongoose cursor
```jsx
import { connectionFromMongoCursor } from '@entria/graphql-mongoose-loader';export const loadUsers = async (context: GraphQLContext, args: ConnectionArguments) => {
const where = args.search
? {
name: {
$regex: new RegExp(`^${args.search}`, 'ig'),
},
}
: {};
const users = UserModel.find(where, { _id: 1 }).sort({
createdAt: -1,
});return connectionFromMongoCursor({
cursor: users,
context,
args,
loader: load,
});
};
```## Connection from Mongoose Aggregate
Create a connection from mongoose aggregate
```jsx
import { connectionFromMongoAggregate } from '@entria/graphql-mongoose-loader';export const loadUsersThatHaveGroup = async (context: GraphQLContext, args: ConnectionArguments) => {
const aggregate = GroupModel.aggregate([
{
$lookup: {
from: 'User',
localField: 'users',
foreignField: '_id',
as: 'users',
},
},
{
// remove empty groups
$match: { users: { $exists: true, $ne: [] } },
},
{
// promote each user to a new document
$unwind: '$users',
},
{
$sort: {
_id: 1,
},
},
{
$replaceRoot: { newRoot: '$users' },
},
]);return connectionFromMongoAggregate({
aggregate,
context,
args,
loader: load,
});
};
```