Homework assessment for Favs
This project will assume you have Node.js
, PostgreSQL
setup on your computer previously. We will need at least Node 18 for the project to run properly.
- Clone the repository:
git clone https://github.com/m1771vw/favs-hw.git
cd favs-hw
- Use npm to install project dependencies listed in
package.json
:
npm install
- Create an
.env
file. Usually we won't keep this here, but for demonstration purposes will give you the .env example. Please replace username and password with you Postgres information
DATABASE_URL="postgresql://<username>:<password>@localhost:5432/favsdb?schema=public"
- We're going to assume you have Postgres set up with default options. There'll be a script to be run to create a database and a user for that database.
If your postgres user isn't postgres, please update the -U tag
psql -U postgres -d postgres -h localhost -p 5432 -f ./scripts/create_database.sql
- We need to use Prisma to set up your database.
npx prisma migrate dev --name first_migration
- Now we can start the project
npm run start
- You can now navigate to here to see the GraphQL Interface. It's mapped to localhost:3003 so if 3003 is being used please change it to something else.
Example queries and mutations can be found in scripts/graphql_scripts.js
.
Here are some examples from that file
mutation {
createUser(data: {
first_name: "User1FirstName",
last_name: "User1LastName",
phone_number: "1234567890",
username: "user1"
}) {
id
first_name
last_name
phone_number
username
}
}
mutation {
createUser(data: {
first_name: "User2FirstName",
last_name: "User2LastName",
phone_number: "9876543210",
username: "user2"
}) {
id
first_name
last_name
phone_number
username
}
}
query {
users {
id
first_name
last_name
phone_number
username
created_at
updated_at
}
}
query {
user(id: "<user_id>") {
id
first_name
last_name
phone_number
username
created_at
updated_at
}
}
Here are some for Friend Request. This will require you to get the user id from the created users earlier.
mutation {
createFriendRequest(data: {
user_requestor_id: "<input user id here>",
user_requested_id: "<input different id here>",
status: "pending"
}) {
id
user_requestor_id
user_requested_id
status
}
}
You can accept the request like this. You can change response to REJECTED as well.
mutation {
responseFriendRequest(
friendRequestId: "<insert friend request id here>"
response: ACCEPTED
) {
id
user_requestor_id
user_requested_id
status
created_at
updated_at
}
}
You can check friendship of a user like this
query {
friends(userId: "<input user id here>") {
id
first_name
last_name
phone_number
username
created_at
updated_at
}
}
- Design of friend list table
- Picking single rows over a friend list array
- Array would be simpler at the beginning, but can run into problems once friend list grows
- Unable to add more data about it
- Want to add some extra fields, like when they first became friends
- Picking single rows over a friend list array
- Keeping friend request table separate
- Thought about having a status on friend list table but ultimately wanted to seaprate it so I can have a history and also see who sent friend request to whom
- Choosing GraphQL library
- Picked Apollo because itβs pretty standard
- Lots of documentation
- Good for scaling , wide range of features
- More familiar with the structure as well
- How to store friendships
- Bidirectional or single row
- Two entries:
- Pro simple, easy to query
- Con: Increased storage
- Single entry:
- Pro: Less storage
- Con: Complex queries, could take more time
- Going with bidirectional: Storage is cheap, speed is better