├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── nodemon-debug.json ├── nodemon.json ├── ormconfig.json ├── package-lock.json ├── package.json ├── src ├── app.module.ts ├── main.ts ├── product.entity.ts ├── product.graphql ├── product.resolver.ts └── product.service.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json ├── tsconfig.json ├── tslint.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Installation 2 | 3 | ```bash 4 | $ npm install 5 | ``` 6 | 7 | ## Running the app 8 | 9 | ```bash 10 | 11 | # configure database 12 | $ edit file ormconfig.json to your database configuration 13 | 14 | # development 15 | $ npm run start 16 | 17 | # watch mode 18 | $ npm run start:dev 19 | 20 | # production mode 21 | $ npm run start:prod 22 | ``` -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /nodemon-debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "ts-node -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /ormconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "mysql", 3 | "host": "localhost", 4 | "port": 3306, 5 | "username": "root", 6 | "password": "root", 7 | "database": "nestgraphql", 8 | "synchronize": true, 9 | "logging": true, 10 | "entities": [ 11 | "src/**/*.entity.ts", 12 | "dist/**/*.entity.js" 13 | ] 14 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-mysql", 3 | "version": "0.0.0", 4 | "description": "description", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "build": "tsc -p tsconfig.build.json", 9 | "format": "prettier --write \"src/**/*.ts\"", 10 | "start": "ts-node -r tsconfig-paths/register src/main.ts", 11 | "start:dev": "nodemon", 12 | "start:debug": "nodemon --config nodemon-debug.json", 13 | "prestart:prod": "rimraf dist && npm run build", 14 | "start:prod": "node dist/main.js", 15 | "lint": "tslint -p tsconfig.json -c tslint.json", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^5.4.0", 24 | "@nestjs/core": "^5.4.0", 25 | "@nestjs/graphql": "^5.5.7", 26 | "@nestjs/typeorm": "^5.3.0", 27 | "apollo-server-express": "^2.4.8", 28 | "class-transformer": "^0.2.0", 29 | "class-validator": "^0.9.1", 30 | "graphql": "^14.1.1", 31 | "graphql-tools": "^4.0.4", 32 | "mysql": "^2.16.0", 33 | "reflect-metadata": "^0.1.12", 34 | "rimraf": "^2.6.2", 35 | "rxjs": "^6.2.2", 36 | "typeorm": "^0.2.14", 37 | "typescript": "^3.0.1" 38 | }, 39 | "devDependencies": { 40 | "@nestjs/testing": "^5.1.0", 41 | "@types/express": "^4.16.0", 42 | "@types/jest": "^23.3.1", 43 | "@types/node": "^10.7.1", 44 | "@types/supertest": "^2.0.5", 45 | "jest": "^23.5.0", 46 | "nodemon": "^1.18.3", 47 | "prettier": "^1.14.2", 48 | "supertest": "^3.1.0", 49 | "ts-jest": "^23.1.3", 50 | "ts-loader": "^4.4.2", 51 | "ts-node": "^7.0.1", 52 | "tsconfig-paths": "^3.5.0", 53 | "tslint": "5.11.0" 54 | }, 55 | "jest": { 56 | "moduleFileExtensions": [ 57 | "js", 58 | "json", 59 | "ts" 60 | ], 61 | "rootDir": "src", 62 | "testRegex": ".spec.ts$", 63 | "transform": { 64 | "^.+\\.(t|j)s$": "ts-jest" 65 | }, 66 | "coverageDirectory": "../coverage", 67 | "testEnvironment": "node" 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { GraphQLModule } from '@nestjs/graphql'; 3 | import { TypeOrmModule } from '@nestjs/typeorm'; 4 | import { ProductResolver } from './product.resolver'; 5 | import { ProductService } from './product.service'; 6 | import { ProductEntity } from './product.entity'; 7 | 8 | @Module({ 9 | imports: [ 10 | TypeOrmModule.forRoot(), 11 | TypeOrmModule.forFeature([ProductEntity]), 12 | GraphQLModule.forRoot({ 13 | typePaths: ['./**/*.graphql'], 14 | playground: true, 15 | }), 16 | ], 17 | providers: [ProductResolver, ProductService], 18 | }) 19 | export class AppModule { } 20 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import { ValidationPipe } from '@nestjs/common'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | app.useGlobalPipes(new ValidationPipe()); 8 | await app.listen(3000); 9 | } 10 | bootstrap(); 11 | -------------------------------------------------------------------------------- /src/product.entity.ts: -------------------------------------------------------------------------------- 1 | import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinColumn } from 'typeorm'; 2 | 3 | @Entity('products') 4 | export class ProductEntity { 5 | @PrimaryGeneratedColumn() 6 | id: string; 7 | 8 | @Column({ name: 'productName' }) 9 | productName: string; 10 | 11 | @Column({ name: 'productDescription', nullable: true }) 12 | productDescription: string; 13 | 14 | @Column({ name: 'price' }) 15 | price: number; 16 | 17 | @Column({ name: 'isInStock' }) 18 | isInStock: boolean; 19 | 20 | @Column({ name: 'image', length: 2083 }) 21 | image: string; 22 | } 23 | -------------------------------------------------------------------------------- /src/product.graphql: -------------------------------------------------------------------------------- 1 | type Product { 2 | id: ID! 3 | productName: String! 4 | productDescription: String 5 | price: Int! 6 | isInStock: Boolean! 7 | image: String! 8 | } 9 | 10 | type Query { 11 | products: [Product!] 12 | product(id: Int): Product 13 | } 14 | 15 | type Mutation { 16 | createProduct(productName: String!, productDescription: String, price: Int!, isInStock: Boolean, image: String ): Product 17 | updateProduct(id: ID!, productName: String!, productDescription: String, price: Int!, isInStock: Boolean, image: String): Product 18 | deleteProduct(id: ID!): Product 19 | } -------------------------------------------------------------------------------- /src/product.resolver.ts: -------------------------------------------------------------------------------- 1 | import { Resolver, Query, ResolveProperty, Args, Mutation } from '@nestjs/graphql'; 2 | import { ProductService } from './product.service'; 3 | import { userInfo } from 'os'; 4 | 5 | @Resolver() 6 | export class ProductResolver { 7 | constructor(private productService: ProductService) { } 8 | 9 | @Query() 10 | async products() { 11 | return await this.productService.getAll(); 12 | } 13 | 14 | @Query() 15 | async product(@Args('id') id: string) { 16 | return await this.productService.getById(id); 17 | } 18 | 19 | @Mutation() 20 | async createProduct( 21 | @Args('productName') productName: string, 22 | @Args('productDescription') productDescription: string, 23 | @Args('price') price: number, 24 | @Args('isInStock') isInStock: boolean, 25 | @Args('image') image: string) { 26 | const product = { productName, productDescription, price, isInStock, image }; 27 | return await this.productService.create(product); 28 | } 29 | 30 | @Mutation() 31 | async updateProduct( 32 | @Args('id') id: string, 33 | @Args('productName') productName: string, 34 | @Args('productDescription') productDescription: string, 35 | @Args('price') price: number, 36 | @Args('isInStock') isInStock: boolean, 37 | @Args('image') image: string) { 38 | const product = { productName, productDescription, price, isInStock, image }; 39 | return await this.productService.update(id, product); 40 | } 41 | 42 | @Mutation() 43 | async deleteProduct(@Args('id') id: string) { 44 | return await this.productService.delete(id); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/product.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, NotFoundException } from '@nestjs/common'; 2 | import { Repository } from 'typeorm'; 3 | import { ProductEntity } from './product.entity'; 4 | import { InjectRepository } from '@nestjs/typeorm'; 5 | 6 | @Injectable() 7 | export class ProductService { 8 | constructor(@InjectRepository(ProductEntity) private productRepository: Repository) { } 9 | 10 | async getAll() { 11 | return await this.productRepository.find(); 12 | } 13 | 14 | async getById(id: string) { 15 | const product = await this.productRepository.findOne({ where: { id } }); 16 | if (product) { 17 | return product; 18 | } else { 19 | throw new NotFoundException(); 20 | } 21 | } 22 | 23 | async create(data: any) { 24 | const product = this.productRepository.create(data); 25 | await this.productRepository.save(product); 26 | return product; 27 | } 28 | 29 | async update(id: string, data: any) { 30 | await this.productRepository.update({ id }, data); 31 | return this.productRepository.findOne({ id }); 32 | } 33 | 34 | async delete(id: string) { 35 | await this.productRepository.delete({ id }); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import * as request from 'supertest'; 3 | import { AppModule } from './../src/app.module'; 4 | 5 | describe('AppController (e2e)', () => { 6 | let app; 7 | 8 | beforeEach(async () => { 9 | const moduleFixture: TestingModule = await Test.createTestingModule({ 10 | imports: [AppModule], 11 | }).compile(); 12 | 13 | app = moduleFixture.createNestApplication(); 14 | await app.init(); 15 | }); 16 | 17 | it('/ (GET)', () => { 18 | return request(app.getHttpServer()) 19 | .get('/') 20 | .expect(200) 21 | .expect('Hello World!'); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es6", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./" 12 | }, 13 | "exclude": ["node_modules"] 14 | } 15 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": { 5 | "no-unused-expression": true 6 | }, 7 | "rules": { 8 | "quotemark": [true, "single"], 9 | "member-access": [false], 10 | "ordered-imports": [false], 11 | "max-line-length": [true, 150], 12 | "member-ordering": [false], 13 | "interface-name": [false], 14 | "arrow-parens": false, 15 | "object-literal-sort-keys": false 16 | }, 17 | "rulesDirectory": [] 18 | } 19 | --------------------------------------------------------------------------------