1

在 GraphQL 中,我有两种类型,AuthorQuotes,如下所示:

  type Author {
    id: Int!
    name: String!
    last_name: String!
    quotes: [Quote!]!
  }

  type Quote {
    id: Int!
    author: Author! 
    quote: String!
  } 

在实现中,可以单独创建作者引用。
但我想在同一个请求中添加创建作者和多个引号的功能,如下所示:

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创建突变吗?

4

0 回答 0