|
| 1 | +import { jsonToGraphQLQuery } from 'json-to-graphql-query'; |
| 2 | +import { ClientInterface } from './client'; |
| 3 | + |
| 4 | +export type ProductHydrater = ( |
| 5 | + items: string[], |
| 6 | + language: string, |
| 7 | + extraQuery?: any, |
| 8 | + perProduct?: (item: string, index: number) => any |
| 9 | +) => Promise<any>; |
| 10 | + |
| 11 | +export function createProductHydraterByPaths( |
| 12 | + client: ClientInterface |
| 13 | +): ProductHydrater { |
| 14 | + return <T>( |
| 15 | + paths: string[], |
| 16 | + language: string, |
| 17 | + extraQuery?: any, |
| 18 | + perProduct?: (item: string, index: number) => any |
| 19 | + ): Promise<T> => { |
| 20 | + const productListQuery = paths |
| 21 | + .map((path: string, index: number) => { |
| 22 | + return { |
| 23 | + [`product${index}`]: { |
| 24 | + __aliasFor: 'catalogue', |
| 25 | + __args: { path, language }, |
| 26 | + name: true, |
| 27 | + path: true, |
| 28 | + __on: { |
| 29 | + __typeName: 'Product', |
| 30 | + id: true, |
| 31 | + vatType: { |
| 32 | + name: true, |
| 33 | + percent: true |
| 34 | + }, |
| 35 | + variants: { |
| 36 | + id: true, |
| 37 | + sku: true, |
| 38 | + name: true, |
| 39 | + stock: true, |
| 40 | + priceVariants: { |
| 41 | + price: true, |
| 42 | + identifier: true, |
| 43 | + currency: true |
| 44 | + } |
| 45 | + }, |
| 46 | + ...(perProduct !== undefined |
| 47 | + ? perProduct(path, index) |
| 48 | + : {}) |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + }) |
| 53 | + .reduce((acc, curr) => { |
| 54 | + return { ...acc, ...curr }; |
| 55 | + }, {}); |
| 56 | + |
| 57 | + const query = { |
| 58 | + query: { |
| 59 | + ...{ ...productListQuery }, |
| 60 | + ...(extraQuery !== undefined ? extraQuery : {}) |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + const fetch = client.catalogueApi; |
| 65 | + return fetch(jsonToGraphQLQuery(query)); |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function createProductHydraterBySkus( |
| 70 | + client: ClientInterface |
| 71 | +): ProductHydrater { |
| 72 | + async function getPathForSkus( |
| 73 | + skus: string[], |
| 74 | + language: string |
| 75 | + ): Promise<string[]> { |
| 76 | + const search = client.searchApi; |
| 77 | + const pathsSet = new Set<string>(); |
| 78 | + let searchAfterCursor: any; |
| 79 | + async function getNextSearchPage() { |
| 80 | + const searchAPIResponse = await search( |
| 81 | + `query GET_PRODUCTS_BY_SKU ($skus: [String!], $after: String, $language: String!) { |
| 82 | + search ( |
| 83 | + after: $after |
| 84 | + language: $language |
| 85 | + filter: { |
| 86 | + include: { |
| 87 | + skus: $skus |
| 88 | + } |
| 89 | + } |
| 90 | + ) { |
| 91 | + pageInfo { |
| 92 | + endCursor |
| 93 | + hasNextPage |
| 94 | + } |
| 95 | + edges { |
| 96 | + node { |
| 97 | + path |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + }`, |
| 102 | + { |
| 103 | + skus: skus, |
| 104 | + after: searchAfterCursor, |
| 105 | + language |
| 106 | + } |
| 107 | + ); |
| 108 | + |
| 109 | + const { edges, pageInfo } = searchAPIResponse.search || {}; |
| 110 | + |
| 111 | + edges?.forEach((edge: any) => pathsSet.add(edge.node.path)); |
| 112 | + |
| 113 | + if (pageInfo?.hasNextPage) { |
| 114 | + searchAfterCursor = pageInfo.endCursor; |
| 115 | + await getNextSearchPage(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + await getNextSearchPage(); |
| 120 | + |
| 121 | + return Array.from(pathsSet); |
| 122 | + } |
| 123 | + return async <T>( |
| 124 | + skus: string[], |
| 125 | + language: string, |
| 126 | + extraQuery?: any, |
| 127 | + perProduct?: (item: string, index: number) => any |
| 128 | + ): Promise<T> => { |
| 129 | + const paths = await getPathForSkus(skus, language); |
| 130 | + if (paths.length === 0) { |
| 131 | + const empty = skus |
| 132 | + .map((sku: string, index: number) => { |
| 133 | + return { |
| 134 | + [`product${index}`]: {} |
| 135 | + }; |
| 136 | + }) |
| 137 | + .reduce((acc, curr) => { |
| 138 | + return { ...acc, ...curr }; |
| 139 | + }); |
| 140 | + |
| 141 | + return empty as any; |
| 142 | + } |
| 143 | + return createProductHydraterByPaths(client)( |
| 144 | + paths, |
| 145 | + language, |
| 146 | + extraQuery, |
| 147 | + perProduct |
| 148 | + ); |
| 149 | + }; |
| 150 | +} |
0 commit comments