我有以下代码,查询时出现错误,我不知道为什么。使用GraphiQL时,查询成功。我不确定这是否只是一个拼写错误,或者我的代码是否有问题。下面我包括了查询、调用查询的组件和架构。我收到的错误是“响应不成功:收到状态代码 400”。任何帮助将不胜感激。谢谢你。
查询:
import { gql } from "@apollo/client";
const CREATE_CLIENT_ACCOUNT = gql`
mutation CreateClientAccount($name: String, $email: String!, $password: String!) {
createClientAccount(name: $name, email: $email, password: $password) {
id
auth
}
}
`
export { CREATE_CLIENT_ACCOUNT };
查询电话:
import { useMutation } from "@apollo/client";
import { CREATE_CLIENT_ACCOUNT } from "../queries/accountQueries";
function CreateAccount({ name, email, password }) {
const [ createClientAccount, { data, loading, error }] = useMutation(CREATE_CLIENT_ACCOUNT);
return (
form onSubmit={(e) => {
e.preventDefault();
createClientAccount({
variables: {
name: name,
email: email,
password: password
}
})
}}>
</form>
)
}
架构:
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
createClientAccount: {
type: AccountType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
email: { type: new GraphQLNonNull(GraphQLString) },
password: { type: new GraphQLNonNull(GraphQLString) }
},
async resolve(parent, args) {
return await account.createAccount({
name: args.name,
email: args.email,
password: args.password
});
}
}
}
})
`name` parameter in your schema is also Required | Non Nullable ->
`name: { type: new GraphQLNonNull(GraphQLString) },`
所以你的突变应该如下:
const CREATE_CLIENT_ACCOUNT = gql`
mutation CreateClientAccount($name: String!, $email: String!, $password: String!) {
createClientAccount(name: $name, email: $email, password: $password) {
id
auth
}
}