GraphQL API w/ Express & PostgreSQL
A basic example of a graphQL API connected to PostgreSQL. Inspired by this Snipcart article, but refactored and modularized to simulate a more realistic project structure.
Setup
- Install all npm packages with
npm install
. - Create a new PostgreSQL database.
- Use the following queries to create three tables inside your new database (
team
,player
, andmatch
):
CREATE TABLE team (
id SERIAL PRIMARY KEY,
name VARCHAR (255)
);
CREATE TABLE player (
id SERIAL PRIMARY KEY,
first_name VARCHAR (255),
last_name VARCHAR (255),
team_id INT NOT NULL REFERENCES team (id)
);
CREATE TABLE match (
id SERIAL PRIMARY KEY,
date DATE NOT NULL DEFAULT CURRENT_DATE,
winner_team_id INT NOT NULL REFERENCES team (id),
loser_team_id INT NOT NULL REFERENCES team (id)
);
- Create a
.env
file in the root directory. Inside the file, typePG_URI=
, followed by your db URI (no quotes). (Alternately, you could replaceenv.process.PG_URI
in thedbConnect.js
file with your own db URI.) - Spin up the server with
npm start
. - Go to
http://localhost:3000/api
in your browser. - Use the GraphiQL interface to make query and mutation requests.