From 07bc522af103100664fb18018d953a07c166cb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20=C5=BBegle=C5=84?= Date: Wed, 14 Apr 2021 13:09:13 +0200 Subject: [PATCH] Prevent endless limit refetching (#1059) * Fix endless limit refresh loop * Improve naming --- src/components/Shop/query.ts | 36 ++++++++++++++++------ src/components/Shop/types/RefreshLimits.ts | 10 +++--- src/hooks/makeQuery.ts | 2 +- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/components/Shop/query.ts b/src/components/Shop/query.ts index 5a3b56441..5c185fc33 100644 --- a/src/components/Shop/query.ts +++ b/src/components/Shop/query.ts @@ -1,5 +1,5 @@ import { limitFragment } from "@saleor/fragments/shop"; -import makeQuery from "@saleor/hooks/makeQuery"; +import makeQuery, { UseQueryOpts } from "@saleor/hooks/makeQuery"; import gql from "graphql-tag"; import { TypedQuery } from "../../queries"; @@ -39,21 +39,37 @@ const shopInfo = gql` `; export const TypedShopInfoQuery = TypedQuery(shopInfo); +const limitVariables: Record = { + channels: false, + orders: false, + productVariants: false, + staffUsers: false, + warehouses: false +}; const limitInfo = gql` ${limitFragment} query RefreshLimits( - $channels: Boolean = false - $orders: Boolean = false - $productVariants: Boolean = false - $staffUsers: Boolean = false - $warehouses: Boolean = false + $channels: Boolean! + $orders: Boolean! + $productVariants: Boolean! + $staffUsers: Boolean! + $warehouses: Boolean! ) { shop { ...ShopLimitFragment } } `; -export const useShopLimitsQuery = makeQuery< - RefreshLimits, - RefreshLimitsVariables ->(limitInfo); +const useBaseShopLimitsQuery = makeQuery( + limitInfo +); +export const useShopLimitsQuery = ( + opts: UseQueryOpts> +) => + useBaseShopLimitsQuery({ + ...opts, + variables: { + ...limitVariables, + ...opts.variables + } + }); diff --git a/src/components/Shop/types/RefreshLimits.ts b/src/components/Shop/types/RefreshLimits.ts index f76dfa554..7280252bd 100644 --- a/src/components/Shop/types/RefreshLimits.ts +++ b/src/components/Shop/types/RefreshLimits.ts @@ -41,9 +41,9 @@ export interface RefreshLimits { } export interface RefreshLimitsVariables { - channels?: boolean | null; - orders?: boolean | null; - productVariants?: boolean | null; - staffUsers?: boolean | null; - warehouses?: boolean | null; + channels: boolean; + orders: boolean; + productVariants: boolean; + staffUsers: boolean; + warehouses: boolean; } diff --git a/src/hooks/makeQuery.ts b/src/hooks/makeQuery.ts index 3511a3c30..6648d93c9 100644 --- a/src/hooks/makeQuery.ts +++ b/src/hooks/makeQuery.ts @@ -42,7 +42,7 @@ export interface LoadMore { export type UseQueryResult = QueryResult & LoadMore; -type UseQueryOpts = Partial<{ +export type UseQueryOpts = Partial<{ displayLoader: boolean; skip: boolean; variables: TVariables;