繁体   English   中英

如何在Graphql nodejs中解析用于查询的mongodb嵌入式文档数组

[英]How to resolve mongodb embedded document array of object for query in Graphql nodejs

我是 Graphql 的新手,我正在尝试在学习它的同时构建一个应用程序。 实际上我正在尝试编写一个解析器来解析以下模式的查询。 请告诉我如何解析引用的对象驻留在 feedformula 的数组中:[feeditemSchema] 如何从farmintake 获取相应的记录用于喂养。 我不知道我向您解释我的问题有多清楚,但我相信当您看到架构的结构时,您会理解我的问题。 请帮助我无法找到。 当我如下查询时,它可以工作。

query{
feeds{
 _id
feedname
feedtime
feedformula{
  feeding{
    _id
    
  }
  qty
  mesunit
  description
  remarks
 }creator{
  email
 }
 }
 } 

但是当我使用下面的查询时,它会引发错误:

query{
feeds{
_id
feedname
feedtime
feedformula{
  feeding{
    _id
    name
  }
  qty
  mesunit
  description
  remarks
  }creator{
  email
  }
 }
 }


   {
   "errors": [
   {
  "message": "Cannot return null for non-nullable field 
   Farmintake.name.",
  "locations": [
    {
      "line": 81,
      "column": 8
    }
    ],
   "path": [
    "feeds",
    0,
    "feedformula",
    0,
    "feeding",
    "name"
   ]
  },
  {
  "message": "Cannot return null for non-nullable field 
  Farmintake.name.",
  "locations": [
    {
      "line": 81,
      "column": 8
    }
  ],
  "path": [
    "feeds",
    0,
    "feedformula",
    1,
    "feeding",
    "name"
  ]
  },
  {
  "message": "Cannot return null for non-nullable field 
  Farmintake.name.",
  "locations": [
    {
      "line": 81,
      "column": 8
    }
  ],
  "path": [
    "feeds",
    0,
    "feedformula",
    2,
    "feeding",
    "name"
  ]
  }
  ],
  "data": {
  "feeds": [
   {
    "_id": "62b417a91797e5053c2d58a3",
    "feedname": "Dana",
    "feedtime": "Evening",
    "feedformula": [
      null,
      null,
      null
    ],
    "creator": {
      "email": "auzrimfa@gmail.com"
    }
  }
 ]
 }
}

遵循架构:

const feeditemSchema = new Schema({

feeding: {type: Schema.Types.ObjectId,ref: 'Farmintake'},
mesunit: {type: String},
qty: { type: Number },
description:{ type: String },
remarks:{ type: String }

});

const feedSchema = new Schema({
feedname:{ type: String },
feedtime:{ type: String },
feedformula:[ feeditemSchema ],
description:{ type: String },
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true });

const farmintakeSchema = new Schema({
name:{type: String,required: true},
itmtype:{type: String,required: true},
madein:{type: String,required: true},
mesunit:{type: String},
usage:{type: String},
remarks:{type: String},

creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true}
);
module.exports = mongoose.model('Feed', feedSchema );
module.exports = mongoose.model('Farmintake',farmintakeSchema);

我尝试了以下解析器:

const transformFeed = feed =>{


return {
    ...feed._doc,
    _id: feed.id,
    feeding: farmintake.bind(this, feed.feedformula.feeding),
    creator: user.bind(this,feed.creator)
    
 };

};
const farmintake = async farmintakeId => {
try{
    const farmintake = await Farmintake.findById(farmintakeId);
   
    return {
        ...farmintake._doc,
        _id:farmintake.id
        
    };

}catch (err) {
    throw err;
}
};

我没有等待任何回复,而是自己挣扎并找到了解决方案,以下是我的解决方案,它对我有用,请专家反馈以提出任何改进建议。 我的 Graphql 查询现在如下所示。

query{
 feeds {
  id
  feedname
  feedtime
  feeditems {
    farmintakes {
      id
      name
      remarks
    }
    id
    qty
    mesunit
    description
    remarks
  }
}
}

我想要的查询结果:

{
"data": {
  "feeds": [
    {
      "id": "62b417a91797e5053c2d58a3",
      "feedname": "Dana",
      "feedtime": "Evening",
      "feeditems": [
        {
          "farmintakes": [
            {
              "id": "62323fd1109749e4a4f49a02",
              "name": "Green Maiz Leaf (Makai Chara)",
              "remarks": "Green sliced maiz for evening feed "
            }
          ],
          "id": "62b418ad1797e5053c2d58b2",
          "qty": 6,
          "mesunit": "Kg",
          "description": "Testing Goat Feed",
          "remarks": "1st Goat Feed"
        },
        {
          "farmintakes": [
            {
              "id": "62323f06109749e4a4f499ff",
              "name": "Millet (Jawar)",
              "remarks": "Mix with morning feed of Goat"
            }
          ],
          "id": "62b4183b1797e5053c2d58ad",
          "qty": 3.5,
          "mesunit": "Kg",
          "description": "Testing Goat Feed",
          "remarks": "1st Goat Feed"
        },
        {
          "farmintakes": [
            {
              "id": "62323e49109749e4a4f499fc",
              "name": "Barley (Jau)",
              "remarks": "Mix with morning feed of Goat"
            }
          ],
          "id": "62b417d11797e5053c2d58a8",
          "qty": 2.5,
          "mesunit": "Kg",
          "description": "Testing Goat Feed",
          "remarks": "1st Goat Feed"
        }
      ]
    }
  ]
}
}

更改后的 Schema 如下所示:

const feeditemSchema = new Schema({
feedId : {type: Schema.Types.ObjectId,ref: 'Feed'},
farmintakeId : {type: Schema.Types.ObjectId,ref: 'Farmintake'},
mesunit: {type: String},
qty: { type: Number },
description:{ type: String },
remarks:{ type: String }

});

const feedSchema = new Schema({
feedname:{ type: String },
feedtime:{ type: String },
feeditems:[ {type: Schema.Types.ObjectId,ref: 'Feeditem'}],
description:{ type: String },
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true });

const farmintakeSchema = new Schema({
name:{type: String,required: true},
itmtype:{type: String,required: true},
madein:{type: String,required: true},
mesunit:{type: String},
usage:{type: String},
remarks:{type: String},

creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true}
);
module.exports = mongoose.model('Feed', feedSchema );
module.exports = mongoose.model('Feeditem',feeditemSchema);
module.exports = mongoose.model('Farmintake',farmintakeSchema);

类型定义:

type Feed {
  id: ID!
  feedname: String!
  feedtime: String!
  description: String
  feeditems:[Feeditem!]!
  
 }

 type Farmintake {
  id: ID!
  name: String!
  itmtype: String
  madin: String
  mesunit: String
  usage: String
  remarks: String
 }

 type Feeditem{
  id: ID!
  mesunit: String
  qty: Float
  description: String
  remarks: String
  farmintakes:[Farmintake!]!
 }

最后是解析器:

Feed:{
      feeditems:(parent,args,context) => {
      const { Fid } = parent.id;
      return feeditems.filter((feeditem) => feeditem.feedid === Fid );
  
     },
   },
Feeditem:{
  farmintakes:(parent,args,context) => {
    const { Fiid } = parent.farmintakeId;
    return farmintakes.filter(( farmintake ) => farmintake.id === 
 parent.farmintakeId );
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM