From b8d919b24747b0839669db909adb363a3bac2d49 Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 4 Jan 2021 07:36:00 +0000 Subject: [PATCH] basic outfit data from GQL --- src/app/UserOutfitsPage.js | 9 ++++++++- src/server/loaders.js | 21 +++++++++++++++++++++ src/server/types/User.js | 13 +++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/app/UserOutfitsPage.js b/src/app/UserOutfitsPage.js index 2c783489..fe76f623 100644 --- a/src/app/UserOutfitsPage.js +++ b/src/app/UserOutfitsPage.js @@ -25,6 +25,13 @@ function UserOutfitsPageContent() { currentUser { outfits { id + name + petAppearance { + id + } + wornItems { + id + } } } } @@ -46,7 +53,7 @@ function UserOutfitsPageContent() { return ( -
Data: {JSON.stringify(data)}
+
Data: {JSON.stringify(data, null, 4)}
); } diff --git a/src/server/loaders.js b/src/server/loaders.js index afcc1174..cc6009ca 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -998,6 +998,26 @@ const buildUserClosetListsLoader = (db, loaders) => ); }); +const buildUserOutfitsLoader = (db, loaders) => + new DataLoader(async (userIds) => { + const qs = userIds.map((_) => "?").join(","); + const [rows, _] = await db.execute( + `SELECT * FROM outfits + WHERE user_id IN (${qs}) + ORDER BY name`, + userIds + ); + + const entities = rows.map(normalizeRow); + for (const entity of entities) { + loaders.outfitLoader.prime(entity.id, entity); + } + + return userIds.map((userId) => + entities.filter((e) => e.userId === String(userId)) + ); + }); + const buildUserLastTradeActivityLoader = (db) => new DataLoader(async (userIds) => { const qs = userIds.map((_) => "?").join(","); @@ -1156,6 +1176,7 @@ function buildLoaders(db) { loaders.userByEmailLoader = buildUserByEmailLoader(db); loaders.userClosetHangersLoader = buildUserClosetHangersLoader(db); loaders.userClosetListsLoader = buildUserClosetListsLoader(db, loaders); + loaders.userOutfitsLoader = buildUserOutfitsLoader(db, loaders); loaders.userLastTradeActivityLoader = buildUserLastTradeActivityLoader(db); loaders.zoneLoader = buildZoneLoader(db); loaders.zoneTranslationLoader = buildZoneTranslationLoader(db); diff --git a/src/server/types/User.js b/src/server/types/User.js index c6a4cafb..07d6cd17 100644 --- a/src/server/types/User.js +++ b/src/server/types/User.js @@ -19,6 +19,10 @@ const typeDefs = gql` # When this user last updated any of their trade lists, as an ISO 8601 # timestamp. lastTradeActivity: String! + + # This user's outfits. Returns an empty list if the current user is not + # authorized to see them. + outfits: [Outfit!]! } extend type Query { @@ -222,6 +226,15 @@ const resolvers = { const lastTradeActivity = await userLastTradeActivityLoader.load(id); return lastTradeActivity.toISOString(); }, + + outfits: async ({ id }, _, { currentUserId, userOutfitsLoader }) => { + if (currentUserId !== id) { + return []; + } + + const outfits = await userOutfitsLoader.load(id); + return outfits.map((outfit) => ({ id: outfit.id })); + }, }, Query: {