具有嵌套类型的 Graphql 突变?

Graphql mutation with nested types?

我在使用 graphql 输入类型时遇到问题。 我正在使用与 apollo

的反应

我有这个工作突变:

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,
          visible_order,
          bot_photo,
          responses: newResponses,
          bubbles: newBubbles,
        },
      },
    });
    //setShowEdit({});
  };

我收到此错误:

即使数据正确:

我想在我的突变中包含这 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

中每个元素的附加数组