【发布时间】:2018-02-02 19:20:35
【问题描述】:
我在发送删除突变后无法让 React+Apollo 更新商店。我正在使用内置 apollo+react 和 express graphQL 服务器的 reactQL 样板(我没有安装 apollo 服务器 - 我只是使用参考 express-graphQL 包)。我的数据是用_id存储在mongoDB中的,但是客户端的实际数据使用id作为id。
apollo 客户端是这样定义的:
new ApolloClient(
Object.assign(
{
reduxRootSelector: state => state.apollo,
dataIdFromObject: o => o.id
},
opt
)
);
我有一个父组件,它使用 从 'src/graphql/queries/courses.gql
导入课程@graphql(courses)
export default class CoursesPage extends Component {
constructor(props){
super(props)
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(event) {
this.props.mutate({ variables: {id: selectedId}}
.catch(err => console.log(err))
}
render() {
return (
{ this.props.data.courses.map(course =>
<CourseDelete selectedId={course.id} key={course.id} />
})
}
)
}
}
还有一个看起来像这样的子组件:
import deleteCoursefrom 'src/graphql/mutations/deleteCourse.gql
@graphql(deleteCourse)
export default class CourseDelete extends Component {
constructor(props){
super(props)
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(event) {
this.props.mutate({ variables: {id: this.props.selectedId}}
.catch(err => console.log(err))
}
render() {
return (
<button onClick={this.handleDelete}>Button</button>
)
}
}
deleteCourse.gql:
mutation deleteCourse($id: String!) {
deleteCourse(id: $id) {
id
}
}
我原来的查询是在 courses.gql 中:
query courses {
courses {
id
}
}
【问题讨论】:
标签: mongodb reactjs apollo react-apollo reactql