【问题标题】:how to send multiple data to mutation in apollo graphql如何在 apollo graphql 中将多个数据发送到突变
【发布时间】:2021-11-20 23:43:41
【问题描述】:

我想将多个数据(对象数组)从输入动态发送到 apollo graphql 突变,但还没有找到可行的方法。 现在我仍然像下面的代码示例一样手动编写它。

我制作的示例代码:

变异

mutation createData(
  $userId: Int!, 
  $Content: String, 
  $Title: String
) {
    createData(
      input: {
        content: $Content
        filterMetas: [
          // how to make this dynamic from user input?
          { categoryName: "Category Name 1", categoryId: "1", categoryType: INFO }
          { categoryName: "Category Name 2", categoryId: "2", categoryType: INFO }
        ]
        meta: { title: $Title }
      }
      userId: $userId
    ) {
      id
      name
}

在反应中调用突变

CreateData({
   variables: {
     Content: content,
     Title: title,
     // how to send multiple filtersMetas(array of objects) from here? instead of entering it manually
   },
   context: { clientName: API_CLIENT },
});

谢谢。

【问题讨论】:

  • 像任何其他变量一样,您甚至可以使用一个变量发送所有变量,整个$input

标签: reactjs graphql apollo-client


【解决方案1】:

好的,解决了。我将FilterMetas 更改为类似于graphql 服务器中的类型

mutation createData(
  $userId: Int!, 
  $Content: String, 
  $Title: String
  $FilterMetas: [FilterMetaInput!]
) {
    createData(
      input: {
        content: $Content
        filterMetas: $FilterMetas
      }
      userId: $userId
    ) {
      id
      name
}

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2020-10-20
    • 2020-02-01
    • 2019-12-18
    • 2021-03-24
    • 2017-02-07
    • 2020-04-24
    • 2020-09-22
    • 2017-08-25
    相关资源
    最近更新 更多