This project contains an API for a fake book store called Athenaeum.
It showcases a GraphQL API compliant with the Relay GraphQL server spec.
Setting up this project involves the following:
The data for the GraphQL API in this project is resolved from a PostgreSQL database running as a Docker container.
If you don't already have Docker installed, you can download Docker using the link below:
https://docs.docker.com/get-docker/
With Docker installed, let's pull down the PostgreSQL Docker image with this command:
Before we spin up a new postgres Docker container, let's first clone this project and then install its dependencies with:
Now that the postgres database is setup, we can spin up the Express server provided by this project.
This can be done via the following command:
To use the
The
This project ships with GraphiQL allowing you to write and execute queries via a web interface.
If the Express server is up, we can head over to
The
Expected arguments:
The
Expected Arguments:
The
#Project Stack:
- GraphQL API
- Express API
- PostgreSQL
- Docker
#Features:
- Relay GraphQL API featuring:
- Global Object Identification
- Cursor Connections
- Dataloaders
- Authentication via salted hashed password
- Authorization via signed JWT
- Database data mocking with Faker
#Table of Contents
#Setup
- Install Docker
- Pull PostgreSQL Docker Image
- Setup Database
- Start Express server
- Register User
- Login User
#Install Docker
#Pull PostgreSQL Docker Image
Now we can use this image to spin up a PostgreSQL Docker container.docker pull postgres
#Setup Database
With the project cloned and it's dependencies installed we'll next create a newnpm install
.env
file in the project root with the following contents:
The project includes aPOSTGRES_USER=root POSTGRES_PASSWORD=example_password POSTGRES_DB=athenaeum HMAC_SECRET_KEY=abcd1234
.env-example
file which can be used as a reference.
The environment variables contained in the .env
file will be used to provision a new postgres Docker container among other uses.
Now we can proceed with setting up the database - let's run the following command to do this:
This command does three things:npm run db:setup
- Spins up a postgres database inside a Docker container
- Inserts tables into the postgres database
- Populates database tables with some mock data
users
books
authors
genres
book_authors
book_genres
book_format
genre_type
#Start Express Server
This will spin up an Express server exposing three REST endpoints:npm run start
/register
- endpoint to register new user account/login
- endpoint to login as particular user/graphql
- endpoint to send GraphQL queries to
#Register User
/graphql
endpoint we first need to register as a new user and then login as that user.
See the register endpoint documentation for more details.
Once a new user account has been registered, we can proceed to login as that user.
#Login User
/login
endpoint is used to login with a set of user credentials and responds with a JSON Web Token, or JWT for short.
See the login endpoint documentation for more details.
Once a JWT has been obtained via the /login
endpoint, we can proceed to query the GraphQL API endpoint /graphql
.
#GraphQL API
http://localhost:4000/graphql
to start querying the GraphQL API.
To use the GraphiQL interface, we'll need to create a new cookie in the browser called Authorization
with the value of the JWT we acquired by logging in.
#Endpoints
#register
/register
endpoint is used to register a new user account.
Method: POST
Expected HTTP headers:
Content-Type | 'application/json' |
---|
Field | Type | Required |
---|---|---|
email | string | true |
firstName | string | true |
lastName | string | true |
password | string | true |
confirmPassword | string | true |
#login
/login
endpoint is used to login with a set of user credentials and responds with a JSON Web Token, abbreviated to JWT.
Method: POST
Expected HTTP headers:
Content-Type | 'application/json' |
---|
Field | Type | Required |
---|---|---|
email | string | true |
password | string | true |
#graphql
/graphql
endpoint accepts GraphQL requests.
Method: POST
HTTP Headers:
Content-Type | 'application/json' |
---|---|
Authorization | 'Bearer xxx.xxx.xxx' |
Some of the tech I worked with on this project: