【问题标题】:Graphql React mutation doesn't give me a responseGraphql React 突变没有给我回应
【发布时间】:2019-09-29 02:37:58
【问题描述】:

我尝试从我的 react 应用程序中调用 graphql 突变,但没有得到任何响应。

在操场上我的变异起作用了

这就是我所说的突变:

async getValues(values) {
    const mutation = await this.props.mutate({
      variables: {
        name: values.username,
        email: values.email,
        password: values.password
      }});
}

这是我的突变:

const registerMutation = gql`
  mutation($name: String!, $email: String!, $password: String!) {
    register(name: $name, email: $email, password: $password) {
      ok
      error
    }
  }
`;

我确定调用了函数 getValues 并且 this.props.mutate 是一个函数,但我没有得到任何响应

【问题讨论】:

  • 展示如何在 reactjs 中调用 registerMutation

标签: reactjs graphql


【解决方案1】:

问题是你没有通过突变。您需要添加此行。

mutation = {registerMutation } 

所以它会是这样的:

async getValues(values) {
    const mutation = await this.props.mutate({
      mutation = {registerMutation } 
      variables: {
        name: values.username,
        email: values.email,
        password: values.password
      }});

并将您的 const 更改为:

const registerMutation = gql`
  mutation registerMutation($name: String!, $email: String!, $password: String!) {
    register(name: $name, email: $email, password: $password) {
      ok
      error
    }
  }
`;

如果没有帮助,请告诉我

【讨论】:

  • 如果我添加 mutation : registerMutation 我会得到同样的结果(无响应)
  • 如果我添加 mutation : {registerMutation} 我会收到一条错误消息,说我需要传递一个 graphql 文档
  • 如果我记录 registerMutation 它确认它是一个文档
  • 对不起,我错了,当我连接到后端时,我输错了 uri
  • 好的,很高兴你能解决它。
猜你喜欢
  • 2018-07-15
  • 2020-05-05
  • 2021-02-27
  • 2018-07-02
  • 2016-03-08
  • 2018-10-17
  • 2019-11-25
  • 2021-04-25
  • 2021-04-18
相关资源
最近更新 更多