简体   繁体   English

如何在graphql和mongodb中的一个突变中保存多行?

[英]How can I save multiple rows in one mutation in graphql and mongodb?

Here I want to add multiple thoughts in one mutation query. 在这里,我想在一个变异查询中添加多种想法。 How can I achieve this: 我该如何实现:

mutation{
  savethoughts(data: [{id:1,name:"a"},data: {id:2,name:"b"}]){
    id
  }
}

For save multiple records in database you need to follow every steps carefully 为了将多个记录保存在数据库中,您需要仔细执行每个步骤

For example 例如

mutation{
  setMultipleRecord(data: [{title: "first Title", name: "First"},
                           {title: "Second Title", name: "Second"}])
}

You must have schema type 您必须具有架构类型

  `
  .......
  input Record {
    title: String!
    name: String!
  }
  .....
  ` 

And add Mutation as given that 并按如下所示添加Mutation

  type Mutation/type RootMutation { {
    ......
    setMultipleRecord(data: [Record]): String!
  }

Here you can see Record is input type schema object where is data parameter holding variable which we are using to get data object. 在这里,您可以看到Record是输入类型的模式对象,其中是我们用来获取数据对象的数据参数保存变量。

you can give any name instead of data also change in the argument in mutation 您可以给任何名称而不是数据,也可以更改突变中的参数

Now in resolver function a 现在在解析器功能中

  Mutation: {
    setMultipleRecord: async(args, { data }) => {
      console.log(data)
      //Here data contain data which you passed which is
      // [{title: "first Title", name: "First"},
      //  {title: "Second Title", name: "Second"}]
      // Perform operation as your requirement
      // Please return String 
    }
  }

Now you can change name as par your requirement of object. 现在,您可以按对象的要求更改名称。

You made it successfully... 您成功做到了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM