add neomail username to user GQL

This commit is contained in:
Emi Matchu 2020-10-23 22:55:13 -07:00
parent 851e6201eb
commit 53d399f46b
4 changed files with 36 additions and 0 deletions

View file

@ -28,6 +28,7 @@ GRANT INSERT ON modeling_logs TO impress2020;
GRANT SELECT, INSERT, DELETE ON closet_hangers TO impress2020; GRANT SELECT, INSERT, DELETE ON closet_hangers TO impress2020;
GRANT SELECT ON closet_lists TO impress2020; GRANT SELECT ON closet_lists TO impress2020;
GRANT SELECT ON item_outfit_relationships TO impress2020; GRANT SELECT ON item_outfit_relationships TO impress2020;
GRANT SELECT ON neopets_connections TO impress2020;
GRANT SELECT ON outfits TO impress2020; GRANT SELECT ON outfits TO impress2020;
GRANT SELECT ON users TO impress2020; GRANT SELECT ON users TO impress2020;

View file

@ -475,6 +475,19 @@ const buildPetSwfAssetLoader = (db, loaders) =>
); );
}); });
const buildNeopetsConnectionLoader = (db) =>
new DataLoader(async (ids) => {
const qs = ids.map((_) => "?").join(", ");
const [rows, _] = await db.execute(
`SELECT * FROM neopets_connections WHERE id IN (${qs})`,
ids
);
const entities = rows.map(normalizeRow);
return ids.map((id) => entities.find((e) => e.id === id));
});
const buildOutfitLoader = (db) => const buildOutfitLoader = (db) =>
new DataLoader(async (outfitIds) => { new DataLoader(async (outfitIds) => {
const qs = outfitIds.map((_) => "?").join(","); const qs = outfitIds.map((_) => "?").join(",");
@ -741,6 +754,7 @@ function buildLoaders(db) {
loaders.swfAssetByRemoteIdLoader = buildSwfAssetByRemoteIdLoader(db); loaders.swfAssetByRemoteIdLoader = buildSwfAssetByRemoteIdLoader(db);
loaders.itemSwfAssetLoader = buildItemSwfAssetLoader(db, loaders); loaders.itemSwfAssetLoader = buildItemSwfAssetLoader(db, loaders);
loaders.petSwfAssetLoader = buildPetSwfAssetLoader(db, loaders); loaders.petSwfAssetLoader = buildPetSwfAssetLoader(db, loaders);
loaders.neopetsConnectionLoader = buildNeopetsConnectionLoader(db);
loaders.outfitLoader = buildOutfitLoader(db); loaders.outfitLoader = buildOutfitLoader(db);
loaders.itemOutfitRelationshipsLoader = buildItemOutfitRelationshipsLoader( loaders.itemOutfitRelationshipsLoader = buildItemOutfitRelationshipsLoader(
db db

View file

@ -12,6 +12,7 @@ describe("User", () => {
user(id: "6") { user(id: "6") {
id id
username username
contactNeopetsUsername
} }
} }
`, `,
@ -21,6 +22,7 @@ describe("User", () => {
expect(res.data).toMatchInlineSnapshot(` expect(res.data).toMatchInlineSnapshot(`
Object { Object {
"user": Object { "user": Object {
"contactNeopetsUsername": "matchu1993",
"id": "6", "id": "6",
"username": "matchu", "username": "matchu",
}, },
@ -34,6 +36,12 @@ describe("User", () => {
"6", "6",
], ],
], ],
Array [
"SELECT * FROM neopets_connections WHERE id IN (?)",
Array [
"1",
],
],
] ]
`); `);
}); });

View file

@ -4,6 +4,7 @@ const typeDefs = gql`
type User { type User {
id: ID! id: ID!
username: String! username: String!
contactNeopetsUsername: String
itemsTheyOwn: [Item!]! itemsTheyOwn: [Item!]!
itemsTheyWant: [Item!]! itemsTheyWant: [Item!]!
} }
@ -21,6 +22,18 @@ const resolvers = {
return user.name; return user.name;
}, },
contactNeopetsUsername: async (
{ id },
_,
{ userLoader, neopetsConnectionLoader }
) => {
const user = await userLoader.load(id);
const neopetsConnection = await neopetsConnectionLoader.load(
user.contactNeopetsConnectionId
);
return neopetsConnection.neopetsUsername;
},
itemsTheyOwn: async ( itemsTheyOwn: async (
{ id }, { id },
_, _,