我正在从阿波罗客户端运行具有以下结构的查询:
export const LOGIN_USER = gql`
mutation loginUser($authenticationInput: AuthenticationInput){
loginUser(authenticationInput: $authenticationInput){
email
password
}
} `;
在服务器端,我有以下解析器,它是基于TypeGraphQL构建的:
@Mutation(() => User)
async loginUser(@Arg('authenticationInput') authenticationInput : AuthenticationInput): Promise<User> {
logResolver.info("Login user query received.");
let result = await this.userService.findUser(authenticationInput)
.then(result => {
return result;
});
return result;
}
我从服务器收到400响应。我检查了网络控制台,响应中显示了一条消息,内容如下:
extensions: {code: "GRAPHQL_VALIDATION_FAILED", exception: {stacktrace: [,…]}}
locations: [{line: 1, column: 20}, {line: 2, column: 34}]
message: "Variable "$authenticationInput" of type "AuthenticationInput" used in position expecting type "AuthenticationInput!"."
我不确定该如何解决类型不匹配的问题。它声称期望“ AuthenticationInput!”也很奇怪。此处的类型应该是该类型,但不带!。
这是React代码,显示了我如何从组件中调用查询:
export default function LoginForm() {
const [fields, setFields] = useState({});
const client = useApolloClient();
const [loginUser, { loading, error, data }] = useMutation(LOGIN_USER, {
onCompleted({ login }) {
console.log("Logged in baby");
localStorage.setItem('token', login);
client.writeData({data: { isLoggedIn: true}});
}
});
function updateField(idx : string, value : string){
setFields({...fields, [idx]: value});
}
function userLogin(){
loginUser({variables: fields});
}
return(<div>
{loading ? <p>loading!</p> : null}
{error ? <p>Error!</p> : null}
<InputText onChange={updateField} id="email" type="email" placeholder="email"></InputText>
<InputText onChange={updateField} id="password" type="password" placeholder="password"></InputText>
<SubmitFormButton
click={userLogin}
text="login"></SubmitFormButton>
</div>
);
};
答案 0 :(得分:2)
您共享的查询具有AuthenticationInput
(不是AuthenticationInput!
),表示此值可以为空。
export const LOGIN_USER = gql`
mutation loginUser($authenticationInput: AuthenticationInput) {
loginUser(authenticationInput: $authenticationInput) {
email
password
}
}
`;
但是您看到的错误:"Variable "$authenticationInput" of type "AuthenticationInput" used in position expecting type "AuthenticationInput!"
表示GraphQL服务器期望AuthenticationInput!
(非空值)。
您是否尝试过更新查询以匹配这些类型?
export const LOGIN_USER = gql`
mutation loginUser($authenticationInput: AuthenticationInput!) {
loginUser(authenticationInput: $authenticationInput) {
email
password
}
}
`;