Node.js 如何在graphql中从mongo填充相关集合?
如何使用带有mongoose的graphql查询具有Node.js 如何在graphql中从mongo填充相关集合?,node.js,mongodb,mongoose,graphql,Node.js,Mongodb,Mongoose,Graphql,如何使用带有mongoose的graphql查询具有UserType的User对象 我查过了,但不知道如何处理不同的收藏品 export const UserType = mongoose.model('User-Type', new mongoose.Schema({ typeName: { type: String } })); export const User = mongoose.model('User', new mongoose.Schema({ name: {
UserType
的User
对象
我查过了,但不知道如何处理不同的收藏品
export const UserType = mongoose.model('User-Type', new mongoose.Schema({
typeName: { type: String }
}));
export const User = mongoose.model('User', new mongoose.Schema({
name: { type: String },
type: { type: mongoose.Schema.Types.ObjectId, ref: 'User-Type' },
})
);
const Users = new GraphQLObjectType({
name: 'Users',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
type: { type: /* HOW TO CONNECT TO USER TYPE? */ },
}),
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({});
},
},
});
export const Schema = new GraphQLSchema({
query: RootQuery,
});
在mongoose中,要使用UserType
解析User
,我需要:
const users = await User.find({})
.populate({ path: 'type', model: 'User-Type' })
更新
这个问题不是重复的。为什么?我想从User
查询那些字段id
,name
。根据以下代码,这项功能运行良好:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({});
},
},
});
但是当使用类型
字段查询时,我应该执行以下操作:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({}).populate({ path: 'type', model: 'User-Type' });
},
},
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
onlyUsers: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({});
},
},
UsersWithType: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({}).populate({ path: 'type', model: 'User-Type' });
},
},
});
graphql似乎有很大的错误,因为如果我只需要id,那么命名mongoose也需要类型(return wait User.find({})。**填充**({path:'type',model:'User type'});
),我没有要求
因此,我认为我应该:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({}).populate({ path: 'type', model: 'User-Type' });
},
},
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
onlyUsers: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({});
},
},
UsersWithType: {
type: new GraphQLList(Users),
async resolve(parent, args) {
return await User.find({}).populate({ path: 'type', model: 'User-Type' });
},
},
});
这是如何在图形ql
中进行操作的
我想查询用户并从数据库中只获取我要求的字段
如果graphql不这样做,那就是使用它的能力?我还可以使用lodash从对象中获取所需的字段
.get(obj,'somefield.anothersomefield')
这实际上是.populate('type')
,因为这就是字段的名称。这并没有什么“特定于graphql的”,因为它仍然在使用mongoose methods.corrent。我更新我的问题。问题还没解决,是吗?你试过了吗?我刚刚告诉过你这是完全一样的。所以,不是return wait User.find({})
我需要将其更改为return wait User.find({}).populate('type')
?这就是使用mongoose populate的方式。我只能用很多种方式说它是一样的。你的问题是你最初使用了错误的论点<代码>填充()接受“填充路径”作为单个字符串参数。可以选择像{path:'type',model:'User type}
这样的对象,但仅当您确实需要这些选项时。底线是,.populate(“UserType”)
,因为您最初提出的问题是参数不正确。