gpt4 book ai didi

graphql - 如何使用 Apollo 客户端按顺序链接两个 GraphQL 查询

转载 作者:行者123 更新时间:2023-12-03 21:04:54 25 4
gpt4 key购买 nike

我在前端使用 Apollo Client,在后端使用 Graphcool。有两个查询firstQuerysecondQuery我希望在页面打开时按顺序调用它们。下面是示例代码(TestPage 组件的定义这里没有列出):

export default compose(
graphql(firstQuery, {
name: 'firstQuery'
}),
graphql(secondQuery, {
name: 'secondQuery' ,
options: (ownProps) => ({
variables: {
var1: *getValueFromFirstQuery*
}
})
})
)(withRouter(TestPage))

我需要得到 var1secondQuery来自 firstQuery 的结果.我如何使用 Apollo Client 和 compose 做到这一点?或者有没有其他方法可以做到这一点?提前致谢。

最佳答案

对于使用 react apollo hooks 的任何人同样的方法有效。

您可以使用两个 useQuery钩子(Hook)并将第一个查询的结果传递到 skip option第二个,

示例代码:

const AlertToolbar = ({ alertUid }: AlertToolbarProps) => {
const authenticationToken = useSelectAuthenticationToken()

const { data: data1 } = useQuery<DataResponse>(query, {
skip: !authenticationToken,
variables: {
alertUid,
},
context: makeContext(authenticationToken),
})

const { data: data2, error: error2 } = useQuery<DataResponse2>(query2, {
skip:
!authenticationToken ||
!data1 ||
!data1.alertOverview ||
!data1.alertOverview.deviceId,
variables: {
deviceId:
data1 && data1.alertOverview ? data1.alertOverview.deviceId : null,
},
context: makeContext(authenticationToken),
})

if (error2 || !data2 || !data2.deviceById || !data2.deviceById.id) {
return null
}
const { deviceById: device } = data2
return (
<Toolbar>
...
// do some stuff here with data12

关于graphql - 如何使用 Apollo 客户端按顺序链接两个 GraphQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49317582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com