├── .prettierrc ├── nodemon.json ├── nest-cli.json ├── tsconfig.build.json ├── src ├── cats │ ├── cats.providers.ts │ ├── dto │ │ └── create-cat.dto.ts │ ├── inputs │ │ └── cat.input.ts │ ├── cats.module.ts │ ├── cats.service.spec.ts │ ├── cat.entity.ts │ ├── cats.resolver.ts │ └── cats.service.ts ├── app.service.ts ├── main.ts ├── database │ ├── database.module.ts │ └── database.providers.ts ├── app.controller.ts ├── app.module.ts └── app.controller.spec.ts ├── nodemon-debug.json ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── tsconfig.json ├── tslint.json ├── .gitignore ├── schema.gql ├── package.json └── README.md /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["dist"], 3 | "ext": "js", 4 | "exec": "node dist/main" 5 | } 6 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /src/cats/cats.providers.ts: -------------------------------------------------------------------------------- 1 | import { Cat } from './cat.entity'; 2 | 3 | export const catsProviders = [ 4 | { 5 | provide: 'CATS_REPOSITORY', 6 | useValue: Cat, 7 | }, 8 | ]; 9 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello Rodrigo!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(4000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/database/database.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { databaseProviders } from './database.providers'; 3 | 4 | @Module({ 5 | providers: [...databaseProviders], 6 | exports: [...databaseProviders], 7 | }) 8 | export class DatabaseModule {} 9 | -------------------------------------------------------------------------------- /src/cats/dto/create-cat.dto.ts: -------------------------------------------------------------------------------- 1 | import { Field, ObjectType, ID, Int } from 'type-graphql'; 2 | 3 | @ObjectType() 4 | export class CreateCatDto { 5 | @Field(() => ID) 6 | id: number; 7 | 8 | @Field() 9 | name: string; 10 | 11 | @Field(() => Int) 12 | age: number; 13 | 14 | @Field() 15 | breed: string; 16 | } 17 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/cats/inputs/cat.input.ts: -------------------------------------------------------------------------------- 1 | import { Int, Field, InputType, ID } from 'type-graphql'; 2 | 3 | @InputType() 4 | export class CatInput { 5 | @Field(() => ID, { nullable: true }) 6 | readonly id?: number; 7 | @Field() 8 | readonly name: string; 9 | 10 | @Field(() => Int) 11 | readonly age: number; 12 | 13 | @Field() 14 | readonly breed: string; 15 | } 16 | -------------------------------------------------------------------------------- /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 | "incremental": true 13 | }, 14 | "exclude": ["node_modules"] 15 | } 16 | -------------------------------------------------------------------------------- /src/cats/cats.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { CatsService } from './cats.service'; 3 | import { catsProviders } from './cats.providers'; 4 | import { DatabaseModule } from '../database/database.module'; 5 | import { CatsResolver } from './cats.resolver'; 6 | @Module({ 7 | imports: [DatabaseModule], 8 | providers: [CatsService, CatsResolver, ...catsProviders], 9 | }) 10 | export class CatsModule {} 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { GraphQLModule } from '@nestjs/graphql'; 5 | import { CatsModule } from './cats/cats.module'; 6 | 7 | @Module({ 8 | imports: [ 9 | CatsModule, 10 | GraphQLModule.forRoot({ 11 | autoSchemaFile: 'schema.gql', 12 | }), 13 | ], 14 | controllers: [AppController], 15 | providers: [AppService], 16 | }) 17 | export class AppModule {} 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json -------------------------------------------------------------------------------- /src/cats/cats.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { CatsService } from './cats.service'; 3 | 4 | describe('CatsService', () => { 5 | let service: CatsService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [CatsService], 10 | }).compile(); 11 | 12 | service = module.get(CatsService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/cats/cat.entity.ts: -------------------------------------------------------------------------------- 1 | import { Table, Model, Column } from 'sequelize-typescript'; 2 | import { IDefineOptions } from 'sequelize-typescript/lib/interfaces/IDefineOptions'; 3 | 4 | const tableOptions: IDefineOptions = { 5 | tableName: 'cats', 6 | } as IDefineOptions; 7 | 8 | @Table(tableOptions) 9 | export class Cat extends Model { 10 | @Column({ 11 | primaryKey: true, 12 | autoIncrement: true, 13 | }) 14 | id: number; 15 | 16 | @Column 17 | name: string; 18 | 19 | @Column 20 | age: number; 21 | 22 | @Column 23 | breed: string; 24 | } 25 | -------------------------------------------------------------------------------- /src/database/database.providers.ts: -------------------------------------------------------------------------------- 1 | import { Sequelize } from 'sequelize-typescript'; 2 | import { Cat } from '../cats/cat.entity'; 3 | 4 | export const databaseProviders = [ 5 | { 6 | provide: 'SEQUELIZE', 7 | useFactory: async () => { 8 | const sequelize = new Sequelize({ 9 | dialect: 'postgres', 10 | host: 'localhost', 11 | port: 5432, 12 | username: 'postgres', 13 | password: 'postgres', 14 | database: 'nest', 15 | }); 16 | sequelize.addModels([Cat]); 17 | await sequelize.connectionManager; 18 | return sequelize; 19 | }, 20 | }, 21 | ]; 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /schema.gql: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------- 2 | # !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!! 3 | # !!! DO NOT MODIFY THIS FILE BY YOURSELF !!! 4 | # ----------------------------------------------- 5 | 6 | input CatInput { 7 | id: ID 8 | name: String! 9 | age: Int! 10 | breed: String! 11 | } 12 | 13 | type CreateCatDto { 14 | id: ID! 15 | name: String! 16 | age: Int! 17 | breed: String! 18 | } 19 | 20 | type Mutation { 21 | create(input: CatInput!): CreateCatDto! 22 | updateCat(input: CatInput!): CreateCatDto! 23 | } 24 | 25 | type Query { 26 | hello(name: String!): String! 27 | cats: [CreateCatDto!]! 28 | getCat(id: Int!): CreateCatDto 29 | } 30 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/cats/cats.resolver.ts: -------------------------------------------------------------------------------- 1 | import { Resolver, Args, Query, Mutation } from '@nestjs/graphql'; 2 | import { CatsService } from './cats.service'; 3 | import { CreateCatDto } from './dto/create-cat.dto'; 4 | import { CatInput } from './inputs/cat.input'; 5 | import { Int } from 'type-graphql'; 6 | 7 | @Resolver() 8 | export class CatsResolver { 9 | constructor(private readonly catsService: CatsService) {} 10 | 11 | @Query(() => String) 12 | async hello(@Args({ name: 'name', type: () => String }) name: string) { 13 | return `Hello => ${name}`; 14 | } 15 | @Query(() => [CreateCatDto]) 16 | async cats() { 17 | return this.catsService.findAllCats(); 18 | } 19 | 20 | @Query(() => CreateCatDto, { nullable: true }) 21 | async getCat(@Args({ name: 'id', type: () => Int }) id: number) { 22 | return this.catsService.getCat(id); 23 | } 24 | 25 | @Mutation(() => CreateCatDto) 26 | async create(@Args('input') cat: CatInput) { 27 | return this.catsService.createCat(cat); 28 | } 29 | 30 | @Mutation(() => CreateCatDto) 31 | async updateCat(@Args('input') cat: CatInput) { 32 | return this.catsService.update(cat); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/cats/cats.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, Inject } from '@nestjs/common'; 2 | import { CreateCatDto } from './dto/create-cat.dto'; 3 | import { Cat } from './cat.entity'; 4 | import { CatInput } from './inputs/cat.input'; 5 | 6 | @Injectable() 7 | export class CatsService { 8 | constructor( 9 | @Inject('CATS_REPOSITORY') private readonly CATS_REPOSITORY: typeof Cat, 10 | ) {} 11 | 12 | async findAllCats(): Promise { 13 | return await this.CATS_REPOSITORY.findAll(); 14 | } 15 | 16 | async getCat(id: number): Promise { 17 | return this.CATS_REPOSITORY.findByPk(id); 18 | } 19 | 20 | async createCat(cat: CatInput): Promise { 21 | return await this.CATS_REPOSITORY.create(cat); 22 | } 23 | async update(cat: CatInput): Promise { 24 | const { name, age, breed, id } = cat; 25 | const response = await this.CATS_REPOSITORY.update( 26 | { name, age, breed, id }, 27 | { where: { id }, returning: true }, 28 | ); 29 | const [updated, updatedCat] = response; 30 | if (!!updated) { 31 | return updatedCat[0].dataValues as Cat; 32 | } 33 | return; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-api", 3 | "version": "0.0.1", 4 | "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": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ", 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": "^6.0.0", 24 | "@nestjs/core": "^6.0.0", 25 | "@nestjs/graphql": "^6.2.1", 26 | "@nestjs/platform-express": "^6.0.0", 27 | "apollo-server-express": "^2.5.0", 28 | "graphql": "^14.3.0", 29 | "graphql-tools": "^4.0.4", 30 | "pg": "^7.11.0", 31 | "pg-hstore": "^2.3.2", 32 | "reflect-metadata": "^0.1.12", 33 | "rimraf": "^2.6.2", 34 | "rxjs": "^6.3.3", 35 | "sequelize": "^5.8.6", 36 | "sequelize-typescript": "^0.6.11", 37 | "type-graphql": "^0.17.4" 38 | }, 39 | "devDependencies": { 40 | "@nestjs/testing": "^6.0.0", 41 | "@types/express": "^4.16.0", 42 | "@types/jest": "^23.3.13", 43 | "@types/node": "^10.12.18", 44 | "@types/sequelize": "^4.28.1", 45 | "@types/supertest": "^2.0.7", 46 | "concurrently": "^4.1.0", 47 | "jest": "^23.6.0", 48 | "nodemon": "^1.18.9", 49 | "prettier": "^1.15.3", 50 | "supertest": "^3.4.1", 51 | "ts-jest": "24.0.2", 52 | "ts-node": "8.1.0", 53 | "tsconfig-paths": "3.8.0", 54 | "tslint": "5.16.0", 55 | "typescript": "3.4.3", 56 | "wait-on": "^3.2.0" 57 | }, 58 | "jest": { 59 | "moduleFileExtensions": [ 60 | "js", 61 | "json", 62 | "ts" 63 | ], 64 | "rootDir": "src", 65 | "testRegex": ".spec.ts$", 66 | "transform": { 67 | "^.+\\.(t|j)s$": "ts-jest" 68 | }, 69 | "coverageDirectory": "../coverage", 70 | "testEnvironment": "node" 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master 6 | [travis-url]: https://travis-ci.org/nestjs/nest 7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux 8 | [linux-url]: https://travis-ci.org/nestjs/nest 9 | 10 |

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

11 |

12 | NPM Version 13 | Package License 14 | NPM Downloads 15 | Travis 16 | Linux 17 | Coverage 18 | Gitter 19 | Backers on Open Collective 20 | Sponsors on Open Collective 21 | 22 | 23 |

24 | 26 | 27 | ## Description 28 | 29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ npm install 35 | ``` 36 | 37 | ## Running the app 38 | 39 | ```bash 40 | # development 41 | $ npm run start 42 | 43 | # watch mode 44 | $ npm run start:dev 45 | 46 | # production mode 47 | $ npm run start:prod 48 | ``` 49 | 50 | ## Test 51 | 52 | ```bash 53 | # unit tests 54 | $ npm run test 55 | 56 | # e2e tests 57 | $ npm run test:e2e 58 | 59 | # test coverage 60 | $ npm run test:cov 61 | ``` 62 | 63 | ## Support 64 | 65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 66 | 67 | ## Stay in touch 68 | 69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 70 | - Website - [https://nestjs.com](https://nestjs.com/) 71 | - Twitter - [@nestframework](https://twitter.com/nestframework) 72 | 73 | ## License 74 | 75 | Nest is [MIT licensed](LICENSE). 76 | --------------------------------------------------------------------------------