This repository can be used as a basic sturucture to start off with an Apollo GraphQL service. I have made sure most of the heavy-lifting is done, and you can start off by simply cloning the repo and writing business logic. Things I have taken care of:
- Typescript setup for type safety and Object Oriented Structure
- Dotenv for configuration using .env.* files
- Efficient multistage Docker build (rebuilds a layer only if required) & Docker-compose
- Dependency Injection using Inversify
- Hot Reload with debugger(for VS Code editor) using nodemon for fast development
- A basic structure for writing cron jobs using node-cron
- A simple directory structure in line with following convention:
- Controllers: Called
resolvers
in GraphQL terminology, contain application logic and passing user input data to service. - Services: The middleware between controller and repository. Gather data from controller, performs validation and business logic, and calling repositories for data manipulation.
- Repositories: Layer for interaction with models and performing DB operations.
- Models: A model is called
type
in GraphQL terminology, they are defined in schema files with relationships.
- Controllers: Called
Hasura is a framework that provides a GraphQL query interface over a postgre database, I have used Hasura as an ORM and datasource in this project. You can use any other ORM over any other database of your choice.
git clone https://github.com/subhankarshah/apollo-graphql-starter.git
cd apollo-graphql-starter
Install Node & Run
npm install
Deploy Hasura with one-click on Heroku with the free Postgres add-on:
Or Deploy Hasura locally using Docker-compose
- Install Docker
- Install Docker-compose
- Run the following command
docker-compose up -d graphql-engine
Create a simple schema via Hasura Console as shown in the demo below:
Make the following schema for testing this repo (TODO: add migration to automate this)
- Create a
user
schema - Create a
user
table, under this schema - Create an
id
field of type UUID - Create fields firstName, lastName, mobile, email of type Text
The following env varibles: NODE_ENV, HASURA_ADMIN_SECRET, HASURA_END_POINT Are set in docker-compose.yaml for Docker deployments OR in nodemon.json for local development
All other env varialbes are set in .env.{NODE_ENV} files
For hot reload without debugger, run
npm run dev
For hot reload without debugger(VS code), run the command below and attach debugger to node process
npm run dev:debug
Run following commands
docker-compose build
docker-compose up -d apollo-graphql-server