Skip to content

Boilerplate and tooling for authoring data API backends with Node.js and GraphQL

License

Notifications You must be signed in to change notification settings

ht22pt/nodejs-api-starter

 
 

Repository files navigation

Node.js API Starter Kit  

Boilerplate and tooling for authoring data API backends with Node.js and GraphQL. It's meant to be paired with a web and/or mobile application project such as React Starter Kit.

This project is maintained with support from Hiring

✓ Cross-platform development on macOS, Windows or Linux inside Docker
GraphQL boilerplate, everything needed to get started building a GraphQL API service or gateway
PostgreSQL database schema boilerplate and migration tools (see tools, migrations)
✓ Authentication and authorization via Passport.js (see src/passport.js, src/routes/account.js)
✓ Session and cache management with Redis and DataLoader (see stop using JWT for sessions)
✓ Email templates for sending transactional email (see src/emails, src/email.js)
24/7 community support on Gitter + premium support on Skype (book a session)


This project was bootstraped with Node.js API Starter Kit (support).

GraphQL Demo
https://graphql-demo.kriasoft.com

Directory Layout

.
├── /build/                     # The compiled output (via Babel)
├── /config/                    # Configuration files (for Docker containers etc.)
├── /locales/                   # Localization resources (i18n)
├── /migrations/                # Database schema migrations
├── /seeds/                     # Scripts with reference/sample data
├── /src/                       # Node.js application source files
│   ├── /emails/                # Handlebar templates for sending transactional email
│   ├── /routes/                # Express routes, e.g. /login/facebook
│   ├── /schema/                # GraphQL schema, types, fields and mutations
│   │   ├── /Node.js            # Relay's "node" definitions
│   │   ├── /User.js            # User related top-level fields and mutations
│   │   ├── /UserType.js        # User type, representing a user account (id, emails, etc.)
│   │   ├── /...                # etc.
│   │   └── /index.js           # Exports GraphQL schema object
│   ├── /app.js                 # Express.js application
│   ├── /DataLoaders.js         # Data access utility for GraphQL /w batching and caching
│   ├── /db.js                  # Database access and connection pooling (via Knex)
│   ├── /email.js               # Client utility for sending transactional email
│   ├── /passport.js            # Passport.js authentication strategies
│   ├── /redis.js               # Redis client
│   └── /server.js              # Node.js server (entry point)
├── /test/                      # Unit, integration and load tests
├── /tools/                     # Build automation scripts and utilities
├── docker-compose.yml          # Defines Docker services, networks and volumes
├── docker-compose.override.yml # Overrides per developer environment (not under source control)
├── Dockerfile                  # Commands for building a Docker image for production
└── package.json                # The list of project dependencies

Prerequisites

  • Docker Community Edition v17 or higher
  • VS Code editor (preferred) + Project Snippets, EditorConfig, ESLint and Flow plug-ins.

Getting Started

Just clone the repo and run docker-compose up:

git clone https://github.com/kriasoft/nodejs-api-starter.git example-api
cd example-api                  # Change current directory to the newly created one
docker-compose up               # Launch Docker containers with the Node.js API app running inside
yarn docker-db-seed             # Seed the database with some test data

The API server must become available at http://localhost:8080/graphql (live demo).

Once the Docker container named api is started, the Docker engine executes node tools/run.js command that installs Node.js dependencies, migrates database schema to the latest version, compiles Node.js app from source files (see src) and launches it with "live reload" on port 8080.

If you need to manually rollback and re-apply the latest database migration file, run the following:

yarn docker-db-rollback         # Rollbacks the latest migration
yarn docker-db-migrate          # Migrates database to the latest version (see /migrates folder)
yarn docker-db-seed             # Seeds database with test data (see /seeds folder)

In order to open a shell from inside the running "api" container, run:

docker-compose exec api /bin/sh

Similarly, if you need to open a PostgreSQL shell (psql), execute this command:

docker-compose exec db psql <db> -U postgres

For the full list of automation scripts available in this project, please reffer to "scripts" section in the package.json file and the tools folder.

Testing

yarn lint                       # Find problematic patterns in code
yarn check                      # Check source code for type errors
yarn docker-test                # Run unit tests once inside a Docker container
yarn docker-test-watch          # Run unit tests in watch mode inside a Docker container

Debugging

In order to run the app with V8 inspector enabled, simply replace node tools/run.js with node --inspect=0.0.0.0:9229 tools/run.js in either docker-compose.yml file or, even better, in docker-compose.override.yml. Then restart the app (docker-compose up) and attach your debugger to 127.0.0.1:9230 (see .vscode/launch.json).

Keeping Up-to-Date

If you keep the original Git history after clonning this repo, you can always fetch and merge the recent updates back into your project by running:

git remote add nodejs-api-starter https://github.com/kriasoft/nodejs-api-starter.git
git checkout master
git fetch nodejs-api-starter
git merge nodejs-api-starter/master
docker-compose up

NOTE: Try to merge as soon as the new changes land on the master branch in Node.js API Starter repository, otherwise your project may diverse too much from the base/upstream repo.

Deployment

Customize the deployment script found in tools/publish.js if needed. Then whenever you need to deploy your app to a remote server simply run:

node tools/publish <host>       # where <host> is the name of your web server (see ~/.ssh/config)

Not sure where to deploy your app? DigitalOcean is a great choice in many cases (get $10 credit)

Contributing

Anyone and everyone is welcome to contribute. Start by checking out the list of open issues marked help wanted. However, if you decide to get involved, please take a moment to review the guidelines.

Books and Tutorials

Docker in Action You Don't Know JS JavaScript Ninja Effective JavaScript NodeSchool.io

Related Projects

  • GraphQL.js — The JavaScript reference implementation for GraphQL
  • DataLoader — Batching and caching for GraphQL data access layer
  • React Starter Kit — Isomorphic web app boilerplate (React, Node.js, Babel, Webpack, CSS Modules)
  • React Static Boilerplate — Single-page application (SPA) starter kit (React, Redux, Webpack, Firebase)
  • Membership Database — SQL schema boilerplate for user accounts, profiles, roles, and auth claims

License

Copyright © 2016-present Kriasoft. This source code is licensed under the MIT license found in the LICENSE.txt file.


Made with ♥ by Konstantin Tarkus (@koistya, blog) and contributors

About

Boilerplate and tooling for authoring data API backends with Node.js and GraphQL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 80.0%
  • HTML 18.7%
  • Other 1.3%