【发布时间】:2017-12-13 00:28:44
【问题描述】:
我开始使用 NodeJS 和 Mongoose 构建一个 Graphql 服务以与已经投入生产的 REST api 一起使用,但是我在寻找将我的 mongoose 模式转换为 GraphQL 类型的方法时遇到了一些麻烦。
我有以下架构:
var userSchema = new mongoose.Schema({
name: {type: String, required: true},
email: {type: String},
imageURL: {type: String, default:null},
providerId: {type: String, required: true},
token: {type: String, required: true},
history: [{
orders:[{type: mongoose.Types.ObjectId, ref:'Order'}],
restaurant: {type: mongoose.Types.ObjectId, ref: 'Restaurant'}
}]
}, { timestamps: true});
所以,基本上,问题在于history 字段。该字段是一个包含 2 个子字段的对象列表:orders(引用另一个 mongoose 架构的对象列表)和 restaurant(引用餐厅的 mongoose 架构)。
我的主要问题是:在为“用户”编写相应的 GraphQL 类型时,有没有办法创建子字段,就像我在 history 字段上使用猫鼬所做的那样,或者我必须创建第二个类型然后引用这种类型(知道应用程序不需要在 mongoose 中创建另一个模式,而只能在 GraphQL 中创建)。
【问题讨论】:
标签: mongoose graphql graphql-js