Мутация Graphql с вложенными типами?

#reactjs #graphql #react-apollo #apollo-client

#reactjs #graphql #react-apollo #apollo-клиент

Вопрос:

У меня проблемы с типами ввода graphql. Я использую react с 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 playground:

 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 типа в свою мутацию, но я не знаю как.

Комментарии:

1. плохая полезная нагрузка, двойные квадратные скобки

2. @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