带有嵌套类型的Graphql突变?

问题描述

我在graphql输入类型方面遇到麻烦。 我正在使用与阿波罗的反应

我有这个工作突变:

addQuestion(
question: QuestionInput!
): Question!

具有以下类型:

type QuestionInput {
name: String!
account_id: Int!
chatbot_id: Int!
question_variants: [String!]!
answer: String!
}

但是这种突变会发生问题:

updateBranch(
id: Int!
branch: BranchInput!
): Branch!

具有以下类型:

type BranchInput {
lead_id: Int!
title: String!
visible_order: Int!
bot_photo: String!
responses: [ResponseInput!]!
bubbles: [BubbleInput!]!
}

和这些嵌套类型:

type ResponseInput {
branch_id: Int
owner: String!
message: String!
}

type BubbleInput {
branch_id: Int
name: String!
goto: String!
}

该变异在graphql操场上起作用

mutation {
  updateBranch(
    id: 2
    branch: {
      lead_id: 1
      title: "new title"
      visible_order: 1
      bot_photo: "photo"
      responses: [
        { message: "message 1",owner: "owner1" }
        { message: "message 2",owner: "owner 2" }
      ]
      bubbles: [{name: "bubble 1",goto: "link"}]
    }
  ) {
    id
    title
  }
}

当我在这样的代码中执行变异时:

 const handleEditBranche = async (
    lead_id: number,title: string,visible_order: number,bot_photo: string,responses: {}[],bubbles: {}[],id?: string
  ) => {
    const newResponses = responses.map((response: any) => [
      { message: response.message,owner: response.owner },]);
    const newBubbles = bubbles.map((bubble: any) => [
      { name: bubble.name,goto: bubble.goto },]);
    console.log(id,title,visible_order,bot_photo,newResponses,newBubbles);

    await updateBranche({
      variables: {
        id,branch: {
          title,lead_id,responses: newResponses,bubbles: newBubbles,},});
    //setShowEdit({});
  };

我收到此错误:

Error

即使数据正确:

mutation payload

我想在我的突变中包括这2种类型,但我不知道怎么做。

解决方法

问题在于构建有效负载的方式。

const newResponses = responses.map((response: any) => 
  { message: response.message,owner: response.owner },);
const newBubbles = bubbles.map((bubble: any) => 
  { name: bubble.name,goto: bubble.goto },);
console.log(id,title,visible_order,bot_photo,newResponses,newBubbles);

删除了newResponsesnewBubbles 中每个元素的附加数组

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...