我需要在按下Download按钮时调用查询,然后处理响应。我只需要在按下下载按钮时执行查询,而不是在react组件加载时执行查询。因此,我使用了graphql中的"useLazyQuery"选项。
我需要这样的东西:
const [downloadAllRecipients,{ loading, error, data }] = useLazyQuery(QUERY,
{
variables: {
campaignId: props.campaignId,
},
fetchPolicy: 'network-only',
},
);
//This is the download button onPress event handler.
const downloadDataButton = async () => {
const res = await downloadAllRecipients({ variables: { campaignID: props.campaignId }})
console.log(res) // handle response
}
//Output:-
//undefined
执行上述查询时,res是未定义的。然而,在网络层中可以看到graphql查询。我有一个反应前端,不能访问查询返回的数据。请帮助!提前感谢!
你会得到的数据onCompleted
事件。
const [downloadAllRecipients,{ loading, error, data }] = useLazyQuery(QUERY,
{
variables: {
campaignId: props.campaignId,
},
fetchPolicy: 'network-only',
onCompleted: res => { setRes(res); console.log(res);}
},
);