Skip to content

Boilerplate for a Node.js Express Server with Apollo for GraphQL

Notifications You must be signed in to change notification settings

AnnaXWang/NodeApollo-Boilerplate

Repository files navigation

Node.js Express Server + Apollo Boilerplate

About

  • This repo is a work-in-progress to design a scalable and modular Node.js Express Server with Apollo (GraphQL)

Setup

  • Install NodeJS/npm: https://nodejs.org/en/download/

    • Linux: apt-get install nodejs
    • MacOS: brew install nodejs
    • Windows: direct download, or use Bash on Windows 10
  • Install Heroku CLI and create a Heroku account

      $ brew install heroku/brew/heroku
    
  • Install Postgres locally (Note: you may also have to install it globally)

      $ npm install pg --save
    
  • Optional: Install CRA globally (only if you want to create a react app from scratch)

      $ npm install -g create-react-app
    
  • Update the eslint version in the client/frontend to match the eslint in the backend

      $ cd frontend
      $ rm -rf node_modules
      $ npm uninstall babel-eslint
      $ npm uninstall eslint
      $ npm install
      $ cd frontend/node_modules/react-scripts
      $ rm -rf node_modules
      $ //go into frontend/node_modules/react-scripts/package.json and change the eslint dependency to be 5.9.0 and the babel-eslint dependency to be 10.0.1
      $ npm install //while still in the react-scripts directory
      $ cd ..
      $ npm uninstall babel-eslint@9.0.0
      $ npm uninstall eslint@5.9.0
      $ npm install babel-eslint@10.0.1
    
  • Install concurrently (https://www.npmjs.com/package/concurrently) to test in development

      $ npm install -g concurrently
    
  • Install Sequelize CLI globally if you don't already have it

      $ npm install -g sequelize-cli --save
    
  • Create a local Postgres database called "boilerplate_db"

      $ psql CREATE DATABASE boilerplate_db;
    
    • Ideally, you should not set up a username and password for your local database
    • 'postgres://localhost/boilerplate_db' should point to this database
    • Look at knexfile.js in the root directory to understand how the server accesses your local database
  • Run migrations and seed database

      $ sequelize db:migrate
      $ sequelize db:seed:all
    
  • If necessary to reset migrations and seeds

      $ sequelize db:migrate:undo
      $ sequelize db:seed:undo:all
    
  • Install node dependencies

      $ npm install && cd frontend && npm install
    
  • To run the linter (to be done before any commit, after removing all build folders in backend and frontend directories)

      $ npm run lint 
    
  • To run the web app locally

      $ npm run dev
    
  • To create a Heroku application on the command line and set up the database

      $ heroku create <name of application>
      $ heroku addons:create heroku-postgresql:hobby-dev
      $ heroku run bash
      $ sequelize -m
    
  • To run locally on Heroku at localhost:5000

      $ cd frontend && npm run build
      $ ..
      $ heroku local
    
  • To push to Heroku application

      $ git push heroku master
    
    • Note: Your first time pushing to heroku may require 'npm install babel' to be added to the build script in package.json

Query Examples

  • Example of queries the User model with arguments

      {
          user(username: "John") {
          username
          password
          userType{
            id
            isCandidate
            isReference
            isEmployer
          }
        }
      }
    

Mutation Examples

  • Example of mutations to add a User model

      mutation {
        addUser(username: "asdf", password: "asdf123123123123", email: "email901322@gmail.com", isEmployer: true) {
          token
        }
      }
    
      mutation {
        signIn(email:"email901322@gmail.com",  password: "asdf123123123123"){
          token
        }
      } 
    
      Mutation with inputs:
      
      mutation {
        addUser(input: { username: "asdfasdf", password: "asasdfasdfasdfasdf", email: "demo@gmail.com", isEmployer: true})
          {
            token
          }
      }
    

References

About

Boilerplate for a Node.js Express Server with Apollo for GraphQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published