问题描述
我正在使用以下版本的graphql并做出反应。
“反应阿波罗”:“ ^ 1.4.10”, “ graphiql”:“ ^ 0.11.2”, “ graphql”:“ ^ 14.0.2”,
以下是compose函数中graphql调用的代码段。 我想使用async await来调用graphql,所以第二个调用将一直等待直到响应来自第一个。第三个graphl调用只会在第二个调用之后。
我们如何实现以下代码。
export default compose(
withRouter,connect(
mapStateToProps,mapDispatchToProps,),graphql(call1,{
options: props => {
return {
fetchPolicy: 'network-only',variables: { id },};
},props: ({ data }) => {
return data
},}),graphql(call2,variables: { firstcallid },graphql(call3,variables: { based on the graphql 1 and graphql call 2 },)(Component);
解决方法
我认为您可以尝试使用.then
export default compose(
withRouter,connect(
mapStateToProps,mapDispatchToProps,),graphql(call1,{
options: props => {
return {
fetchPolicy: 'network-only',variables: { id },};
},props: ({ data }) => {
return data
},}).then((res)=>{
graphql(call2,variables: { firstcallid },}).then((res)=>{ graphql(call3,variables: { based on the graphql 1 and graphql call 2 },})
})
})
)(Component);
如果满足条件,您还可以检查通话后收到的res
对象的字段!