簡體   English   中英

在 Javascript 中使用 GraphQL + Apollo 客戶端查詢具有部分字符串匹配的數據

[英]Query data with partial string match using GraphQL + Apollo client in Javascript

我有以下查詢:

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 為空|未定義,我需要做的是能夠僅按所有者查詢訂單。 這意味着我應該能夠使用 owner_id=${ownerId}% 進行部分查找。 我怎樣才能做到這一點?

假設ownerId變量是一個復合變量(奇數),那么

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

會成為

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

創建變量的值時,您只需要進行條件檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM