A simple graphql and apollo server API for JWT based authentication.
Contains config.js file where the configuration for MySQL database is defined which is required for connecting to the database.
Contains sequelize migrations models for company and user tables. These define parameters for when we run the initial create table command on our database.
Source files that have most of the server code. Migrations and Config are not inside src folder since they are only used once initially before running the code and not while the code is running.
Contains SQL models for company and user in respective files. User has a hook which has functions that run before certain type of queries.
beforeCreate
this hook is called when create query is being called. The hook contains logic to hash the password with a salt so that we don’t store un-encrypted password in database.
beforeUpdate
this hook is called when update query is being called on user table. Just as before it hashesh the updated password.
User.validPassword
is a class Methods which users bcrypt to compare the hash stored in db against a string to check if both are same.
User.associate
is one-to-one association with company table with employeeId foreign key.
The index.js
contains the code for connecting seqelize to database. It also takes all the models defined in the models folder, and it applies them to the “db” object. so if you have a model in the models folder, and it’s name is “User”, it will be found on the db object as db.User
Resolvers contain the functions that are called for respective query and mutaion. They take in 4 arguments
root
contains the result returned from the resolver on the parent field.args
arguments passed into the field in the query.context
object shared by all resolvers in a particular query.info
contains information about the execution state of the query.
The query
object in getUserList
is a dynamic object which changes values based on arguments passed to the query.All arguments are optional.All queries require an Authorization header with valid jwt token.
Schema is at the core of any GraphQL and defines the functionality available to the client applications that connect to it, also defines all the query and mutations the client can use along with their parameters and what data type they return.
There are two tables the user table which has employeeId as primary and foreign key and has all user details except for the company. The company table which has employeeId as primary and foreign key and a company column.
mutation {
registerUser(
email: "[email protected]"
password: "polo30"
firstName: "arnav"
lastName: "chauhan"
employeeId: 2
company: "wee"
) {
token
user {
firstName
lastName
email
}
}
}
mutation {
login(email: "[email protected]", password: "polo30") {
token
user {
firstName
lastName
email
}
}
}
search: firstName,lastName,employeeId
query myself {
getUserList(
pagination: { items: 2, page: 1 }
search: { employeeId: 2 }
sort: "employeeId"
) {
email
firstName
employeeId
company
}
}