Skip to content

Add code for chapter 9 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# rename file to .env
DATABASE_URL=postgres://postgres:postgres@localhost:5432/hackernews-db
25 changes: 25 additions & 0 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

name: deploy-hackernews-app-heroku

on:
push:
branches:
- master
- main

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm ci
- name: Run production migration
run: npm run migrate:deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
- uses: akhileshns/heroku-deploy@v3.12.12
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
heroku_email: ${{ secrets.HEROKU_EMAIL }}

5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
# Keep environment variables out of version control
.env
.idea
.vscode
dist
dist
.idea
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ Tutorial for HowToGraphQL with Typescript, Apollo-Server, Nexus and Prisma.

### Installation

1. Run `npm install`
2. Run `npx prisma generate`
3. Run `npm dev`
1. Install dependencies: `npm install`
2. Start a PostgreSQL database with docker using: `docker-compose up -d`.
- If you have a local instance of PostgreSQL running, you can skip this step.
3. Create a `.env` file and add the `DATABASE_URL` environment variable with a [PostgreSQL connection string](https://www.prisma.io/docs/concepts/database-connectors/postgresql#connection-details).
- The `.env.example` file is provided as reference.
4. Apply database migrations: `npx prisma migrate dev`
5. Start the project: `npm dev`


_Note:_ There's no need to run migrations on the database because the SQLite Database is added to version control for convenience.
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'
services:

# Docker connection string for local machine: postgres://postgres:postgres@localhost:5432/

postgres:
image: postgres:13.5
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- postgres:/var/lib/postgresql/data
ports:
- '5432:5432'

volumes:
postgres:

20 changes: 14 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --transpile-only --no-notify --exit-child src/index.ts",
"generate": "ts-node --transpile-only src/schema",
"prettier-format": "prettier 'src/**/*.ts' --write"
"prettier-format": "prettier 'src/**/*.ts' --write",
"migrate:deploy": "prisma migrate deploy",
"build": "prisma generate && npm run generate && tsc",
"start": "node dist/src/index.js"
},
"keywords": [],
"author": "",
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
7 changes: 0 additions & 7 deletions prisma/migrations/20211116205624_init/migration.sql

This file was deleted.

26 changes: 0 additions & 26 deletions prisma/migrations/20211120093350_user/migration.sql

This file was deleted.

13 changes: 0 additions & 13 deletions prisma/migrations/20211214080826_add_vote_relation/migration.sql

This file was deleted.

44 changes: 44 additions & 0 deletions prisma/migrations/20220214215856_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateTable
CREATE TABLE "Link" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"description" TEXT NOT NULL,
"url" TEXT NOT NULL,
"postedById" INTEGER,

CONSTRAINT "Link_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "_Votes" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "_Votes_AB_unique" ON "_Votes"("A", "B");

-- CreateIndex
CREATE INDEX "_Votes_B_index" ON "_Votes"("B");

-- AddForeignKey
ALTER TABLE "Link" ADD CONSTRAINT "Link_postedById_fkey" FOREIGN KEY ("postedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_Votes" ADD FOREIGN KEY ("A") REFERENCES "Link"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_Votes" ADD FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
provider = "postgresql"
6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
datasource db {
provider = "sqlite"
url = "file:./dev.db"
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model Link {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
description String
url String
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { ApolloServer } from "apollo-server";
import { ApolloServerPluginLandingPageLocalDefault } from "apollo-server-core";

import { schema } from "./schema";

import { context } from "./context";

export const server = new ApolloServer({
schema,
context,
introspection: true,
plugins: [ApolloServerPluginLandingPageLocalDefault()],
});

const port = 3000;
const port = process.env.PORT || 3000;

server.listen({ port }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
Expand Down
3 changes: 1 addition & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { makeSchema } from "nexus";
import { join } from "path";
import * as types from "./graphql";


export const schema = makeSchema({
types,
outputs: {
Expand All @@ -13,4 +12,4 @@ export const schema = makeSchema({
module: join(process.cwd(), "./src/context.ts"),
export: "Context",
},
});
});