带有嵌套类型的 Graphql 突变?

Posted

技术标签:

【中文标题】带有嵌套类型的 Graphql 突变?【英文标题】:Graphql mutation with nested types? 【发布时间】:2020-12-09 17:48:49 【问题描述】:

我在使用 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,
          visible_order,
          bot_photo,
          responses: newResponses,
          bubbles: newBubbles,
        ,
      ,
    );
    //setShowEdit();
  ;

我收到此错误:

即使数据正确:

我想在我的突变中包含这两种类型,但我不知道如何。

【问题讨论】:

错误的有效载荷,双方括号 @xadm 谢谢你,你救了我 【参考方案1】:

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

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 中每个元素的附加数组

【讨论】:

以上是关于带有嵌套类型的 Graphql 突变?的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌套突变的 Graphql?

突变中的 GraphQL 嵌套数据

GraphQL 嵌套突变 + Sequelize

graphql 突变不返回嵌套字段

突变中的 GraphQL 嵌套节点 - 对象文字只能指定已知属性,并且类型中不存在“天”

在 Graphcool / GraphQL 中发布更新突变时如何“更新”数组/嵌套字段?