Skip to content

Commit 4d407ab

Browse files
Merge pull request nestjs#1091 from cschroeter/master
sample(prisma) update Prisma example
2 parents ce498e8 + 447372a commit 4d407ab

7 files changed

Lines changed: 98 additions & 282 deletions

File tree

sample/22-graphql-prisma/graphql.schema.d.ts

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

sample/22-graphql-prisma/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
"start:prod": "node dist/main.js"
1313
},
1414
"dependencies": {
15-
"@nestjs/common": "5.3.6",
16-
"@nestjs/core": "5.3.6",
17-
"@nestjs/graphql": "5.2.2",
15+
"@nestjs/common": "5.3.7",
16+
"@nestjs/core": "5.3.7",
17+
"@nestjs/graphql": "5.3.1",
1818
"apollo-server-express": "2.0.6",
1919
"graphql": "0.13.2",
2020
"graphql-tools": "3.1.1",
21-
"prisma": "1.15.3",
21+
"prisma": "1.16.2",
2222
"prisma-binding": "2.1.5",
2323
"reflect-metadata": "0.1.12",
2424
"rxjs": "6.3.2",
@@ -28,7 +28,7 @@
2828
"nodemon": "1.18.4",
2929
"prettier": "1.14.2",
3030
"ts-node": "7.0.1",
31-
"tsconfig-paths": "3.5.0",
31+
"tsconfig-paths": "3.6.0",
3232
"tslint": "5.11.0"
3333
}
3434
}

sample/22-graphql-prisma/src/app.module.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import { Module } from '@nestjs/common';
22
import { GraphQLModule } from '@nestjs/graphql';
3-
import { join } from 'path';
43
import { PrismaModule } from './prisma/prisma.module';
54
import { PostsModule } from './posts/posts.module';
5+
import { GraphqlOptions } from 'graphql.options';
66

77
@Module({
88
imports: [
9-
GraphQLModule.forRoot({
10-
typePaths: ['./**/*.graphql'],
11-
path: '/',
12-
installSubscriptionHandlers: true,
13-
resolverValidationOptions: {
14-
requireResolversForResolveType: false,
15-
},
16-
definitions: {
17-
path: join(process.cwd(), 'src/graphql.schema.d.ts'),
18-
outputAs: 'class',
19-
},
9+
GraphQLModule.forRootAsync({
10+
useClass: GraphqlOptions,
2011
}),
2112
PrismaModule,
2213
PostsModule,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { GqlOptionsFactory, GqlModuleOptions } from '@nestjs/graphql';
2+
import { Injectable } from '@nestjs/common';
3+
import { join } from 'path';
4+
5+
@Injectable()
6+
export class GraphqlOptions implements GqlOptionsFactory {
7+
createGqlOptions(): Promise<GqlModuleOptions> | GqlModuleOptions {
8+
return {
9+
typePaths: ['./**/*.graphql'],
10+
path: '/',
11+
installSubscriptionHandlers: true,
12+
resolverValidationOptions: {
13+
requireResolversForResolveType: false,
14+
},
15+
definitions: {
16+
path: join(process.cwd(), 'src/graphql.schema.d.ts'),
17+
outputAs: 'class',
18+
},
19+
};
20+
}
21+
}

sample/22-graphql-prisma/src/graphql.schema.d.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ export class BatchPayload {
108108
count: Long;
109109
}
110110

111-
export class IMutation {
112-
createPost(data: PostCreateInput): Post | Promise<Post>;
113-
updatePost(data: PostUpdateInput, where: PostWhereUniqueInput): Post | Promise<Post>;
114-
deletePost(where: PostWhereUniqueInput): Post | Promise<Post>;
115-
upsertPost(where: PostWhereUniqueInput, create: PostCreateInput, update: PostUpdateInput): Post | Promise<Post>;
116-
updateManyPosts(data: PostUpdateInput, where?: PostWhereInput): BatchPayload | Promise<BatchPayload>;
117-
deleteManyPosts(where?: PostWhereInput): BatchPayload | Promise<BatchPayload>;
111+
export abstract class IMutation {
112+
abstract createPost(data: PostCreateInput): Post | Promise<Post>;
113+
abstract updatePost(data: PostUpdateInput, where: PostWhereUniqueInput): Post | Promise<Post>;
114+
abstract deletePost(where: PostWhereUniqueInput): Post | Promise<Post>;
115+
abstract upsertPost(where: PostWhereUniqueInput, create: PostCreateInput, update: PostUpdateInput): Post | Promise<Post>;
116+
abstract updateManyPosts(data: PostUpdateInput, where?: PostWhereInput): BatchPayload | Promise<BatchPayload>;
117+
abstract deleteManyPosts(where?: PostWhereInput): BatchPayload | Promise<BatchPayload>;
118118
}
119119

120120
export class PageInfo {
@@ -156,15 +156,16 @@ export class PostSubscriptionPayload {
156156
previousValues?: PostPreviousValues;
157157
}
158158

159-
export class IQuery {
160-
posts(where?: PostWhereInput, orderBy?: PostOrderByInput, skip?: number, after?: string, before?: string, first?: number, last?: number): Post[] | Promise<Post[]>;
161-
post(where: PostWhereUniqueInput): Post | Promise<Post>;
162-
postsConnection(where?: PostWhereInput, orderBy?: PostOrderByInput, skip?: number, after?: string, before?: string, first?: number, last?: number): PostConnection | Promise<PostConnection>;
163-
node(id: string): Node | Promise<Node>;
159+
export abstract class IQuery {
160+
abstract posts(where?: PostWhereInput, orderBy?: PostOrderByInput, skip?: number, after?: string, before?: string, first?: number, last?: number): Post[] | Promise<Post[]>;
161+
abstract post(where: PostWhereUniqueInput): Post | Promise<Post>;
162+
abstract postsConnection(where?: PostWhereInput, orderBy?: PostOrderByInput, skip?: number, after?: string, before?: string, first?: number, last?: number): PostConnection | Promise<PostConnection>;
163+
abstract node(id: string): Node | Promise<Node>;
164+
abstract temp__(): boolean | Promise<boolean>;
164165
}
165166

166-
export class ISubscription {
167-
post(where?: PostSubscriptionWhereInput): PostSubscriptionPayload | Promise<PostSubscriptionPayload>;
167+
export abstract class ISubscription {
168+
abstract post(where?: PostSubscriptionWhereInput): PostSubscriptionPayload | Promise<PostSubscriptionPayload>;
168169
}
169170

170171
export class User {

sample/22-graphql-prisma/src/posts/posts.resolver.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Info,
88
} from '@nestjs/graphql';
99
import { PrismaService } from '../prisma/prisma.service';
10+
import { BatchPayload } from '../prisma/prisma.binding';
1011
import { Post } from '../graphql.schema';
1112

1213
@Resolver()
@@ -28,6 +29,26 @@ export class PostsResolver {
2829
return await this.prisma.mutation.createPost(args, info);
2930
}
3031

32+
@Mutation('updatePost')
33+
async updatePost(@Args() args, @Info() info): Promise<Post> {
34+
return await this.prisma.mutation.updatePost(args, info);
35+
}
36+
37+
@Mutation('updateManyPosts')
38+
async updateManyPosts(@Args() args, @Info() info): Promise<BatchPayload> {
39+
return await this.prisma.mutation.updateManyPosts(args, info);
40+
}
41+
42+
@Mutation('deletePost')
43+
async deletePost(@Args() args, @Info() info): Promise<Post> {
44+
return await this.prisma.mutation.deletePost(args, info);
45+
}
46+
47+
@Mutation('deleteManyPosts')
48+
async deleteManyPosts(@Args() args, @Info() info): Promise<BatchPayload> {
49+
return await this.prisma.mutation.deleteManyPosts(args, info);
50+
}
51+
3152
@Subscription('post')
3253
onUserMutation() {
3354
return {

0 commit comments

Comments
 (0)