【问题标题】:useMutation from react-apollo-hooks is not passing variablesreact-apollo-hooks 中的 useMutation 没有传递变量
【发布时间】:2019-10-03 04:53:49
【问题描述】:

我正在将我的代码从 apollo-boost 重构为 react-apollo-hooks,但我不断收到以下错误:

Variable "$email" of required type "String!" was not provided
Variable "$password" of required type "String!" was not provided

我的重构代码如下:

const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const variables = {
    data: { email, password }
}
const login = useMutation(LOGIN_MUTATION, {
    variables
})

const onSubmitHandler = async (login, e) => {
    e.preventDefault()
    const authResults = await login()
    localStorage.setItem(AUTH_TOKEN, authResults.data.login.token);
    props.history.push('/') 
}

graphql 突变

const LOGIN_MUTATION = gql`
  mutation Login($email: String!, $password: String!) {
    login(data: {
        email: $email, password: $password
        }
    ){
      token
      user {
        id
      }
    }
  }
`

我的架构

login(data: LoginUserInput!): AuthPayload!

console.logging 电子邮件和密码变量表明它们已正确传递给useMutation

【问题讨论】:

    标签: reactjs graphql apollo react-apollo


    【解决方案1】:

    您的文档中有两个变量——emailpassword。您实际上传递给useMutation 的是一个名为data 的变量,它不存在。

    const variables = { email, password }
    

    【讨论】:

      猜你喜欢
      • 2020-01-25
      • 2020-04-28
      • 2020-06-21
      • 2019-10-06
      • 2020-01-11
      • 2021-05-01
      • 2020-05-18
      • 2021-09-03
      • 2020-06-19
      相关资源
      最近更新 更多