[英]GraphQL relationship returning null
I'm learning graphql and working on a simple API with mongodb database. 我正在学习graphql并使用mongodb数据库开发一个简单的API。 I can't figure out why the relationship declared in my schema is not working : 我不知道为什么在我的模式中声明的关系不起作用:
type People {
id:ID!
firstName:String!
lastName:String!
email:String!
serviceId:String
apps:[String]
service:Service
}
type Service {
id:ID!
name:String!
location:String!
peoples:[People]
}
When I run this query: 当我运行此查询时:
query getLocationByPerson {
People {
firstName
lastName
service {
location
}
}
}
Here's what I get: 这是我得到的:
"People": [
{
"firstName": "John",
"lastName": "DOE",
"service": null
},
{
"firstName": "Jane",
"lastName": "DOE",
"service": null
}
]
Any idea of what I'm missing here? 对我在这里想念的东西有任何想法吗?
The issue lays in your resolvers: 问题在于您的解决者:
Based on the repo you linked your queries look like the following: 根据回购,您链接的查询如下所示:
const People = require('../database/people');
const Service = require('../database/service');
const queries = {
People: () => People.find({}),
...
Service: () => Service.find({}),
...
};
module.exports = queries;
The People schema looks like this: People模式如下所示:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const peopleSchema = new Schema({
Xid: { type: String },
firstName: { type: String },
lastName: { type: String },
email: { type: String },
apps: { type: Array },
serviceId: { type: String },
service: { type: Schema.Types.ObjectId, ref: 'service' }
},{ versionKey: false })
module.exports = mongoose.model('people', peopleSchema);
People.find()
will return only the service _id
though not the whole service object. People.find()
将仅返回服务_id
但不会返回整个服务对象。 That is why you get null
in the response. 这就是为什么您在响应中得到null
原因。
The GraphQL relationship you implemented in People has a Service Type
while you're getting back from the db only the service _id
. 在People中实现的GraphQL关系只有一个Service Type
_id
而从数据库中取回时,该Service Type
具有Service Type
。
You have 2 solutions: 您有2个解决方案:
A) You want to retrieve the Service object as well when you query for People. A)您在查询People时也要检索Service对象。 In this case you need to use the mongoose populate
function: People: () => People.find({}).populate('service'),
在这种情况下,您需要使用猫鼬populate
函数: People: () => People.find({}).populate('service'),
The above will provide People with the referenced Service object (not just the _id) 上面的代码将为People提供所引用的Service对象(而不仅仅是_id)
Because you're using id
instead of _id
in your schema the above is not enough and you need to use the following instead where you also create an id
field to return for each service 因为您在架构中使用的是id
而不是_id
,所以上面的方法是不够的,您需要使用以下内容来代替,其中您还创建了一个id
字段来为每个服务返回
People: async () => {
const people = await People.find({}).populate('service').exec()
return people.map(person => ({
...person._doc,
id: person._doc._id,
service: {
...person._doc.service._doc,
id: person._doc.service._doc._id,
},
}))
}, return people
}
The above is quite convulted. 以上是相当令人费解的。 I'd strongly suggest going with solution (B) 我强烈建议您使用解决方案(B)
Docs about populate(): https://mongoosejs.com/docs/populate.html 关于populate()的文档: https : //mongoosejs.com/docs/populate.html
B) User a type
resolver B)用户type
解析器
// Type.js
const Service = require('../database/service');
const types = {
People: {
// you're basically saying: In People get service field and return...
service: ({ service }) => Service.findById(service), // service in the deconstructed params is just an id coming from the db. This param comes from the `parent` that is People
},
Service: {
id: ({_id}) => _id, // because you're using id in your schema
},
};
module.exports = queries;
A note on the implementation of this option: 关于此选项实施的说明:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.