来自react-apollo-hooks的useMutation没有传递变量

时间:2019-05-16 01:30:06

标签: reactjs graphql apollo react-apollo

我正在将代码从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

1 个答案:

答案 0 :(得分:1)

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

const variables = { email, password }