【问题标题】:Error when I try to pass variable to apollo (GraphQl) mutation当我尝试将变量传递给阿波罗(GraphQl)突变时出错
【发布时间】:2020-09-21 14:26:00
【问题描述】:

我在我的 react 应用中使用了 apollo 突变。一切正常,直到我尝试将变量传递给这个突变。我的代码:

const DELETE_WORD = gql`
  mutation($eng_word: String!) {
    deleteWord(eng_word: $engWord) {
      eng_word
    }
  }
`;
const deleteWord = engWord => {
  deleteWordHook({ variables: { engWord } }).then(closeModal());
  refetch();
};

在调用 deleteWord 之后,我收到了错误,它没有提供任何有价值的反馈。

这是工作代码(不传递变量):

const DELETE_WORD = gql`
    mutation {
      deleteWord(eng_word: "apple") {
        eng_word
      }
    }
  `;
const deleteWord = engWord => {
    deleteWordHook().then(closeModal());
    refetch();
  };

【问题讨论】:

  • $eng_word vs $engWord - 在两个地方都使用第二个

标签: reactjs graphql react-apollo


【解决方案1】:

因为您在调用突变时将 engWord 作为变量传递, 您也应该在声明突变时使用相同的名称。像下面这样更改您的代码应该可以工作

const DELETE_WORD = gql`
      mutation($engWord: String!) {
        deleteWord(eng_word: $engWord) {

【讨论】:

    【解决方案2】:

    您将错误的参数传递给您的突变。您应该将eng_word 传递给deleteWordHook 的变量。
    试试这个:

    const DELETE_WORD = gql`
      mutation($eng_word: String!) {
        deleteWord(eng_word: $engWord) {
          eng_word
        }
      }
    `;
    const deleteWord = engWord => {
      deleteWordHook({ variables: { eng_word: engWord } }).then(closeModal());
      refetch();
    };

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2019-04-02
      • 2017-03-23
      • 2021-01-24
      • 2019-02-20
      • 1970-01-01
      • 2020-05-05
      • 2017-09-04
      • 2017-10-15
      相关资源
      最近更新 更多