-
Notifications
You must be signed in to change notification settings - Fork 5
/
post.resolver.ts
69 lines (58 loc) · 1.93 KB
/
post.resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Arg, Args, Ctx, FieldResolver, Mutation, Query, Resolver, Root } from 'type-graphql';
import { Inject } from 'typedi';
import { BaseContext, Fields, UserId } from 'warthog';
import {
PostCreateInput,
PostCreateManyArgs,
PostUpdateArgs,
PostWhereArgs,
PostWhereInput,
PostWhereUniqueInput
} from '../../../generated';
import { User } from '../user/user.model';
import { Post } from './post.model';
import { PostService } from './post.service';
@Resolver(Post)
export class PostResolver {
constructor(@Inject('PostService') public readonly service: PostService) {}
@FieldResolver(() => User)
user(@Root() post: Post, @Ctx() ctx: BaseContext): Promise<User> {
return ctx.dataLoader.loaders.Post.user.load(post);
}
@Query(() => [Post])
async posts(
@Args() { where, orderBy, limit, offset }: PostWhereArgs,
@Fields() fields: string[]
): Promise<Post[]> {
return this.service.find<PostWhereInput>(where, orderBy, limit, offset, fields);
}
@Query(() => Post)
async post(@Arg('where') where: PostWhereUniqueInput): Promise<Post> {
return this.service.findOne<PostWhereUniqueInput>(where);
}
@Mutation(() => Post)
async createPost(@Arg('data') data: PostCreateInput, @UserId() userId: string): Promise<Post> {
return this.service.create(data, userId);
}
@Mutation(() => Post)
async updatePost(
@Args() { data, where }: PostUpdateArgs,
@UserId() userId: string
): Promise<Post> {
return this.service.update(data, where, userId);
}
@Mutation(() => [Post])
async createManyPosts(
@Args() { data }: PostCreateManyArgs,
@UserId() userId: string
): Promise<Post[]> {
return this.service.createMany(data, userId);
}
// @Mutation(() => StandardDeleteResponse)
// async deletePost(
// @Arg('where') where: PostWhereUniqueInput,
// @UserId() userId: string
// ): Promise<StandardDeleteResponse> {
// return this.service.delete(where, userId);
// }
}