【发布时间】:2020-05-08 21:28:12
【问题描述】:
有人能解释一下,当我尝试使用返回错误的突变时,为什么我的 react app + apollo 会这样?
GraphQL 突变返回此(响应代码为 200):{"errors":[{"error":{"result":"identity.not-found","error":"authentication-failed","statusCode":401}}],"data":{"login":null}}
我的变异是这样的:
export const LOGIN_MUTATION = gql`
mutation($input: LoginInput!) {
login(input: $input) {
token
}
}
`;
调用:
const handleSignIn = () => {
loginMutation({
variables: {
input: {
clientId: config.clientId,
username: userName,
password: password,
clientSecret: config.clientSecret
}
}
});
};
它的行为有一段时间像预期的那样(呈现了我自己的自定义错误组件 - {error && <div>error</div>}),但随后它抛出了这个未处理的拒绝。
如果我将 catch 回调添加到突变调用,它会按预期工作。
但是,我在 apollo 文档中没有找到任何关于需要始终以这种方式捕获 GraphQL 错误的内容。这应该足够了,如果我理解正确的话: const [loginMutation, {data, loading, error}] = useMutation(LOGIN_MUTATION);
这种行为是正确的还是我错过了什么?
版本:
"@apollo/react-hooks": "^3.1.3"
"apollo-boost": "^0.4.7"
"graphql": "^14.5.8"
【问题讨论】: