-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection-item.resolver.ts
127 lines (111 loc) · 4.36 KB
/
collection-item.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Arg, Args, Mutation, Query, Resolver, FieldResolver, Root, Ctx } from 'type-graphql';
import { Inject } from 'typedi';
import { Fields, StandardDeleteResponse, UserId, BaseContext } from 'warthog';
import {
CollectionItemCreateInput,
CollectionItemCreateManyArgs,
CollectionItemUpdateArgs,
CollectionItemWhereArgs,
CollectionItemWhereInput,
CollectionItemWhereUniqueInput
} from '../../../generated';
import { CollectionItem } from './collection-item.model';
import { CollectionItemService } from './collection-item.service';
import { User } from '../user/user.model';
import { Item } from '../item/item.model';
@Resolver(CollectionItem)
export class CollectionItemResolver {
constructor(@Inject('CollectionItemService') public readonly service: CollectionItemService) {}
@Query(() => [CollectionItem])
async collectionItems(
@Args() { where, orderBy, limit, offset }: CollectionItemWhereArgs,
@Fields() fields: string[]
): Promise<CollectionItem[]> {
// TODO: Be able to use fields here without breaking artist/title fieldResolvers
return this.service.find<CollectionItemWhereInput>(where, orderBy, limit, offset);
}
@Query(() => CollectionItem)
async collectionItem(
@Arg('where') where: CollectionItemWhereUniqueInput
): Promise<CollectionItem> {
return this.service.findOne<CollectionItemWhereUniqueInput>(where);
}
@FieldResolver(() => String)
async artist(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<string> {
let itemDetails = await ctx.dataLoader.loaders.CollectionItem.itemDetails.load(collectionItem);
return itemDetails.artist;
}
@FieldResolver(() => String)
async title(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<string> {
let itemDetails = await ctx.dataLoader.loaders.CollectionItem.itemDetails.load(collectionItem);
return itemDetails.title;
}
// TODO: "Cannot return null for non-nullable field CollectionItem.mbid."
// @FieldResolver(() => String)
// async mbid(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<string> {
// let itemDetails = await ctx.dataLoader.loaders.CollectionItem.itemDetails.load(collectionItem);
// if(itemDetails.mbid) {
// return itemDetails.mbid;
// }
// else {
// return '';
// }
// }
@FieldResolver(() => String)
async rating(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<string> {
let reviews = await ctx.dataLoader.loaders.CollectionItem.reviews.load(collectionItem);
if(reviews.length > 0) {
return reviews[reviews.length-1].rating;
}
else {
return '';
}
}
@FieldResolver(() => String)
async reviewBody(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<string> {
// TODO: Only loas the most recent review
let reviews = await ctx.dataLoader.loaders.CollectionItem.reviews.load(collectionItem);
if(reviews.length > 0) {
return reviews[reviews.length-1].body;
}
else {
return '';
}
}
@FieldResolver(() => [Item])
itemDetails(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<Item[]> {
return ctx.dataLoader.loaders.CollectionItem.itemDetails.load(collectionItem);
}
@FieldResolver(() => [Item])
reviews(@Root() collectionItem: CollectionItem, @Ctx() ctx: BaseContext): Promise<Item[]> {
return ctx.dataLoader.loaders.CollectionItem.reviews.load(collectionItem);
}
@Mutation(() => CollectionItem)
async createCollectionItem(
@Arg('data') data: CollectionItemCreateInput,
@UserId() userId: string
): Promise<CollectionItem> {
return this.service.create(data, userId);
}
@Mutation(() => [CollectionItem])
async createManyCollectionItems(
@Args() { data }: CollectionItemCreateManyArgs,
@UserId() userId: string
): Promise<CollectionItem[]> {
return this.service.createMany(data, userId);
}
@Mutation(() => CollectionItem)
async updateCollectionItem(
@Args() { data, where }: CollectionItemUpdateArgs,
@UserId() userId: string
): Promise<CollectionItem> {
return this.service.update(data, where, userId);
}
@Mutation(() => StandardDeleteResponse)
async deleteCollectionItem(
@Arg('where') where: CollectionItemWhereUniqueInput,
@UserId() userId: string
): Promise<StandardDeleteResponse> {
return this.service.delete(where, userId);
}
}