Skip to content

Commit e682e4b

Browse files
committed
refactor: Small improvement to hydrater looping
Instead of doing .map.reduce and spreading it gets more readable and performant with a single reduce and writing directly to memo
1 parent 69a1da8 commit e682e4b

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

src/core/hydrate.ts

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,38 @@ function byPaths(client: ClientInterface): ProductHydrater {
1717
perProduct?: (path: string, index: number) => any,
1818
perVariant?: (path: string, index: number) => any,
1919
): Promise<T> => {
20-
const productListQuery = paths
21-
.map((path: string, index: number) => {
22-
return {
23-
[`product${index}`]: {
24-
__aliasFor: 'catalogue',
25-
__args: { path, language },
20+
const productListQuery = paths.reduce((acc, path: string, index: number) => {
21+
acc[`product${index}`] = {
22+
__aliasFor: 'catalogue',
23+
__args: { path, language },
24+
name: true,
25+
path: true,
26+
__on: {
27+
__typeName: 'Product',
28+
vatType: {
2629
name: true,
27-
path: true,
28-
__on: {
29-
__typeName: 'Product',
30-
vatType: {
31-
name: true,
32-
percent: true,
33-
},
34-
variants: {
35-
sku: true,
36-
name: true,
37-
attributes: {
38-
attribute: true,
39-
value: true,
40-
},
41-
priceVariants: {
42-
name: true,
43-
price: true,
44-
identifier: true,
45-
currency: true,
46-
},
47-
...(perVariant !== undefined ? perVariant(path, index) : {}),
48-
},
49-
...(perProduct !== undefined ? perProduct(path, index) : {}),
30+
percent: true,
31+
},
32+
variants: {
33+
sku: true,
34+
name: true,
35+
attributes: {
36+
attribute: true,
37+
value: true,
38+
},
39+
priceVariants: {
40+
name: true,
41+
price: true,
42+
identifier: true,
43+
currency: true,
5044
},
45+
...(perVariant !== undefined ? perVariant(path, index) : {}),
5146
},
52-
};
53-
})
54-
.reduce((acc, curr) => {
55-
return { ...acc, ...curr };
56-
}, {});
47+
...(perProduct !== undefined ? perProduct(path, index) : {}),
48+
},
49+
};
50+
return acc;
51+
}, {} as any);
5752

5853
const query = {
5954
...{ ...productListQuery },
@@ -73,26 +68,26 @@ function bySkus(client: ClientInterface): ProductHydrater {
7368
async function getNextSearchPage() {
7469
const searchAPIResponse = await search(
7570
`query GET_PRODUCTS_BY_SKU ($skus: [String!], $after: String, $language: String!) {
76-
search (
77-
after: $after
78-
language: $language
79-
filter: {
80-
include: {
81-
skus: $skus
82-
}
83-
}
84-
) {
85-
pageInfo {
86-
endCursor
87-
hasNextPage
88-
}
89-
edges {
90-
node {
91-
path
71+
search (
72+
after: $after
73+
language: $language
74+
filter: {
75+
include: {
76+
skus: $skus
77+
}
78+
}
79+
) {
80+
pageInfo {
81+
endCursor
82+
hasNextPage
83+
}
84+
edges {
85+
node {
86+
path
87+
}
88+
}
9289
}
93-
}
94-
}
95-
}`,
90+
}`,
9691
{
9792
skus: skus,
9893
after: searchAfterCursor,
@@ -123,15 +118,10 @@ function bySkus(client: ClientInterface): ProductHydrater {
123118
): Promise<T> => {
124119
const paths = await getPathForSkus(skus, language);
125120
if (paths.length === 0) {
126-
const empty = skus
127-
.map((sku: string, index: number) => {
128-
return {
129-
[`product${index}`]: {},
130-
};
131-
})
132-
.reduce((acc, curr) => {
133-
return { ...acc, ...curr };
134-
});
121+
const empty = skus.reduce((acc, sku, index) => {
122+
acc[`product${index}`] = {};
123+
return acc;
124+
}, {} as any);
135125

136126
return empty as any;
137127
}

0 commit comments

Comments
 (0)