-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade nest prisma graphql sample into hybrid blog api
- Loading branch information
1 parent
badf833
commit d96f617
Showing
52 changed files
with
2,862 additions
and
9,850 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"recommendations": [ | ||
"graphql.vscode-graphql-syntax", | ||
"prisma.prisma", | ||
"dbaeumer.vscode-eslint" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
``` | ||
|
||
**List all Existing Users** | ||
|
||
``` | ||
query { | ||
users { | ||
id | ||
name | ||
} | ||
} | ||
``` | ||
|
||
**Retrieve an Existing User** | ||
|
||
``` | ||
query { | ||
user(id: "3f234751-1819-4d96-ad0b-29840796806d") { | ||
id | ||
name | ||
} | ||
} | ||
``` | ||
|
||
**Update an Existing User** | ||
|
||
``` | ||
mutation { | ||
updateUser(input: { id: "3f234751-1819-4d96-ad0b-29840796806d", name: "James Koome", email: "josephgodwink90@aol.com" }) { | ||
id | ||
name | ||
} | ||
} | ||
``` | ||
|
||
**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 | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.