diff --git a/packages/gatsby-source-vtex/src/gatsby-node.ts b/packages/gatsby-source-vtex/src/gatsby-node.ts index 7f4c2959a4..2d0fdacb17 100644 --- a/packages/gatsby-source-vtex/src/gatsby-node.ts +++ b/packages/gatsby-source-vtex/src/gatsby-node.ts @@ -60,9 +60,9 @@ export interface Options extends PluginOptions, VTEXOptions { ignorePaths?: string[] concurrency?: number /** - * @description max number of paths for getStaticPaths to generate + * @description minimum number of products to fetch from catalog * */ - maxNumPaths?: number + minProducts?: number } const DEFAULT_PAGE_TYPES_WHITELIST = [ @@ -358,7 +358,7 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ( execute: run, gatsbyTypePrefix: `Store`, gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }), - paginationAdapters: [ProductPaginationAdapter], + paginationAdapters: [ProductPaginationAdapter(options)], } // Step5. Add explicit types to gatsby schema @@ -569,5 +569,5 @@ export const pluginOptionsSchema = ({ Joi }: PluginOptionsSchemaArgs) => getStaticPaths: Joi.function().arity(0), getRedirects: Joi.function().arity(0), pageTypes: Joi.array().items(Joi.string()), - maxNumPaths: Joi.number(), + minProducts: Joi.number(), }) diff --git a/packages/gatsby-source-vtex/src/graphql/pagination/product.ts b/packages/gatsby-source-vtex/src/graphql/pagination/product.ts index 23a3591c94..26d2d41595 100644 --- a/packages/gatsby-source-vtex/src/graphql/pagination/product.ts +++ b/packages/gatsby-source-vtex/src/graphql/pagination/product.ts @@ -1,5 +1,7 @@ import type { IPaginationAdapter } from 'gatsby-graphql-source-toolkit' +import type { Options } from '../../gatsby-node' + // Define pagination adapters interface IProduct { productId: string @@ -11,31 +13,40 @@ interface IPage { // Current max page size supported by VTEX API // TODO: Increasing this number could help on our build times -const PAGE_SIZE = 100 - -export const ProductPaginationAdapter: IPaginationAdapter = { - name: 'ProductPaginationAdapter', - expectedVariableNames: [`from`, `to`], - start: () => ({ - // Our search is inclusive, so 0 -> PAGE_SIZE - 1 fetches PAGE_SIZE items - variables: { from: 0, to: PAGE_SIZE - 1 }, - hasNextPage: true, - }), - next: (state, page) => { - const from = Number(state.variables.from) + PAGE_SIZE - const to = Number(state.variables.to) + PAGE_SIZE - - return { - variables: { - from, - to, - }, - // VTEX Search API hard limits us to 2500 products at most - hasNextPage: page.products.length > 0 && to < 2500, - } - }, - concat: (result, page) => ({ - products: result.products.concat(page.products), - }), - getItems: (pageOrResult) => pageOrResult.products, +const PAGE_SIZE = 90 + +// VTEX Search API hard limits us to 2500 products at most +// Increasing this hard limit on the API may help us fetch more products +const MAX_PRODUCTS = 2500 + +export const ProductPaginationAdapter = ({ + minProducts = MAX_PRODUCTS, +}: Options): IPaginationAdapter => { + const threshold = Math.min(minProducts, MAX_PRODUCTS) + + return { + name: 'ProductPaginationAdapter', + expectedVariableNames: [`from`, `to`], + start: () => ({ + // Our search is inclusive, so 0 -> PAGE_SIZE - 1 fetches PAGE_SIZE items + variables: { from: 0, to: PAGE_SIZE - 1 }, + hasNextPage: true, + }), + next: (state, page) => { + const from = Number(state.variables.from) + PAGE_SIZE + const to = Number(state.variables.to) + PAGE_SIZE + + return { + variables: { + from, + to, + }, + hasNextPage: page.products.length > 0 && to < threshold, + } + }, + concat: (result, page) => ({ + products: result.products.concat(page.products), + }), + getItems: (pageOrResult) => pageOrResult.products, + } }