Fetch only first 250 best selling products

This commit is contained in:
cond0r 2021-05-13 09:00:43 +03:00
parent a4f56d1549
commit 1ddabd2df8
2 changed files with 13 additions and 15 deletions

View File

@ -1,6 +1,4 @@
import { Product } from '@commerce/types'
import { getConfig, ShopifyConfig } from '../api' import { getConfig, ShopifyConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products'
import { ProductEdge } from '../schema' import { ProductEdge } from '../schema'
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query' import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
@ -21,21 +19,22 @@ const getAllProductPaths = async (options?: {
config?: ShopifyConfig config?: ShopifyConfig
preview?: boolean preview?: boolean
}): Promise<ReturnType> => { }): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {} let { config, variables = { first: 250, sortKey: 'BEST_SELLING' } } =
options ?? {}
config = getConfig(config) config = getConfig(config)
const products = await fetchAllProducts({ const { data } = await config.fetch(getAllProductsPathsQuery, {
config,
query: getAllProductsPathsQuery,
variables, variables,
}) })
return { return {
products: products?.map(({ node: { handle } }: ProductEdge) => ({ products: data.products?.edges?.map(
node: { ({ node: { handle } }: ProductEdge) => ({
path: `/${handle}`, node: {
}, path: `/${handle}`,
})), },
})
),
} }
} }

View File

@ -27,10 +27,9 @@ const getAllProducts = async (options: {
{ variables } { variables }
) )
const products = const products = data.products?.edges?.map(({ node: p }: ProductEdge) =>
data.products?.edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p)
normalizeProduct(p) )
) ?? []
return { return {
products, products,