Prevent endless limit refetching (#1059)

* Fix endless limit refresh loop

* Improve naming
This commit is contained in:
Dominik Żegleń 2021-04-14 13:09:13 +02:00 committed by GitHub
parent 9362ea53b0
commit 07bc522af1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 16 deletions

View file

@ -1,5 +1,5 @@
import { limitFragment } from "@saleor/fragments/shop"; 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 gql from "graphql-tag";
import { TypedQuery } from "../../queries"; import { TypedQuery } from "../../queries";
@ -39,21 +39,37 @@ const shopInfo = gql`
`; `;
export const TypedShopInfoQuery = TypedQuery<ShopInfo, {}>(shopInfo); export const TypedShopInfoQuery = TypedQuery<ShopInfo, {}>(shopInfo);
const limitVariables: Record<keyof RefreshLimitsVariables, boolean> = {
channels: false,
orders: false,
productVariants: false,
staffUsers: false,
warehouses: false
};
const limitInfo = gql` const limitInfo = gql`
${limitFragment} ${limitFragment}
query RefreshLimits( query RefreshLimits(
$channels: Boolean = false $channels: Boolean!
$orders: Boolean = false $orders: Boolean!
$productVariants: Boolean = false $productVariants: Boolean!
$staffUsers: Boolean = false $staffUsers: Boolean!
$warehouses: Boolean = false $warehouses: Boolean!
) { ) {
shop { shop {
...ShopLimitFragment ...ShopLimitFragment
} }
} }
`; `;
export const useShopLimitsQuery = makeQuery< const useBaseShopLimitsQuery = makeQuery<RefreshLimits, RefreshLimitsVariables>(
RefreshLimits, limitInfo
RefreshLimitsVariables );
>(limitInfo); export const useShopLimitsQuery = (
opts: UseQueryOpts<Partial<RefreshLimitsVariables>>
) =>
useBaseShopLimitsQuery({
...opts,
variables: {
...limitVariables,
...opts.variables
}
});

View file

@ -41,9 +41,9 @@ export interface RefreshLimits {
} }
export interface RefreshLimitsVariables { export interface RefreshLimitsVariables {
channels?: boolean | null; channels: boolean;
orders?: boolean | null; orders: boolean;
productVariants?: boolean | null; productVariants: boolean;
staffUsers?: boolean | null; staffUsers: boolean;
warehouses?: boolean | null; warehouses: boolean;
} }

View file

@ -42,7 +42,7 @@ export interface LoadMore<TData, TVariables> {
export type UseQueryResult<TData, TVariables> = QueryResult<TData, TVariables> & export type UseQueryResult<TData, TVariables> = QueryResult<TData, TVariables> &
LoadMore<TData, TVariables>; LoadMore<TData, TVariables>;
type UseQueryOpts<TVariables> = Partial<{ export type UseQueryOpts<TVariables> = Partial<{
displayLoader: boolean; displayLoader: boolean;
skip: boolean; skip: boolean;
variables: TVariables; variables: TVariables;