From 4c78abee962a151583b7c9f5720a9b59eae9d599 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 2 Jul 2020 14:33:47 -0700 Subject: [PATCH] fix bug resolving item fields under pet outfit --- src/server/index.js | 42 ++++++++++++++----- src/server/loaders.js | 8 ++-- src/server/query-tests/Outfit.test.js | 12 +++--- src/server/query-tests/Pet.test.js | 5 +++ .../__snapshots__/Pet.test.js.snap | 40 ++++++++++++++++++ .../SpeciesColorPair.test.js.snap | 8 ++++ 6 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/server/index.js b/src/server/index.js index 228371e..d53f809 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -161,31 +161,46 @@ const typeDefs = gql` const resolvers = { Item: { - name: async (item, _, { itemTranslationLoader }) => { - // Search queries pre-fill this! - if (item.name) return item.name; - - const translation = await itemTranslationLoader.load(item.id); + name: async ({ id, name }, _, { itemTranslationLoader }) => { + if (name) return name; + const translation = await itemTranslationLoader.load(id); return translation.name; }, - description: async (item, _, { itemTranslationLoader }) => { - const translation = await itemTranslationLoader.load(item.id); + description: async ({ id, description }, _, { itemTranslationLoader }) => { + if (description) return description; + const translation = await itemTranslationLoader.load(id); return translation.description; }, - isNc: (item) => item.rarityIndex === 500 || item.rarityIndex === 0, + thumbnailUrl: async ({ id, thumbnailUrl }, _, { itemLoader }) => { + if (thumbnailUrl) return thumbnailUrl; + const item = await itemLoader.load(id); + return item.thumbnailUrl; + }, + rarityIndex: async ({ id, rarityIndex }, _, { itemLoader }) => { + if (rarityIndex) return rarityIndex; + const item = await itemLoader.load(id); + return item.rarityIndex; + }, + isNc: async ({ id, rarityIndex }, _, { itemLoader }) => { + if (rarityIndex) return rarityIndex === 500 || rarityIndex === 0; + const item = await itemLoader.load({ id }); + return item.rarityIndex === 500 || item.rarityIndex === 0; + }, appearanceOn: async ( - item, + { id }, { speciesId, colorId }, - { petTypeBySpeciesAndColorLoader, itemSwfAssetLoader } + { petTypeBySpeciesAndColorLoader, itemSwfAssetLoader, itemLoader } ) => { + const itemPromise = itemLoader.load(id); const petType = await petTypeBySpeciesAndColorLoader.load({ speciesId: speciesId, colorId: colorId, }); const allSwfAssets = await itemSwfAssetLoader.load({ - itemId: item.id, + itemId: id, bodyId: petType.bodyId, }); + if (allSwfAssets.length === 0) { // If there's no assets at all, treat it as non-fitting: no appearance. // (If there are assets but they're non-SWF, we'll treat this as @@ -196,6 +211,7 @@ const resolvers = { const swfAssets = allSwfAssets.filter((sa) => sa.url.endsWith(".swf")); const restrictedZones = []; + const item = await itemPromise; for (const [i, bit] of Array.from(item.zonesRestrict).entries()) { if (bit === "1") { const zone = { id: i + 1 }; @@ -426,6 +442,10 @@ const resolvers = { pose: getPoseFromPetData(petMetaData, customPetData), items: Object.values(customPetData.object_info_registry).map((o) => ({ id: o.obj_info_id, + name: o.name, + description: o.description, + thumbnailUrl: o.thumbnail_url, + rarityIndex: o.rarity_index, })), }; return outfit; diff --git a/src/server/loaders.js b/src/server/loaders.js index 037c3a2..73b4080 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -59,7 +59,7 @@ const loadAllPetTypes = (db) => async () => { return entities; }; -const buildItemsLoader = (db) => +const buildItemLoader = (db) => new DataLoader(async (ids) => { const qs = ids.map((_) => "?").join(","); const [rows, _] = await db.execute( @@ -72,7 +72,7 @@ const buildItemsLoader = (db) => return ids.map( (id) => - entitiesById.get(id) || new Error(`could not find item with ID: ${id}`) + entitiesById.get(String(id)) || new Error(`could not find item with ID: ${id}`) ); }); @@ -89,7 +89,7 @@ const buildItemTranslationLoader = (db) => return itemIds.map( (itemId) => - entitiesByItemId.get(itemId) || + entitiesByItemId.get(String(itemId)) || new Error(`could not find translation for item ${itemId}`) ); }); @@ -352,7 +352,7 @@ function buildLoaders(db) { loaders.loadAllPetTypes = loadAllPetTypes(db); loaders.colorTranslationLoader = buildColorTranslationLoader(db); - loaders.itemLoader = buildItemsLoader(db); + loaders.itemLoader = buildItemLoader(db); loaders.itemTranslationLoader = buildItemTranslationLoader(db); loaders.itemSearchLoader = buildItemSearchLoader(db); loaders.itemSearchToFitLoader = buildItemSearchToFitLoader(db); diff --git a/src/server/query-tests/Outfit.test.js b/src/server/query-tests/Outfit.test.js index 75ce97d..8c49ba0 100644 --- a/src/server/query-tests/Outfit.test.js +++ b/src/server/query-tests/Outfit.test.js @@ -56,6 +56,12 @@ describe("Outfit", () => { "31856", ], ], + Array [ + "SELECT * FROM pet_states WHERE id IN (?)", + Array [ + "3951", + ], + ], Array [ "SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?,?,?,?) AND locale = \\"en\\"", Array [ @@ -72,12 +78,6 @@ describe("Outfit", () => { "56398", ], ], - Array [ - "SELECT * FROM pet_states WHERE id IN (?)", - Array [ - "3951", - ], - ], Array [ "SELECT * FROM pet_types WHERE id IN (?)", Array [ diff --git a/src/server/query-tests/Pet.test.js b/src/server/query-tests/Pet.test.js index 1465c9b..2b51d73 100644 --- a/src/server/query-tests/Pet.test.js +++ b/src/server/query-tests/Pet.test.js @@ -18,6 +18,11 @@ describe("Pet", () => { pose items { id + name + description + thumbnailUrl + rarityIndex + isNc } } } diff --git a/src/server/query-tests/__snapshots__/Pet.test.js.snap b/src/server/query-tests/__snapshots__/Pet.test.js.snap index 876fc77..27fcffa 100644 --- a/src/server/query-tests/__snapshots__/Pet.test.js.snap +++ b/src/server/query-tests/__snapshots__/Pet.test.js.snap @@ -9,28 +9,68 @@ Object { }, "items": Array [ Object { + "description": "What does this ball actually do?", "id": "37229", + "isNc": false, + "name": "Magic Ball Table", + "rarityIndex": 101, + "thumbnailUrl": "http://images.neopets.com/items/gif_magicball_table.gif", }, Object { + "description": "Dont forget to wish upon a star.", "id": "37375", + "isNc": false, + "name": "Moon and Stars Background", + "rarityIndex": 75, + "thumbnailUrl": "http://images.neopets.com/items/bg_moonstars.gif", }, Object { + "description": "Hide your face and hair so no one can recognise you.", "id": "38911", + "isNc": false, + "name": "Zafara Agent Hood", + "rarityIndex": 92, + "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_hood.gif", }, Object { + "description": "This robe is great for being stealthy.", "id": "38912", + "isNc": false, + "name": "Zafara Agent Robe", + "rarityIndex": 90, + "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_robe.gif", }, Object { + "description": "Dont leave any trace that you were there with these gloves.", "id": "38913", + "isNc": false, + "name": "Zafara Agent Gloves", + "rarityIndex": 88, + "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_gloves.gif", }, Object { + "description": "These leaves almost look magical with their gentle glow.", "id": "43014", + "isNc": false, + "name": "Green Leaf String Lights", + "rarityIndex": 80, + "thumbnailUrl": "http://images.neopets.com/items/toy_stringlight_illleaf.gif", }, Object { + "description": "This jewelled staff shines with a magical light.", "id": "43397", + "isNc": true, + "name": "Jewelled Staff", + "rarityIndex": 500, + "thumbnailUrl": "http://images.neopets.com/items/mall_staff_jewelled.gif", }, Object { + "description": "Even the announcers of the Altador Cup celebrate. This was given out by the Advent Calendar in Y11.", "id": "48313", + "isNc": false, + "name": "Altador Cup Brooch", + "rarityIndex": 101, + "thumbnailUrl": "http://images.neopets.com/items/clo_altcuplogo_brooch.gif", }, ], "pose": "SAD_MASC", diff --git a/src/server/query-tests/__snapshots__/SpeciesColorPair.test.js.snap b/src/server/query-tests/__snapshots__/SpeciesColorPair.test.js.snap index 5d01528..1d6c55e 100644 --- a/src/server/query-tests/__snapshots__/SpeciesColorPair.test.js.snap +++ b/src/server/query-tests/__snapshots__/SpeciesColorPair.test.js.snap @@ -571,6 +571,14 @@ Object { "id": "1", }, }, + Object { + "color": Object { + "id": "110", + }, + "species": Object { + "id": "1", + }, + }, Object { "color": Object { "id": "-1",