import type { IPaginationAdapter } from 'gatsby-graphql-source-toolkit' ; import type { Options } from '../../gatsby-node' ; interface IProduct { productId : string ; } interface IPage { products : IProduct [ ] ; } const PAGE_SIZE = 90 ; const MAX_PRODUCTS = 2500 ; export const ProductPaginationAdapter = ( { minProducts = MAX_PRODUCTS , } ) : IPaginationAdapter < IPage , IProduct > => { const threshold = Math . min ( minProducts , MAX_PRODUCTS ) ; return { name : 'ProductPaginationAdapter' , expectedVariableNames : [ `from` , `to` ] , start : ( ) => ( { 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 , } ; } ;