【发布时间】:2020-11-11 06:01:21
【问题描述】:
我有一个用 Express / Apollo Server / GraphQL / MongoDB / Mongoose 制作的后端和一个用 React 制作的前端。
我有我想要做突变和查询的组件,我检查了前端和后端之间的连接,它很好。
我在后端的突变:
createUser(input: SignupInput!): AuthUser!
input SignupInput {
name: String!
username: String!
email: String!
password: String!
}
AuthUser 返回的是一个用户!有更多字段,但我认为这不是必要的信息。
在我的前端
const NEW_USER = gql`
mutation CreateUser($type: SignupInput!) {
createUser(input: $type) {
user {
id
}
}
}
`;
const [newTest, { data }] = useMutation(NEW_USER);
const onSubmit = (formData) => {
newTest({
variables: {
name: formData.name,
username: formData.username,
email: formData.email,
password: formData.password,
},
});
};
错误 400 的网络响应:
"Variable \"$type\" of required type \"SignupInput!\" was not provided."
我真的不明白为什么会这样,我在其他人的代码中看到,使用后端输入类型的名称创建变量是可行的。
如果我尝试解构 SignInput!输入并写入 {$name: !String, ...) 我收到此错误:
Unknown argument \"name\" on field \"Mutation.createUser\".", locations: […], extensions: {…} }
【问题讨论】:
-
newTest({ 变量:{ >>>>> 类型:{
-
谢谢老兄,真的很有帮助。
标签: javascript reactjs graphql schema apollo-server