Skip to content

Commit

Permalink
fix(gatsby-source-vtex): page size (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Jul 1, 2021
1 parent 1cd7f1e commit 03b4b61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
8 changes: 4 additions & 4 deletions packages/gatsby-source-vtex/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
})
65 changes: 38 additions & 27 deletions packages/gatsby-source-vtex/src/graphql/pagination/product.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<IPage, IProduct> = {
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<IPage, IProduct> => {
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,
}
}

0 comments on commit 03b4b61

Please sign in to comment.