我正在将代码从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
。
答案 0 :(得分:1)
您的文档中有两个变量-email
和password
。您实际上传递给useMutation
的是一个名为data
的变量,该变量不存在。
const variables = { email, password }