Add total item count to search results

This will help us set up pagination!
This commit is contained in:
Emi Matchu 2021-02-06 21:50:52 -08:00
parent 6738f07e64
commit 2d0601cfeb
3 changed files with 51 additions and 28 deletions

View file

@ -129,6 +129,7 @@ function ItemSearchPageResults({ query: latestQuery }) {
offset: 0 offset: 0
limit: 30 limit: 30
) { ) {
numTotalItems
items { items {
id id
name name
@ -197,7 +198,8 @@ function ItemSearchPageResults({ query: latestQuery }) {
details="I wanted to get this out asap for looking up specific items! Multi-page browsing coming soon 😅" details="I wanted to get this out asap for looking up specific items! Multi-page browsing coming soon 😅"
marginTop="6" marginTop="6"
> >
We only show the first 30 results for now! 😅 We only show the first 30 results for now! 😅 (
{data.itemSearch.numTotalItems.toLocaleString()} total)
</WIPCallout> </WIPCallout>
</Flex> </Flex>
)} )}

View file

@ -330,36 +330,56 @@ const buildItemSearchLoader = (db, loaders) =>
? [currentUserId, currentUserOwnsOrWants === "OWNS" ? "1" : "0"] ? [currentUserId, currentUserOwnsOrWants === "OWNS" ? "1" : "0"]
: []; : [];
const [rows, _] = await db.execute( const queryJoins = `
`SELECT DISTINCT items.*, t.name FROM items INNER JOIN item_translations t ON t.item_id = items.id
INNER JOIN item_translations t ON t.item_id = items.id INNER JOIN parents_swf_assets rel
INNER JOIN parents_swf_assets rel ON rel.parent_type = "Item" AND rel.parent_id = items.id
ON rel.parent_type = "Item" AND rel.parent_id = items.id INNER JOIN swf_assets ON rel.swf_asset_id = swf_assets.id
INNER JOIN swf_assets ON rel.swf_asset_id = swf_assets.id ${currentUserJoin}
${currentUserJoin} `;
WHERE ${matcherPlaceholders} AND t.locale = "en" AND
${bodyIdCondition} AND const queryConditions = `
${zoneIdsCondition} AND ${itemKindCondition} AND ${matcherPlaceholders} AND t.locale = "en" AND
${currentUserCondition} ${bodyIdCondition} AND
ORDER BY t.name ${zoneIdsCondition} AND ${itemKindCondition} AND
LIMIT ? OFFSET ?`, ${currentUserCondition}
[ `;
...wordMatchersForMysql, const queryConditionValues = [
...bodyIdValues, ...wordMatchersForMysql,
...zoneIds, ...bodyIdValues,
...currentUserValues, ...zoneIds,
actualLimit, ...currentUserValues,
actualOffset, ];
]
); const [[rows, _], [totalRows, __]] = await Promise.all([
db.execute(
`
SELECT DISTINCT items.*, t.name FROM items
${queryJoins}
WHERE ${queryConditions}
ORDER BY t.name
LIMIT ? OFFSET ?
`,
[...queryConditionValues, actualLimit, actualOffset]
),
db.execute(
`
SELECT count(DISTINCT items.id) AS numTotalItems FROM items
${queryJoins}
WHERE ${queryConditions}
`,
queryConditionValues
),
]);
const entities = rows.map(normalizeRow); const entities = rows.map(normalizeRow);
const { numTotalItems } = totalRows[0];
for (const item of entities) { for (const item of entities) {
loaders.itemLoader.prime(item.id, item); loaders.itemLoader.prime(item.id, item);
} }
return entities; return [entities, numTotalItems];
} }
); );

View file

@ -115,6 +115,7 @@ const typeDefs = gql`
query: String! query: String!
zones: [Zone!]! zones: [Zone!]!
items: [Item!]! items: [Item!]!
numTotalItems: Int!
} }
type ItemTrade { type ItemTrade {
@ -472,7 +473,7 @@ const resolvers = {
} }
bodyId = petType.bodyId; bodyId = petType.bodyId;
} }
const items = await itemSearchLoader.load({ const [items, numTotalItems] = await itemSearchLoader.load({
query: query.trim(), query: query.trim(),
bodyId, bodyId,
itemKind, itemKind,
@ -483,7 +484,7 @@ const resolvers = {
limit, limit,
}); });
const zones = zoneIds.map((id) => ({ id })); const zones = zoneIds.map((id) => ({ id }));
return { query, zones, items }; return { query, zones, items, numTotalItems };
}, },
itemSearchToFit: async ( itemSearchToFit: async (
_, _,
@ -509,7 +510,7 @@ const resolvers = {
); );
} }
const { bodyId } = petType; const { bodyId } = petType;
const items = await itemSearchLoader.load({ const [items, numTotalItems] = await itemSearchLoader.load({
query: query.trim(), query: query.trim(),
itemKind, itemKind,
currentUserOwnsOrWants, currentUserOwnsOrWants,
@ -520,7 +521,7 @@ const resolvers = {
limit, limit,
}); });
const zones = zoneIds.map((id) => ({ id })); const zones = zoneIds.map((id) => ({ id }));
return { query, zones, items }; return { query, zones, items, numTotalItems };
}, },
newestItems: async (_, __, { newestItemsLoader }) => { newestItems: async (_, __, { newestItemsLoader }) => {
const items = await newestItemsLoader.load("all-newest"); const items = await newestItemsLoader.load("all-newest");