node.js - 如何在没有 _id 引用的情况下获取 GraphqQL 列表中模型的填充值?

标签 node.js mongodb graphql mern express-graphql

我有一个用户模型,其中包含一些字段并引用了“技能”模型。

模型如下图所示

1.Users.js(用户模型)

 const mongoose = require('mongoose');
 const Schema = mongoose.Schema;

 const UsersSchema = new Scheam({
     name: { type : String },
     email: { type : String },
     address: { type : String },

     Skills: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Skills' }],
 })

 module.exports = mongoose.model('Users', UsersSchema);

2.技能模型

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const SkillsSchema = new Schema({
   name : { type: String },
});

module.exports = mongoose.model('Skills', SkillsSchema);

我正在使用技能选择下拉菜单创建新用户,该菜单在用户模型中存储技能的 ID。

并通过在 Node js 应用程序中像这样填充用户模型来获得技能。

  export function getUser(req, res) {
     console.log('getUser');
     console.log('req.body',req.body);
     Users.findOne({_id: req.body.user_id}).populate('skills').exec(function(err, usr_doc){ 
       if(err) {
          res.json({
             status: 401,
             message: 'something went wrong!',
             err: err,
          });
       } else if (!err && usr_doc != null) {
           res.json({
               status: 200,
               message: 'User details found!',
               data: {_id: usr_doc._id, skills: usr_doc.skills,name: usr_doc.name,token: usr_doc.token},
           })
       }
     })//Users.findOne end
  }

上面的代码对于普通的 rest api 工作正常。

在这里,我尝试使用 Nodejs 中的 express-graphql 的 GraphQL 服务器创建相同的 api。

代码如下所示。

3.Schema.js

 const graphql = require('graphql');
 const Users = require('../models/Users');
 const Skills = require('../models/Skills');

 const { 
    GraphQLObjectType,
    GraphQLInt,
    GraphQLFloat,
    GraphQLString, 
    GraphQLSchema,
    GraphQLID,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLObject
 } = graphql;

 const UsersType = new GraphQLObjectType ({
       name: 'Users',
       fields: () => ({
            id: {type: GraphQLID},
            name: { type : GraphQLString },
            email: { type : GraphQLString },
            **skills: {
                type: new GraphQLList(SkillsType),
                resolve(parent, args){
                  //Here I need to know how to get the skills name of the users
                }
            }**
       })

   const SkillsType = new GraphQLObjectType({
       name: 'Skills',
       fields: () => ({
          name: { type : GraphQLString },
       })
   })    

目前我没有在技能模型中使用任何引用id,只有名称。我想获取用户的技能名称。

如果我使用以下内容,我将获得所有技能名称,而不是用户特定或填充的技能名称。

   skills : {
     type: new GraphQLList(SkillsType),
        resolve(parent, args){
            return Skills.find({})
        } 

请对此提出任何建议。

如果您需要,我可以提供更多信息。

最佳答案

要获取用户的技能,而不是返回技能集合中存在的所有文档的Skills.find({}),请在其中添加一个条件parent 对象。

parent 对象,它包含解析器在此处父字段上的结果。所以技能,一个子解析器,可以写成:

 skills: {
     type: new GraphQLList(SkillsType),
     resolve(parent, args) {
         return await Skills.find({"_id": {$in: parent.skills} }) }
 } 

关于node.js - 如何在没有 _id 引用的情况下获取 GraphqQL 列表中模型的填充值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593881/

相关文章:

node.js - 如何使用带有 Dynamo 的单个 UpdateExpression 进行 SET 和 DELETE

node.js - Handlebars : query and sum

mongodb - 无法使用 mongorestore 命令导入 mongodump

javascript - Node.js Stream,是在这段代码中工作的 Drain

node.js - 如何使用快速验证器验证字符串数组?

c# - 在 asp.net core 3.1 中使用最新的 GraphQL 库时,无法解析 IDependencyResolver 和 ResolveFieldContext

javascript - 如何使用 Relay QueryRenderer 属性更新状态

reactjs - 在 Gatsby 上,如何使用 prop 进行 GraphQL 查询?

javascript - 如何检查关键字是否存在而不管大小写?

javascript - React 博客网站的管理仪表板