问题描述
我在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({});
};
我想在我的突变中包括这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);
删除了newResponses
和newBubbles
中每个元素的附加数组