Last active
September 8, 2020 13:40
-
-
Save alfari16/a95a9eaf818e166fd785311afe807599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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