如何解析graphQl nodejs中查询的mongoDB嵌入式文档数组

发布于 2025-02-10 21:34:05 字数 3067 浏览 1 评论 0原文

我是GraphQL的新手,并且在学习时正在尝试构建应用程序。实际上,我正在尝试编写一个解析器来解决以下模式的查询。 请告诉我如何解决参考物体的源泉:[fefitemschema] 如何获得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": "[email protected]"
    }
  }
 ]
 }
}

遵循模式:

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;
}
};

I am new to Graphql, and I am trying to build an application while I am learning it. Actually I am trying to write a resolver to resolve the query on following schema.
Please tell me how could I resolve the referenced object resides in the array of feedformula: [feeditemSchema]
How to get the corresponding record from farmintake for feeding.
I don't know how clearly I explained my problem to you but i am sure that when you see the structure of schema you will understand my problem. Please help a I am unable to find.
When I query as below it workes.

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

But when I use below query it raise an error:

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": "[email protected]"
    }
  }
 ]
 }
}

Following in Schema:

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);

I tried following Resolver:

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;
}
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2025-02-17 21:34:05

我没有等待任何答复,而是我自己挣扎并找到了解决方案,在下面是我的解决方案,它对我有用,专家反馈要求提出任何改进。我的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"
        }
      ]
    }
  ]
}
}

在更改模式之后,看起来像:

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);

Typedefs:

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 );
    }
  }

Instead of waiting for any reply, I struggled and found the solution by myself, below is my solution and it worked for me, experts feedbacks are requested to suggest for any improvement. My Graphql query is working now as below.

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

Query Result as I wanted:

{
"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"
        }
      ]
    }
  ]
}
}

After changes the Schema looking like as:

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);

TypeDefs:

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!]!
 }

Finally the Resolver:

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 );
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文