reactjs - 如何从另一个组件重新获取 graphql 查询?
问题描述
我对 React 非常陌生,尤其是对 graphql。
每次使用突变 createEvent 时,我都会尝试重新获取事件查询,因此包含所有事件的页面将自动更新,不需要刷新。
但是,我找不到连接这两个查询的方法。
我的突变 createEvent 来自“form.jsx”文件:
const body = {
query:
"mutation{" +
"createEvent(eventInput:{num1:" +
'"' +
this.state.num1.toString() +
'"' +
",num2:" +
'"' +
this.state.num2.toString() +
'"' +
"}){" +
"_id " +
"num1 " +
"num2 " +
"addition " +
"multiply" +
"}}"
};
fetch("https://localhost:8000/graphql", {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json"
}
})
.then(res => {
if (res.status !== 200 && res.status !== 201) {
throw new Error("Failed!");
}
return res.json();
})
.then(resData => {
console.log(resData);
})
.catch(err => {
console.log(err);
});
这是我从“myTable.jsx”文件中查询的事件:
const body = {
query:
"query {" +
"events {" +
"_id " +
"num1 " +
"num2 " +
"addition " +
"multiply" +
"}}"
};
//console.log(body);
fetch("https://localhost:8000/graphql", {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json"
}
})
.then(res => {
if (res.status !== 200 && res.status !== 201) {
throw new Error("Failed!");
}
return res.json();
})
.then(resData => {
const events = resData.data.events;
this.setState({ list: events });
})
.catch(err => {
console.log(err);
});
我在 Promise 'then' 函数中尝试了 refetchQueries 函数,但它崩溃了。
解决方案
我从 queries.js 文件中导出了查询,然后启用了使用 ApolloClient 的重新获取选项。
推荐阅读
- reactjs - 在 React 中使用 useNavigate 和 Typescript
- c# - 无法从 Playwright 的下拉选项中选择一个选项?
- vlookup - 用其他查询中的值替换单元格中的列表
- perl - 当我在 MacBookPro BigSur 11 中安装 Set::Object 时出现“致命错误:找不到‘EXTERN.h’文件”
- c++ - 在 C++ 中分离该线程后使用 cin 获取输入时出现问题
- php - Laravel - 将 mysql 日期时间转换为以前的格式
- c# - 一起使用 INSERT INTO SELECT & VALUES
- serialization - 带有 API 平台的 GraphQL 嵌入式资源序列化上下文/组
- tensorflow - 这个有 10 个节点的网络的输出层如何对应一个整数?
- asp.net-core - 会话超时到期更快