我正在尝试完全与graphql服务器建立连接。我从mongodb获取查询没有问题,但是我有突变问题。其实我看到了一些变化
但是它从标题中的问题中检索错误。另外,控制台中的 rendered by 选项卡与我一直在学习的“ ninja graphql教程项目”的选项卡不同,因此如果我一开始正确设置代码,我会感到非常困惑。
我的设置:
queries.js
export const addUserMutation = gql`
mutation createUser($input: UserInput) {
createUser(input: $input) {
firstName
lastName
email
items {
text
timeISO
title
}
}
}
`;
app.js
const client = new ApolloClient({
uri: "http://localhost:5000/graphql"
});
function App() {
return (
<Router>
<ApolloProvider client={client}>
<Route exact path="/" component={Homepage} />
<Route exact path="/users" component={UsersList} />
<Route exact path="/adduser" component={AddUser} />
</ApolloProvider>
</Router>
);
}
addUser.js
import { graphql } from "react-apollo";
import * as compose from "lodash.flowright";
import { addUserMutation } from "../queries/queries";
const AddUser = props => {
let [firstName, setFirstName] = useState("");
let [lastName, setLastName] = useState("");
let [email, setEmail] = useState("");
let [gender, setGender] = useState("MALE");
let [items, setItems] = useState("");
const onChange = e => {
if (e.target.name === "firstName") {
setFirstName(e.target.value);
}
if (e.target.name === "lastName") {
setLastName(e.target.value);
}
if (e.target.name === "email") {
setEmail(e.target.value);
}
if (e.target.name === "items") {
setItems(e.target.value);
}
if (e.target.name === "gender") {
setGender(e.target.value);
}
};
const onSubmit = e => {
e.preventDefault();
console.log(props);
props.addUserMutation({
variables: {
firstName,
lastName,
email,
gender,
items
}
});
return (....)
};
export default compose(graphql(addUserMutation, { name: "addUserMutation" }))(
AddUser
);
答案 0 :(得分:-1)
请在createUser突变中设置响应参数。请参见以下示例:
mutation createUser($input: UserInput) {
createUser(input: $input) {
firstName
lastName
email
items {
text
timeISO
title
}
} Response
}
type Response {
_id:String
name:String
}
**类型关键字用于返回响应数据