在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):entria/graphql-mongoose-loader开源软件地址(OpenSource Url):https://github.com/entria/graphql-mongoose-loader开源编程语言(OpenSource Language):TypeScript 91.2%开源软件介绍(OpenSource Introduction):GraphQL Mongoose LoaderInstall
Mongoose Dataloader BatchAdd batch to your GraphQL resolvers/loaders Define a mongoose schema for your model 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 import { mongooseLoader } from '@entria/graphql-mongoose-loader';
import UserModel from './User';
export const getLoader = () => new DataLoader(ids => mongooseLoader(UserModel, ids)); Connection from Mongoose CursorCreate a connection from mongoose cursor 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 AggregateCreate a connection from mongoose aggregate 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,
});
}; |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论