执行查询之后,懒惰查询GraphQl响应数据在React内部无法访问

发布于 2025-02-12 13:12:46 字数 686 浏览 0 评论 0原文

按下下载按钮时,我需要调用查询,然后处理响应。我只需要在按下下载按钮时即可执行查询,而不是当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查询。我有一个React前端,无法访问查询返回的数据。请帮助! 提前致谢!

I need to call a query when Download button is pressed and then handle the response.I only need to execute the query when download button is pressed and not when the react component loads. Hence I used 'useLazyQuery' option from graphql.

I need something like this:

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

When executing the above query, res is undefined. However, the graphql query was seen in the network layer. I have a react frontend and am not able to access the data returned by the query.Please help!
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2025-02-19 13:12:46

您将在oncompleded事件中获取数据。

因此您的代码应该看起来像以下内容,

const [downloadAllRecipients,{ loading, error, data }] = useLazyQuery(QUERY,
    {
      variables: {
        campaignId: props.campaignId,
      },
      fetchPolicy: 'network-only',
     onCompleted: res => { setRes(res); console.log(res);}
    },
  );

You will get the data in onCompleted event.

So your code should look like following,

const [downloadAllRecipients,{ loading, error, data }] = useLazyQuery(QUERY,
    {
      variables: {
        campaignId: props.campaignId,
      },
      fetchPolicy: 'network-only',
     onCompleted: res => { setRes(res); console.log(res);}
    },
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文