0

我不能完全改变对象列表,因为只有数组的最后一个元素会被改变。

如果我将每个元素 ({play_positions_id: ...}) 手动放入数组中,就像这里一样:

mutation CreateProfile {
  __typename
  create_profiles_item(data: {status: "draft", play_positions: [{play_positions_id: {id: "1"}}, {play_positions_id: {id: "2"}}]}) {
    id
    status
    play_positions {
     
      play_positions_id {
        abbreviation
        name
      }
    }
  }
}

输出:

{
  "data": {
    "__typename": "Mutation",
    "create_profiles_item": {
      "id": "1337",
      "status": "draft",
      "play_positions": [
        {
          "play_positions_id": {
            "id": "1",
            "abbreviation": "RWB",
            "name": "Right Wingback"
          }
        },
        {
         "play_positions_id": {
            "id": "2",
            "abbreviation": "CAM",
            "name": "Central Attacking Midfielder"
          }
        }
      ],
    }
  }
}

由于您可以添加许多这些元素,因此我定义了一个变量/参数,如下所示

mutation CreateProfile2($cpppi: [create_profiles_play_positions_input]) {
  __typename
  create_profiles_item(data: {status: "draft", play_positions: $cpppi}) {
    id
    status
    play_positions {
      
      play_positions_id {
        id
        abbreviation
        name
      }
    }
  }
}

上面的变量对象:

"cpppi": {    
    "play_positions_id": {
      "id": "1"
    },
    "play_positions_id": {
      "id": "2
    }
  }

输出:

{
  "data": {
    "__typename": "Mutation",
    "create_profiles_item": {
      "id": "1338",
      "play_positions": [
        {
        
          "play_positions_id": {
            "id": "2",
            "abbreviation": "CAM",
            "name": "Central Attacking Midfielder"
          }
        }
      ],
    }
  }
}

架构:

input create_profiles_input {
  id: ID
  status: String!
  play_positions: [create_profiles_play_positions_input]
}

input create_profiles_play_positions_input {
  id: ID
  play_positions_id: create_play_positions_input
}

input create_play_positions_input {
      id: ID 
      abbreviation: String
      name: String
}

在最后两个片段中,只有最后一个 id 为“2”的对象会被突变。我需要这些来使用我后端定义的输入类型。

4

1 回答 1

0

我想到了。我弄错了变量中的括号。这里的解决方案:

"cpppi": [
    {
      "play_positions_id": {
        "id": "1"
      }
    },
    {
      "play_positions_id": {
        "id": "2"
      }
    }
  ]
于 2022-02-10T21:11:20.913 回答