使用react apollo将变量传递到graphql查询
我在将变量选项传递到graphql查询时遇到问题。如果console.log(ownProps.match.params.id)是正确的id 然而,我得到了这个错误: 错误:“selectCustomer”包装“Apollo(Apollo(withRouter(Consumer))”操作需要变量:“id”,但在传递给“Apollo(Apollo(withRouter(Consumer))”的道具中找不到该变量 出口使用react apollo将变量传递到graphql查询,graphql,react-apollo,apollo-client,Graphql,React Apollo,Apollo Client,我在将变量选项传递到graphql查询时遇到问题。如果console.log(ownProps.match.params.id)是正确的id 然而,我得到了这个错误: 错误:“selectCustomer”包装“Apollo(Apollo(withRouter(Consumer))”操作需要变量:“id”,但在传递给“Apollo(Apollo(withRouter(Consumer))”的道具中找不到该变量 出口 const ConsumerWithMutations = graphql(s
const ConsumerWithMutations = graphql(selectCustomer, {
name : 'selectCustomer',
options: (ownProps) => ({
variables: {
id: ownProps.match.params.id
}
})
})(graphql(deleteCustomer, {
name: 'deleteCustomer'
})(withRouter(Consumer)))
export default graphql(selectCustomer)(ConsumerWithMutations);
质疑
您试图用同一个查询包装组件两次——第一次是在
ConsumerWithMutations
的定义中,然后再次是在export语句中
您没有将任何选项传递到export语句中的HOC中,因此您的selectCustomer
查询无法找到您指定的id
变量,这就是您看到错误的原因
将导出语句修改为:
export default ConsumerWithMutations;
它应该会起作用
我还强烈建议在使用多个HOC时使用
compose
,这样有助于保持代码可读性。查看该示例。您尝试使用同一查询包装组件两次——第一次是在您的ConsumerWithTranslations
定义中,然后是在您的export语句中
您没有将任何选项传递到export语句中的HOC中,因此您的selectCustomer
查询无法找到您指定的id
变量,这就是您看到错误的原因
将导出语句修改为:
export default ConsumerWithMutations;
它应该会起作用
我还强烈建议在使用多个HOC时使用
compose
,这样有助于保持代码可读性。看看这个例子。非常感谢你,伙计!Will def check compose out-这是一件非常明显的事情:(我有一个错误,没有以正确的方式传递id参数,使用:options:props=>({id:props.match.params.parentid}),我用options:props=>({variables:{id:props.match.params.parentid})修复了这个错误),非常感谢!Will def check compose-这是一件非常明显的事情:(我有一个错误,没有以正确的方式传递id参数,使用:options:props=>({id:props.match.params.parentid}),我用options:props=>({variables:{id:props.match.params.parentid})修复了这个错误。),