Skip to content

Nest.js Hybrid Application (HTTP server with microservice listeners) with GraphQL (schema first), Prisma, MySQL (easily replaceble), MongoDB, Jest, Docker

Notifications You must be signed in to change notification settings

josephgodwinkimani/nestjs-graphql-prisma

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Simple Blog API ( CRUD users, users CRUD posts, users upload files )

Prisma GraphQL schema-first 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 and MongoDB as no-sql database for constantly changing or growing data such as posts.

To connect other microservices uncomment examples in main.ts, replace jest with vitest and to use a different database, check the Prisma docs 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. Run multi-container Docker applications
# run mongodb, mongo express container
$ docker-compose -f docker-compose-mongo.yml up -d
# run mysql, phpmyadmincontainer
$ docker-compose up -d
  1. Install dependencies: npm install
  2. Generate TypeScript type definitions for the GraphQL schema: npm run generate:typings
  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

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Graphql Playground

When the application is running, you can go to http://localhost:3001/graphql to access the GraphQL Playground. See here 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
  }
}

Related Projects

About

Nest.js Hybrid Application (HTTP server with microservice listeners) with GraphQL (schema first), Prisma, MySQL (easily replaceble), MongoDB, Jest, Docker

Topics

Resources

Stars

Watchers

Forks