Skip to content

ericnograles/graphql-server-pure-js

Repository files navigation

REST in Peace: The Rise of GraphQL

Overview

With GraphQL starting to take off in popularity, the intent of this baseline is to provide an example for anyone who is considering GraphQL for their problem domain or is simply experimenting.

GraphQL provides an expressive, declarative way to define data. Combined with tools from Apollo, it provides an easier way to consume, mutate, and subscribe to data from an API. Buying into the GraphQL stack means no more confusion around:

  • How does the client dynamically get data it needs without one-off routes or janky query handling?
  • What is the best XHR client to use for data management?
  • How does one effectively cache data client side?
  • How does one effectively subscribe to data changes on the client side?

Instead of a traditionally imperative way to obtain data, using GraphQL largely follows a declarative model similar to React: the front-end is focused on the "what" versus the "how"

This Scaffold

This scaffold introduces a more "backend-centric" way of constructing a GraphQL Server. Specifically, the pattern of

Approach

Prerequisites

  1. Docker
  2. Visual Studio Code

Environment Variable Reference

Note: The docker-compose.yml defines all of these in relation to this app's Dockerfile. This is simply for reference if you want to run this locally.

Environment Variable Name Purpose Sample Value
DATABASE_URL The URI to your Postgres database postgres://postgres@127.0.0.1:61001/graphql-baseline
REDIS_URL The URI to your redis instance redis://127.0.0.1:61000
REDIS_USER_TOKENS_DB The redis DB index where to store JWT's 1
API_SECRET The secret with which to sign JWT's shhh_this_should_be_secure
RECREATE_SCHEMA If set to true, will drop and recreate all Sequelize Models' source tables true

Installation

  1. Clone this repo
  2. At the root of the repo, execute docker-compose build
  3. At the root of the repo, execute docker-compose up
  4. Browse to http://localhost:64002/explorer for the GraphiQL server running on Node
  5. Browse to http://localhost:64003 for the React SPA

Ongoing Development

This scaffold is completely containerized in Docker, meaning you do not have to worry about any local dependencies apart from Docker and VS Code.

Adding New Dependencies

Your container is the source of your code going forward. While it has a localized node_modules on both server and client to prevent platform-specific compilation problems (with things like bcrypt), you can still manage dependencies within and it will reflect on your local package.json.

To do this, you simply need to bash into the container by executing: docker exec -i -t reactjstampabay_graphql /bin/bash. This will open a terminal in the container running your code. You can then add dependencies as normal, using yarn add some_library.

To exit from the container's bash, simply execute exit

Debugging (Client)

  1. Browse to http://localhost:64003 for the React SPA
  2. Open Chrome DevTools and debug like a normal SPA
    • Note: Any changes to client/src will reflect on the Docker container

Debugging (Server)

  1. Open VS Code
  2. Execute the Attach to Nodemon task
  3. Place breakpoints in any files under server
    • Note: Any changes to server will reflect on the Docker container

Deploying to Heroku

This project fully supports deployment to a Heroku web dyno. The only items needed to be established are the environment variables listed above.

About

The source code for REST in Peace: The Rise of GraphQL talk given 10/3/17 by Eric Nograles

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published