diff --git a/scripts/setup-mysql.sql b/scripts/setup-mysql.sql index b89d155..1018209 100644 --- a/scripts/setup-mysql.sql +++ b/scripts/setup-mysql.sql @@ -28,6 +28,7 @@ GRANT INSERT ON modeling_logs TO impress2020; GRANT SELECT, INSERT, DELETE ON closet_hangers TO impress2020; GRANT SELECT ON closet_lists 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 users TO impress2020; diff --git a/src/server/loaders.js b/src/server/loaders.js index bada992..68d8de8 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -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) => new DataLoader(async (outfitIds) => { const qs = outfitIds.map((_) => "?").join(","); @@ -741,6 +754,7 @@ function buildLoaders(db) { loaders.swfAssetByRemoteIdLoader = buildSwfAssetByRemoteIdLoader(db); loaders.itemSwfAssetLoader = buildItemSwfAssetLoader(db, loaders); loaders.petSwfAssetLoader = buildPetSwfAssetLoader(db, loaders); + loaders.neopetsConnectionLoader = buildNeopetsConnectionLoader(db); loaders.outfitLoader = buildOutfitLoader(db); loaders.itemOutfitRelationshipsLoader = buildItemOutfitRelationshipsLoader( db diff --git a/src/server/query-tests/User.test.js b/src/server/query-tests/User.test.js index a248a60..7e54e45 100644 --- a/src/server/query-tests/User.test.js +++ b/src/server/query-tests/User.test.js @@ -12,6 +12,7 @@ describe("User", () => { user(id: "6") { id username + contactNeopetsUsername } } `, @@ -21,6 +22,7 @@ describe("User", () => { expect(res.data).toMatchInlineSnapshot(` Object { "user": Object { + "contactNeopetsUsername": "matchu1993", "id": "6", "username": "matchu", }, @@ -34,6 +36,12 @@ describe("User", () => { "6", ], ], + Array [ + "SELECT * FROM neopets_connections WHERE id IN (?)", + Array [ + "1", + ], + ], ] `); }); diff --git a/src/server/types/User.js b/src/server/types/User.js index dc16286..43a253f 100644 --- a/src/server/types/User.js +++ b/src/server/types/User.js @@ -4,6 +4,7 @@ const typeDefs = gql` type User { id: ID! username: String! + contactNeopetsUsername: String itemsTheyOwn: [Item!]! itemsTheyWant: [Item!]! } @@ -21,6 +22,18 @@ const resolvers = { 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 ( { id }, _,