npm i @entria/graphql-mongoose-loader --save
yarn add @entria/graphql-mongoose-loader
Add 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));
Create 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,
});
};
Create 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,
});
};
前端: Vue + Typescript + Graphql + Jwt auto-page-web 项目中使用 vue-typescript-admin-template 做二次改造,功能是 vue 可视化页面生成后台(前端前台) 后端: nodejs + mongoose + apollo + graphql + jwt page-node 前端源码分析: 这里主要介绍整体框架及 apollo
GraphQL Mongoose Cursor Pagination with Elasticsearch Support Adds support for Relay-like cursor pagination with Mongoose models/documents. This library also provides type-ahead (autocomplete) functio
graphql-compose-mongoose This is a plugin for graphql-compose, which derives GraphQLType from your mongoose model. Also derives bunch of internal GraphQL Types. Provide all CRUD resolvers, including g
Koa 2 + Passport + Mongoose + GraphQL Notice You want build a more flexible GraphQL schema, consider using this boilerplate instead: https://github.com/sibelius/graphql-dataloader-boilerplate graffiti
是否可以同时利用GraphQL和Mongoose? 到目前为止,我已经能够集成GraphQL和Mongoose来处理填充数据库,但是我很难理解这如何工作来检索数据,特别是具有嵌套引用的数据。 考虑这个模式: Bar模式本质上是相同的,只有一个“名称”字段。 是否可以运行GraphQL查询,用“bar”中的引用填充数据? 目前,我们正在使用GraphQL工具创建TypeDef、突变和查询,如下所示:
我正在试验graphql,并使用graphql瑜伽创建了一个简单的服务器。我的Mongoose产品模型查询我的数据库,两个解析器按预期返回数据。到目前为止,一切顺利,我很高兴这是多么容易。然而,我有一个问题。我正在尝试添加一种方法来对来自graphQL的结果进行分页。 1) 向查询类型添加限制参数。 2) 通过解析器中的参数访问参数 我可以在我的解析器中使用args.limit参数并使用它来改变M