Skip to content

Commit

Permalink
upgrade nest prisma graphql sample into hybrid blog api
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgodwinkimani committed Dec 7, 2023
1 parent badf833 commit d96f617
Show file tree
Hide file tree
Showing 52 changed files with 2,862 additions and 9,850 deletions.
13 changes: 13 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_PROVIDER=mysql
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_USERNAME=root
DATABASE_PASSWORD=mysql
DATABASE_NAME=graphql
DATABASE_URL="${DATABASE_PROVIDER}://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@localhost:${DATABASE_PORT}/${DATABASE_NAME}"
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
46 changes: 27 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
# dependencies
# compiled output
/dist
/node_modules

# IDE
/.idea
/.awcache
/.vscode

# misc
npm-debug.log
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# example
/quick-start
# OS
.DS_Store

# tests
/test
# Tests
/coverage
/.nyc_output

# dist
/dist

# database
prisma/dev.db
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# secrets
.env
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"graphql.vscode-graphql-syntax",
"prisma.prisma",
"dbaeumer.vscode-eslint"
]
}
179 changes: 174 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,183 @@
### Prisma GraphQL schema first sample
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
</p>

This sample project uses sqlite as the relational database. To use a different database, check the [Prisma docs](https://www.prisma.io/docs/getting-started).
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->

### Simple Blog API ( CRUD users, users CRUD posts )

> [Prisma](https://docs.nestjs.com/recipes/prisma) [GraphQL schema-first](https://docs.nestjs.com/graphql/quick-start#schema-first) [Hybrid application](https://docs.nestjs.com/faq/hybrid-application)
This hybrid project uses graphql API query language for clean responses, TCP transport layer for microservice, `@nestjs/testing` which uses jest for unit testing and mysql as the relational database.

To connect other microservices uncomment examples in `app.module`, replace jest with vitest and to use a different database, check the [Prisma docs](https://www.prisma.io/docs/getting-started) e.g.

*to use CockroachDB*

```
// schema.prisma
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
}
```

```
// docker-compose.yml
cockroachdb:
image: cockroachdb/cockroach
restart: always
ports:
- "26257:26257"
- "8080:8080"
command: start-single-node --cluster-name=node1 --logtostderr=WARNING --log-file-verbosity=WARNING --insecure
environment:
- COCKROACH_USER=${DATABASE_USER}
- COCKROACH_PASSWORD=${DATABASE_PASSWORD}
```

### Installation

1. Install dependencies: `npm install`
2. Generate TypeScript type definitions for the GraphQL schema: `npm run generate:typings`
3. Create sqlite database and create tables: `npx prisma db push`
4. Start server: `npm run start:dev`
3. Generate a type-safe client to interact with your database: `npm run prisma:gen`
4. Create mariadb/mysql database and create tables: `npm run prisma:push`
5. Start server: `npm run start:dev`

### Graphql Playground

When the application is running, you can go to [http://localhost:3000/graphql](http://localhost:3000/graphql) to access the GraphQL Playground. See [here](https://docs.nestjs.com/graphql/quick-start#playground) for more.
When the application is running, you can go to [http://localhost:3000/graphql](http://localhost:3000/graphql) to access the GraphQL Playground. See [here](https://docs.nestjs.com/graphql/quick-start#playground) for more.


**Create a New User**

```
mutation {
createUser(input: { name: "Godwin Kimani", email: "josephgodwink90@aol.com"}) {
id
name
email
}
}
```

**List all Existing Users**

```
query {
users {
id
name
email
}
}
```

**Retrieve an Existing User**

```
query {
user(id: "3f234751-1819-4d96-ad0b-29840796806d") {
id
name
email
}
}
```

**Update an Existing User**

```
mutation {
updateUser(input: { id: "3f234751-1819-4d96-ad0b-29840796806d", name: "James Koome", email: "josephgodwink90@aol.com" }) {
id
name
email
}
}
```

**Create a New Post**

```
mutation {
createPost(input: { title: "Example Title", text: "Example Content", authorId: "3f234751-1819-4d96-ad0b-29840796806d"}) {
id
title
text
}
}
```

**List all Existing Posts**

```
query {
posts {
id
title
text
isPublished
author {
name
}
# Add other fields as needed
}
}
```

**Retrieve a Single Post**

```
query {
post(id: "6c248661-43a7-4b77-9e4d-11978418fc3e") {
id
title
text
author {
name
}
}
}
```

**Update an Existing Post**

```
mutation {
updatePost(input: { id: "265bb380-ebeb-41e3-8670-32eec5c5fa7c", title: "Post on A.I.", text: "Yes Other Example Content", isPublished: true }) {
id
title
text
isPublished
}
}
```

**Delete an Existing Post**

```
mutation {
deletePost(id: "265bb380-ebeb-41e3-8670-32eec5c5fa7c") {
id
}
}
```
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3.8'

services:
mysql:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_USER: ${DATABASE_USERNAME}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql

prisma:
image: prismagraphql/prisma
restart: always
ports:
- "4466:4466"
environment:
DATABASE_URL: mysql://$DATABASE_USERNAME:$DATABASE_PASSWORD@mysql:3306/$DATABASE_NAME
depends_on:
- mysql

phpmyadmin:
image: phpmyadmin:5.2.0
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8081:80"
networks:
mysql-phpmyadmin:
aliases:
- mysql

volumes:
mysql-data:
2 changes: 1 addition & 1 deletion generate-typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { join } from 'path';
const definitionsFactory = new GraphQLDefinitionsFactory();
definitionsFactory.generate({
typePaths: ['./src/**/*.graphql'],
path: join(process.cwd(), 'src/graphql.schema.ts'),
path: join(process.cwd(), 'src/graphql.ts'),
outputAs: 'class',
});
5 changes: 4 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": ["**/*.graphql"]
"deleteOutDir": true
}
}
Loading

0 comments on commit d96f617

Please sign in to comment.