【发布时间】:2019-07-12 02:26:03
【问题描述】:
我目前正在使用 React 和 GraphQL 构建一个简单的 CRUD 工作流。在我创建了一个对象(在这种情况下是一个article,它只有一个id、title 和description。)后,我导航回一个Index 页面,其中显示了所有当前创建的文章。我的问题是创建文章后,在我刷新页面之前,索引页面不会显示创建的文章。我正在使用apollo 查询graphql api 并禁用了缓存,所以我不确定为什么数据没有显示。我在我的ArticlesIndex 的componentDidMount 函数中设置了断点,并确保它正在执行,并且在执行时,数据库确实包含新添加的article。
当执行检索所有文章的客户端查询时,我的服务器端实际上甚至从未被命中。我不确定是什么在缓存这些数据,以及为什么它没有按预期从服务器检索。
我的ArticlesCreate 组件插入新记录并重定向回ArticlesIndex 组件,如下所示:
handleSubmit(event) {
event.preventDefault();
const { client } = this.props;
var article = {
"article": {
"title": this.state.title,
"description": this.state.description
}
};
client
.mutate({ mutation: CREATE_EDIT_ARTICLE,
variables: article })
.then(({ data: { articles } }) => {
this.props.history.push("/articles");
})
.catch(err => {
console.log("err", err);
});
}
}
然后我的ArticlesIndex 组件从数据库中检索所有文章,如下所示:
componentDidMount = () => {
const { client } = this.props; //client is an ApolloClient
client
.query({ query: GET_ARTICLES })
.then(({ data: { articles } }) => {
if (articles) {
this.setState({ loading: false, articles: articles });
}
})
.catch(err => {
console.log("err", err);
});
};
并且我已将 ApolloClient 设置为不缓存数据,就像我的 App.js 中一样,如下所示:
const defaultApolloOptions = {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
}
export default class App extends Component {
displayName = App.name;
client = new ApolloClient({
uri: "https://localhost:44360/graphql",
cache: new InMemoryCache(),
defaultOptions: defaultApolloOptions
});
//...render method, route definitions, etc
}
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
假设
articles是一个数组this.setState({ loading: false, articles: [...articles] });?还是需要解析的JSON? -
articles确实是一个数组,但是设置状态工作正常。据我所知,问题是articles在componentDidMount中我的查询的.then回调中包含陈旧数据(在执行创建调用之前) -
你如何导航回来?浏览器返回按钮或调用您的索引组件?
-
this.props.history.push("/articles");在ArticlesCreate组件中(该路由用于ArticlesIndex组件
标签: reactjs graphql apollo react-apollo apollo-client