使用GraphQl+查询数据与部分字符串匹配JavaScript中的Apollo客户端

发布于 2025-02-13 03:02:55 字数 634 浏览 1 评论 0原文

我有以下查询:

function useAllOrders(ownerId, storeId) {
  const { data, loading, error } = useQuery(
    gql`
      query getOrders($ownerId: String!) {
        orders(query: { owner_id: $ownerId }) {
          _id
          // ... field list              
        }
      }
    `,
    { variables: { ownerId: `owner_id=${ownerId}&store_id=${storeId}` } }
  );
  if (error) {
    throw new Error(`Failed to fetch tasks: ${error.message}`);
  }
  const orders = data?.orders ?? [];
  return { orders, loading };
}

如果StoreID为null |未定义,我需要做的就是仅由所有者查询订单。这意味着我应该能够使用所有者_id = $ {lansalId}%进行部分查找。我该如何实现?

I have the following query:

function useAllOrders(ownerId, storeId) {
  const { data, loading, error } = useQuery(
    gql`
      query getOrders($ownerId: String!) {
        orders(query: { owner_id: $ownerId }) {
          _id
          // ... field list              
        }
      }
    `,
    { variables: { ownerId: `owner_id=${ownerId}&store_id=${storeId}` } }
  );
  if (error) {
    throw new Error(`Failed to fetch tasks: ${error.message}`);
  }
  const orders = data?.orders ?? [];
  return { orders, loading };
}

What I need to do is being able to query the order just by owner if storeId is null|undefined. That means that I should be able to do a partial find with owner_id=${ownerId}%. How can I achieve this?

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

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

发布评论

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

评论(1

慈悲佛祖 2025-02-20 03:02:55

假设所有者变量是一个复合变量(奇数),那么

{ variables: { ownerId: `owner_id=${ownerId}&store_id=${storeId}` } }

即可。

{ variables: { ownerId: `owner_id=${ownerId}${storeId ? `&store_id=${storeId}` : ''}` } }

在创建变量的值时只需要有条件的检查

Assuming ownerId variable is a composite variable (odd) then

{ variables: { ownerId: `owner_id=${ownerId}&store_id=${storeId}` } }

would become

{ variables: { ownerId: `owner_id=${ownerId}${storeId ? `&store_id=${storeId}` : ''}` } }

You just need a conditional check when creating the variable's value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文