带有嵌套类型的 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);
删除了newResponses
和newBubbles
中每个元素的附加数组
【讨论】:
以上是关于带有嵌套类型的 Graphql 突变?的主要内容,如果未能解决你的问题,请参考以下文章