Skip to content
Open
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
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
# TypeGraphQL & TypeORM Example

How To Build a GraphQL API with TypeGraphQL and TypeORM. Read the [article](https://blog.logrocket.com/how-build-graphql-api-typegraphql-typeorm/).
> How To Build a GraphQL API with TypeGraphQL and TypeORM. Read the [article](https://blog.logrocket.com/how-build-graphql-api-typegraphql-typeorm/).

**How to use:**
forked from [rahmanfadhil/learn-typegraphql](https://github.com/rahmanfadhil/learn-typegraphql)

Differences:

- [node-config](https://github.com/lorenwest/node-config) used for configuration
- refactor file names
- update all deps
- add Docker+Compose configuration
- use postgres db in containers

## How to use

### Localhost

```bash
npm install
npm run start
```

This will start node server with database stored in sqlite3 file.

GraphQL playground at http://localhost:4000

### Docker

- Install [Docker](https://docs.docker.com/engine/install/)+[Compose](https://docs.docker.com/compose/install/)

```bash
docker-compose up -d
```

This will start node server with database and pg-admin in different containers.

GraphQL playground at http://localhost:4000

PG Admin GUI at http://localhost:4100 use `admin/admin` as `login/password`

## Query examples

### Hello world

```graphql
{ hello }
```
$ npm install
$ npm run start

### create Book

```graphql
mutation {
createBook(data:{
title: "Megabook",
author: "Megamind"
}) {
id, title, author, isPublished
}
}
```

### get all books

```graphql
{ books
{ id, title, author }
}
```
14 changes: 14 additions & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"app": {
"host": "APP_HOST",
"port": "APP_PORT"
},
"database": {
"type": "DATABASE_TYPE",
"host": "DATABASE_HOST",
"port": "DATABASE_PORT",
"username": "DATABASE_USERNAME",
"password": "DATABASE_PASSWORD",
"database": "DATABASE_NAME"
}
}
12 changes: 12 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app": {
"host": "0.0.0.0",
"port": 4000
},
"database": {
"type": "sqlite",
"database": "./db.sqlite3",
"entities": ["./src/models/*.ts"],
"synchronize": true
}
}
64 changes: 64 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
version: "3.6"

services:
graphql-gateway:
image: node
working_dir: /usr/webapp
depends_on:
postgres:
condition: service_healthy
pg-admin:
condition: service_started
volumes:
- "./src:/usr/webapp/src:ro"
- "./package.json:/usr/webapp/package.json:ro"
# - "./ormconfig.json:/usr/webapp/ormconfig.json:ro"
- "./ormconfig.ts:/usr/webapp/ormconfig.ts:ro"
- "./tsconfig.json:/usr/webapp/tsconfig.json:ro"
- "./config:/usr/webapp/config:ro"
- "./package-lock.json:/usr/webapp/package-lock.json"
- "./entrypoint.sh:/usr/webapp/entrypoint.sh"
environment:
DATABASE_TYPE: postgres
DATABASE_HOST: postgres
DATABASE_PORT: 5432
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgrespassword
DATABASE_NAME: postgres
ports:
- "4000:4000"
entrypoint: "/bin/sh entrypoint.sh npm start"

# Postgres
postgres:
image: postgres
healthcheck:
test: pg_isready -U postgres
interval: 10s
timeout: 3s
retries: 10
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgrespassword
volumes:
- "./docker/postgres/init:/docker-entrypoint-initdb.d:ro"
ports:
- "4153:5432"

# Postgres web GUI
pg-admin:
image: dpage/pgadmin4:5.0
depends_on:
postgres:
condition: service_healthy
environment:
PGADMIN_DEFAULT_EMAIL: admin
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- 4100:80
volumes:
- "pgadmin-data:/var/lib/pgadmin"

volumes:
db_data:
pgadmin-data:
8 changes: 8 additions & 0 deletions docker/postgres/init/0.0.init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Init schema and user
-- CREATE ROLE "camunda" LOGIN PASSWORD 'changeme';
-- CREATE SCHEMA "camunda";
-- ALTER SCHEMA "camunda" OWNER TO "camunda";
-- ALTER ROLE "camunda"
-- SET search_path TO "camunda";
-- ALTER DEFAULT PRIVILEGES IN SCHEMA "camunda"
-- GRANT ALL ON TABLES TO "camunda";
10 changes: 10 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

echo "Entrypoint wrapper script"
echo "-------------------------"
# Run npm install
npm install
echo "-------------------------"

# Run command
$@
15 changes: 15 additions & 0 deletions ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";
import { models as entities } from "./src/models";
import config from "config";
import { SqliteConnectionOptions } from "typeorm/driver/sqlite/SqliteConnectionOptions";

const connectionOptions: PostgresConnectionOptions | SqliteConnectionOptions = {
...config.get<PostgresConnectionOptions | SqliteConnectionOptions>(
"database"
),
entities,
};

console.log(JSON.stringify({ connectionOptions }));

export default connectionOptions;
Loading