Migrate away from item translations for itemByName GQL query

I'm not sure where this is used, my guess is it's for NeoBot?
This commit is contained in:
Emi Matchu 2024-02-20 16:36:59 -08:00
parent f196e2d8fe
commit 8948b6567e
2 changed files with 8 additions and 18 deletions

View file

@ -248,31 +248,21 @@ const buildItemByNameLoader = (db, loaders) =>
const qs = names.map((_) => "?").join(", "); const qs = names.map((_) => "?").join(", ");
const normalizedNames = names.map((name) => name.trim().toLowerCase()); const normalizedNames = names.map((name) => name.trim().toLowerCase());
const [rows] = await db.execute( const [rows] = await db.execute(
{ // NOTE: In our MySQL schema, this is a case-insensitive exact search.
// NOTE: In our MySQL schema, this is a case-insensitive exact search. `SELECT * FROM items WHERE name IN (${qs})`,
sql: `SELECT items.*, item_translations.* FROM item_translations
INNER JOIN items ON items.id = item_translations.item_id
WHERE name IN (${qs}) AND locale = "en"`,
nestTables: true,
},
normalizedNames, normalizedNames,
); );
const entitiesByName = new Map(); const entitiesByName = new Map();
for (const row of rows) { for (const row of rows) {
const item = normalizeRow(row.items); const item = normalizeRow(row);
const itemTranslation = normalizeRow(row.item_translations);
loaders.itemLoader.prime(item.id, item); loaders.itemLoader.prime(item.id, item);
loaders.itemTranslationLoader.prime(item.id, itemTranslation);
const normalizedName = itemTranslation.name.trim().toLowerCase(); const normalizedName = item.name.trim().toLowerCase();
entitiesByName.set(normalizedName, { item, itemTranslation }); entitiesByName.set(normalizedName, item);
} }
return normalizedNames.map( return normalizedNames.map((name) => entitiesByName.get(name) || null);
(name) =>
entitiesByName.get(name) || { item: null, itemTranslation: null },
);
}, },
{ cacheKeyFn: (name) => name.trim().toLowerCase() }, { cacheKeyFn: (name) => name.trim().toLowerCase() },
); );

View file

@ -743,12 +743,12 @@ const resolvers = {
return ids.map((id) => ({ id })); return ids.map((id) => ({ id }));
}, },
itemByName: async (_, { name }, { itemByNameLoader }) => { itemByName: async (_, { name }, { itemByNameLoader }) => {
const { item } = await itemByNameLoader.load(name); const item = await itemByNameLoader.load(name);
return item ? { id: item.id } : null; return item ? { id: item.id } : null;
}, },
itemsByName: async (_, { names }, { itemByNameLoader }) => { itemsByName: async (_, { names }, { itemByNameLoader }) => {
const items = await itemByNameLoader.loadMany(names); const items = await itemByNameLoader.loadMany(names);
return items.map(({ item }) => (item ? { id: item.id } : null)); return items.map((item) => (item ? { id: item.id } : null));
}, },
itemSearch: async ( itemSearch: async (
_, _,