Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-source-vtex): page size #799

Merged
merged 5 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
tlgimenes marked this conversation as resolved.
Show resolved Hide resolved
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it always true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we set this to false we will never reach page 2. Setting this to true makes us fetch at least page 2 and check if we do have more pages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works when we have less than PAGE_SIZE products as well. An example is storecomponents account that has 50-ish products

}),
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,
}
}