Skip to content

Commit 03f2832

Browse files
sample(graphql) update graphql sample (add validation)
1 parent 25588d8 commit 03f2832

11 files changed

Lines changed: 131 additions & 80 deletions

File tree

sample/12-graphql-apollo/package-lock.json

Lines changed: 82 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/12-graphql-apollo/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
"dependencies": {
1212
"@nestjs/common": "^5.3.6",
1313
"@nestjs/core": "^5.3.6",
14-
"@nestjs/graphql": "^5.2.0",
14+
"@nestjs/graphql": "^5.3.0",
1515
"apollo-server-express": "2.0.4",
16+
"class-transformer": "^0.1.9",
17+
"class-validator": "^0.9.1",
1618
"graphql": "0.13.2",
1719
"graphql-subscriptions": "0.5.8",
18-
"graphql-type-json": "0.2.1",
1920
"reflect-metadata": "0.1.12",
2021
"rxjs": "6.3.1",
2122
"typescript": "3.0.3"

sample/12-graphql-apollo/src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CatsModule } from './cats/cats.module';
1010
typePaths: ['./**/*.graphql'],
1111
installSubscriptionHandlers: true,
1212
definitions: {
13-
path: join(process.cwd(), 'src/graphql.schema.d.ts'),
13+
path: join(process.cwd(), 'src/graphql.schema.ts'),
1414
outputAs: 'class',
1515
},
1616
}),

sample/12-graphql-apollo/src/cats/cats.graphql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type Query {
44
}
55

66
type Mutation {
7-
createCat(name: String): Cat
7+
createCat(createCatInput: CreateCatInput): Cat
88
}
99

1010
type Subscription {
@@ -16,3 +16,8 @@ type Cat {
1616
name: String
1717
age: Int
1818
}
19+
20+
input CreateCatInput {
21+
name: String
22+
age: Int
23+
}

sample/12-graphql-apollo/src/cats/cats.resolvers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { ParseIntPipe, UseGuards } from '@nestjs/common';
22
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
33
import { PubSub } from 'graphql-subscriptions';
4+
import { Cat } from '../graphql.schema';
45
import { CatsGuard } from './cats.guard';
56
import { CatsService } from './cats.service';
6-
import { Cat } from './interfaces/cat.interface';
7+
import { CreateCatDto } from './dto/create-cat.dto';
78

89
const pubSub = new PubSub();
910

@@ -26,7 +27,7 @@ export class CatsResolvers {
2627
}
2728

2829
@Mutation('createCat')
29-
async create(@Args() args: Cat): Promise<Cat> {
30+
async create(@Args('createCatInput') args: CreateCatDto): Promise<Cat> {
3031
const createdCat = await this.catsService.create(args);
3132
pubSub.publish('catCreated', { catCreated: createdCat });
3233
return createdCat;

sample/12-graphql-apollo/src/cats/cats.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { Cat } from './interfaces/cat.interface';
2+
import { Cat } from '../graphql.schema';
33

44
@Injectable()
55
export class CatsService {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Min } from 'class-validator';
2+
import { CreateCatInput } from '../../graphql.schema';
3+
4+
export class CreateCatDto extends CreateCatInput {
5+
@Min(1)
6+
age: number;
7+
}

sample/12-graphql-apollo/src/cats/interfaces/cat.interface.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

sample/12-graphql-apollo/src/graphql.schema.d.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export class CreateCatInput {
2+
name?: string;
3+
age?: number;
4+
}
5+
6+
export class Cat {
7+
id?: number;
8+
name?: string;
9+
age?: number;
10+
}
11+
12+
export abstract class IMutation {
13+
abstract createCat(createCatInput?: CreateCatInput): Cat | Promise<Cat>;
14+
}
15+
16+
export abstract class IQuery {
17+
abstract getCats(): Cat[] | Promise<Cat[]>;
18+
19+
abstract cat(id: string): Cat | Promise<Cat>;
20+
21+
abstract temp__(): boolean | Promise<boolean>;
22+
}
23+
24+
export abstract class ISubscription {
25+
abstract catCreated(): Cat | Promise<Cat>;
26+
}

0 commit comments

Comments
 (0)