basic outfit data from GQL

This commit is contained in:
Emi Matchu 2021-01-04 07:36:00 +00:00
parent 778eefbdd5
commit b8d919b247
3 changed files with 42 additions and 1 deletions

View file

@ -25,6 +25,13 @@ function UserOutfitsPageContent() {
currentUser { currentUser {
outfits { outfits {
id id
name
petAppearance {
id
}
wornItems {
id
}
} }
} }
} }
@ -46,7 +53,7 @@ function UserOutfitsPageContent() {
return ( return (
<code> <code>
<pre>Data: {JSON.stringify(data)}</pre> <pre>Data: {JSON.stringify(data, null, 4)}</pre>
</code> </code>
); );
} }

View file

@ -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) => const buildUserLastTradeActivityLoader = (db) =>
new DataLoader(async (userIds) => { new DataLoader(async (userIds) => {
const qs = userIds.map((_) => "?").join(","); const qs = userIds.map((_) => "?").join(",");
@ -1156,6 +1176,7 @@ function buildLoaders(db) {
loaders.userByEmailLoader = buildUserByEmailLoader(db); loaders.userByEmailLoader = buildUserByEmailLoader(db);
loaders.userClosetHangersLoader = buildUserClosetHangersLoader(db); loaders.userClosetHangersLoader = buildUserClosetHangersLoader(db);
loaders.userClosetListsLoader = buildUserClosetListsLoader(db, loaders); loaders.userClosetListsLoader = buildUserClosetListsLoader(db, loaders);
loaders.userOutfitsLoader = buildUserOutfitsLoader(db, loaders);
loaders.userLastTradeActivityLoader = buildUserLastTradeActivityLoader(db); loaders.userLastTradeActivityLoader = buildUserLastTradeActivityLoader(db);
loaders.zoneLoader = buildZoneLoader(db); loaders.zoneLoader = buildZoneLoader(db);
loaders.zoneTranslationLoader = buildZoneTranslationLoader(db); loaders.zoneTranslationLoader = buildZoneTranslationLoader(db);

View file

@ -19,6 +19,10 @@ const typeDefs = gql`
# When this user last updated any of their trade lists, as an ISO 8601 # When this user last updated any of their trade lists, as an ISO 8601
# timestamp. # timestamp.
lastTradeActivity: String! 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 { extend type Query {
@ -222,6 +226,15 @@ const resolvers = {
const lastTradeActivity = await userLastTradeActivityLoader.load(id); const lastTradeActivity = await userLastTradeActivityLoader.load(id);
return lastTradeActivity.toISOString(); 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: { Query: {