在GraphQL中,我有两种类型,分别为 Author 和 Quotes ,如下:
type Author {
id: Int!
name: String!
last_name: String!
quotes: [Quote!]!
}
type Quote {
id: Int!
author: Author!
quote: String!
}
在实现中,可以分别创建 Author 和 Quote 。
但我想添加功能以在同一请求中创建作者和多个引号,如下所示:
mutation{
createAuthor(author:{
name:"Kent",
last_name:"Beck",
quotes:[
{
quote: "I'm not a great programmer; I'm just a good programmer with great habits."
},
{
quote: "Do The Simplest Thing That Could Possibly Work"
}
]
}) {
id
name
quotes{
quote
}
}
}
如果客户要如上所示合并创建,最完美的方法是什么?
作者创建 的当前实现如下:
resolve (source, args) {
return models.author.build({
name: args.author.name,
last_name: args.author.last_name
}).save().then(function(newAuthor) {
const quotes = args.author.quotes || [];
quotes.forEach((quote) => {
models.quote.create({
author_id: newAuthor.id,
quote: quote.quote,
});
});
return models.author.findById(newAuthor.id);
});
}
我可以以某种方式自动调用 Quotes 创建变量吗?