Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GQL home assignment Nerdery #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
24 changes: 15 additions & 9 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
};
}