我有以下架构:
students.graphql.schema.js
export default [
`
type StudentsWithPagination {
total: Int
items: [Students]
}
type Students {
_id: String!
name: String
address: Addresses
}
`,
];
addresses.graphql.schema.js
export default [
`
type AddressesWithPagination {
total: Int
items: [Addresses]
}
type Addresses {
_id: String!
title: String
}
`,
];
我通过运行feathers generate service
students.service.js 和 addresses.services.js 创建了两个服务。
当我用title
搜索地址时,得到结果。但是,当我按_id
搜索时,我得到的是空值。像这样:
const studentsResolvers = {
Students: {
address: student => {
const query = {
_id: student.address
}
return Addresses.find({ query }).then(result => {
console.log(result)
})
}
}
}
上面的代码产生null
,尽管student.address
返回正确的address._id
。即使我用正确的null
硬编码student.address
,我仍然得到address._id
除非我按地址标题搜索,否则上面的代码将返回null
。像这样:
const query = {
title: 'my-location'
}
_id
的类型为String
,而不是ObjectID
。
我在做什么错了?
答案 0 :(得分:0)
与documented in the feathers-mongodb adapter一样,由于MongoDB本身(与Mongoose不同)不具有架构,因此必须将所有查询参数手动转换为数据库in a hook中的类型。该示例可以相应地适用于$in
查询:
const ObjectID = require('mongodb')。ObjectID;
app.service('users').hooks({
before: {
find(context) {
const { query = {} } = context.params;
if(query._id) {
query._id = new ObjectID(query._id);
}
if(query.age !== undefined) {
query.age = parseInt(query.age, 10);
}
context.params.query = query;
return Promise.resolve(context);
}
}
});