Skip to content

Instantly share code, notes, and snippets.

@alfari16
Last active September 8, 2020 13:40
Show Gist options
  • Select an option

  • Save alfari16/a95a9eaf818e166fd785311afe807599 to your computer and use it in GitHub Desktop.

Select an option

Save alfari16/a95a9eaf818e166fd785311afe807599 to your computer and use it in GitHub Desktop.
// graphql.ts
// ...other codes
const loaders = () => {
return {
// ...other loaders
getProductsByMerchantId(pagination: IPagination){
const {limit, offset, orderBy, sortBy, search} = pagination;
return new Dataloader(async merchantIds => {
const connection = Knex(config.get('database'));
const table = connection('products');
const {limit, offset, orderBy, sortBy, search} = productsPagination;
if(search)
table.where('name', 'LIKE', `%${search}%`);
const products = await connection.union(
merchantIds.map(id =>
table
.where('merchantId', id)
.orderBy(orderBy, sortBy)
.limit(limit)
.offset(offset)
),
true //option for wrap queries
);
return merchantIds.map(merchantId => products.filter(product => product.merchantId === merchantId));
});
}
};
};
// ...other codes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment