[英]Using the Amplify Graphql Client in Javascript to Query Data
[英]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.