From 142625d616cd4b294ada752ebcd1f9360e776c04 Mon Sep 17 00:00:00 2001 From: Joaquin Niembro Bueno Date: Thu, 9 May 2024 08:42:28 -0600 Subject: [PATCH] GQL home assignment Nerdery --- src/index.ts | 4 ++++ src/resolver.ts | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index cd9125c..b39e97b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,10 @@ import { typeDefs } from "./schema"; import {getUserById, getUsers} from "./resolver" const resolvers = { + DataSource: { + DATA_SOURCE_1: 0, + DATA_SOURCE_2: 1, + }, Query: { getUsers: () => getUsers(), getUserById: (parent, { id }) => { diff --git a/src/resolver.ts b/src/resolver.ts index 2eeba49..c0432c4 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -7,15 +7,18 @@ export function getUsers(): User[] { // Hint: You may want to use the normalizeUser function on each user object before returning the final array. const rawUsers = [...userData1, ...userData2]; - return []; // TODO: Replace this with the normalized list of users. + return rawUsers.map(normalizeUser); // TODO: Replace this with the normalized list of users. } export function getUserById(id: string | number): User | null { // Task: Implement this function to return a user by their ID from the correct data source. // Hint: Check the type of `id` to determine whether to search in userData1 or userData2. // userData1 contains UUIDs (strings), while userData2 contains numerical IDs. + const dataSource = typeof id === "string" ? userData1 : userData2; + const userFound = dataSource.find((user) => user.id === id); + if (!userFound) return null; - return null; // TODO: Implement the logic to find and return the appropriate user. + return normalizeUser(userFound); // TODO: Implement the logic to find and return the appropriate user. } // This function is intended to normalize user data from different formats into a uniform schema. @@ -24,11 +27,14 @@ function normalizeUser(user: any): User { // Hint: Map properties from both userData1 and userData2 to this uniform schema. // Example mapping: `first` or `first_name` should map to `firstName`. return { - id: '', // TODO: Set this to the user's id. - firstName: '', // TODO: Set this to the user's first name, considering both 'first' and 'first_name'. - lastName: '', // TODO: Set this to the user's last name, considering both 'last' and 'last_name'. - emailAddress: '', // TODO: Set this to the user's email address, considering both 'email' and 'email_address'. - phoneNumber: '', // TODO: Set this to the user's phone number, considering both 'phone' and 'phone_number'. - source: DataSource.DATA_SOURCE_1 // Hint: Determine the source based on the ID type (string for DATA_SOURCE_1, number for DATA_SOURCE_2). - }; + id: user.id, // TODO: Set this to the user's id. + firstName: user.first || user.first_name, // TODO: Set this to the user's first name, considering both 'first' and 'first_name'. + lastName: user.last || user.last_name, // TODO: Set this to the user's last name, considering both 'last' and 'last_name'. + emailAddress: user.email || user.email_address, // TODO: Set this to the user's email address, considering both 'email' and 'email_address'. + phoneNumber: user.phone || user.phone_number, // TODO: Set this to the user's phone number, considering both 'phone' and 'phone_number'. + source: + typeof user.id === "string" + ? DataSource.DATA_SOURCE_1 + : DataSource.DATA_SOURCE_2, // Hint: Determine the source based on the ID type (string for DATA_SOURCE_1, number for DATA_SOURCE_2). + }; }