如何在react中基于其他graphql的响应调用graphql?

问题描述

我正在使用以下版本的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对象的字段!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...