block.resolver.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {
  2. Arg,
  3. Args,
  4. Mutation,
  5. Query,
  6. Root,
  7. Resolver,
  8. FieldResolver,
  9. ObjectType,
  10. Field,
  11. Int,
  12. ArgsType,
  13. } from 'type-graphql';
  14. import { Inject } from 'typedi';
  15. import { Min } from 'class-validator';
  16. import { Fields, StandardDeleteResponse, UserId, PageInfo, RawFields } from 'warthog';
  17. import {
  18. BlockCreateInput,
  19. BlockCreateManyArgs,
  20. BlockUpdateArgs,
  21. BlockWhereArgs,
  22. BlockWhereInput,
  23. BlockWhereUniqueInput,
  24. BlockOrderByEnum,
  25. } from '../../../generated';
  26. import { Block } from './block.model';
  27. import { BlockService } from './block.service';
  28. import { Category } from '../category/category.model';
  29. import { Channel } from '../channel/channel.model';
  30. import { ClassEntity } from '../class-entity/class-entity.model';
  31. import { HttpMediaLocation } from '../http-media-location/http-media-location.model';
  32. import { JoystreamMediaLocation } from '../joystream-media-location/joystream-media-location.model';
  33. import { KnownLicense } from '../known-license/known-license.model';
  34. import { Language } from '../language/language.model';
  35. import { Member } from '../member/member.model';
  36. import { UserDefinedLicense } from '../user-defined-license/user-defined-license.model';
  37. import { Video } from '../video/video.model';
  38. import { VideoMedia } from '../video-media/video-media.model';
  39. import { getConnection } from 'typeorm';
  40. @ObjectType()
  41. export class BlockEdge {
  42. @Field(() => Block, { nullable: false })
  43. node!: Block;
  44. @Field(() => String, { nullable: false })
  45. cursor!: string;
  46. }
  47. @ObjectType()
  48. export class BlockConnection {
  49. @Field(() => Int, { nullable: false })
  50. totalCount!: number;
  51. @Field(() => [BlockEdge], { nullable: false })
  52. edges!: BlockEdge[];
  53. @Field(() => PageInfo, { nullable: false })
  54. pageInfo!: PageInfo;
  55. }
  56. @ArgsType()
  57. export class ConnectionPageInputOptions {
  58. @Field(() => Int, { nullable: true })
  59. @Min(0)
  60. first?: number;
  61. @Field(() => String, { nullable: true })
  62. after?: string; // V3: TODO: should we make a RelayCursor scalar?
  63. @Field(() => Int, { nullable: true })
  64. @Min(0)
  65. last?: number;
  66. @Field(() => String, { nullable: true })
  67. before?: string;
  68. }
  69. @ArgsType()
  70. export class BlockConnectionWhereArgs extends ConnectionPageInputOptions {
  71. @Field(() => BlockWhereInput, { nullable: true })
  72. where?: BlockWhereInput;
  73. @Field(() => BlockOrderByEnum, { nullable: true })
  74. orderBy?: BlockOrderByEnum;
  75. }
  76. @Resolver(Block)
  77. export class BlockResolver {
  78. constructor(@Inject('BlockService') public readonly service: BlockService) {}
  79. @Query(() => [Block])
  80. async blocks(
  81. @Args() { where, orderBy, limit, offset }: BlockWhereArgs,
  82. @Fields() fields: string[]
  83. ): Promise<Block[]> {
  84. return this.service.find<BlockWhereInput>(where, orderBy, limit, offset, fields);
  85. }
  86. @Query(() => BlockConnection)
  87. async blockConnection(
  88. @Args() { where, orderBy, ...pageOptions }: BlockConnectionWhereArgs,
  89. @RawFields() fields: Record<string, any>
  90. ): Promise<BlockConnection> {
  91. let result: any = {
  92. totalCount: 0,
  93. edges: [],
  94. pageInfo: {
  95. hasNextPage: false,
  96. hasPreviousPage: false,
  97. },
  98. };
  99. // If the related database table does not have any records then an error is thrown to the client
  100. // by warthog
  101. try {
  102. result = await this.service.findConnection<BlockWhereInput>(where, orderBy, pageOptions, fields);
  103. } catch (err) {
  104. console.log(err);
  105. // TODO: should continue to return this on `Error: Items is empty` or throw the error
  106. if (!(err.message as string).includes('Items is empty')) throw err;
  107. }
  108. return result as Promise<BlockConnection>;
  109. }
  110. @FieldResolver(() => Category)
  111. async categorys(@Root() r: Block): Promise<Category[]> {
  112. const result = await getConnection()
  113. .getRepository(Block)
  114. .findOne(r.id, { relations: ['categorys'] });
  115. if (!result || !result.categorys) {
  116. throw new Error('Unable to find result for Block.categorys');
  117. }
  118. return result.categorys;
  119. }
  120. @FieldResolver(() => Channel)
  121. async channels(@Root() r: Block): Promise<Channel[]> {
  122. const result = await getConnection()
  123. .getRepository(Block)
  124. .findOne(r.id, { relations: ['channels'] });
  125. if (!result || !result.channels) {
  126. throw new Error('Unable to find result for Block.channels');
  127. }
  128. return result.channels;
  129. }
  130. @FieldResolver(() => ClassEntity)
  131. async classEntitys(@Root() r: Block): Promise<ClassEntity[]> {
  132. const result = await getConnection()
  133. .getRepository(Block)
  134. .findOne(r.id, { relations: ['classEntitys'] });
  135. if (!result || !result.classEntitys) {
  136. throw new Error('Unable to find result for Block.classEntitys');
  137. }
  138. return result.classEntitys;
  139. }
  140. @FieldResolver(() => HttpMediaLocation)
  141. async httpMediaLocations(@Root() r: Block): Promise<HttpMediaLocation[]> {
  142. const result = await getConnection()
  143. .getRepository(Block)
  144. .findOne(r.id, { relations: ['httpMediaLocations'] });
  145. if (!result || !result.httpMediaLocations) {
  146. throw new Error('Unable to find result for Block.httpMediaLocations');
  147. }
  148. return result.httpMediaLocations;
  149. }
  150. @FieldResolver(() => JoystreamMediaLocation)
  151. async joystreamMediaLocations(@Root() r: Block): Promise<JoystreamMediaLocation[]> {
  152. const result = await getConnection()
  153. .getRepository(Block)
  154. .findOne(r.id, { relations: ['joystreamMediaLocations'] });
  155. if (!result || !result.joystreamMediaLocations) {
  156. throw new Error('Unable to find result for Block.joystreamMediaLocations');
  157. }
  158. return result.joystreamMediaLocations;
  159. }
  160. @FieldResolver(() => KnownLicense)
  161. async knownLicenses(@Root() r: Block): Promise<KnownLicense[]> {
  162. const result = await getConnection()
  163. .getRepository(Block)
  164. .findOne(r.id, { relations: ['knownLicenses'] });
  165. if (!result || !result.knownLicenses) {
  166. throw new Error('Unable to find result for Block.knownLicenses');
  167. }
  168. return result.knownLicenses;
  169. }
  170. @FieldResolver(() => Language)
  171. async languages(@Root() r: Block): Promise<Language[]> {
  172. const result = await getConnection()
  173. .getRepository(Block)
  174. .findOne(r.id, { relations: ['languages'] });
  175. if (!result || !result.languages) {
  176. throw new Error('Unable to find result for Block.languages');
  177. }
  178. return result.languages;
  179. }
  180. @FieldResolver(() => Member)
  181. async members(@Root() r: Block): Promise<Member[]> {
  182. const result = await getConnection()
  183. .getRepository(Block)
  184. .findOne(r.id, { relations: ['members'] });
  185. if (!result || !result.members) {
  186. throw new Error('Unable to find result for Block.members');
  187. }
  188. return result.members;
  189. }
  190. @FieldResolver(() => UserDefinedLicense)
  191. async userDefinedLicenses(@Root() r: Block): Promise<UserDefinedLicense[]> {
  192. const result = await getConnection()
  193. .getRepository(Block)
  194. .findOne(r.id, { relations: ['userDefinedLicenses'] });
  195. if (!result || !result.userDefinedLicenses) {
  196. throw new Error('Unable to find result for Block.userDefinedLicenses');
  197. }
  198. return result.userDefinedLicenses;
  199. }
  200. @FieldResolver(() => Video)
  201. async videos(@Root() r: Block): Promise<Video[]> {
  202. const result = await getConnection()
  203. .getRepository(Block)
  204. .findOne(r.id, { relations: ['videos'] });
  205. if (!result || !result.videos) {
  206. throw new Error('Unable to find result for Block.videos');
  207. }
  208. return result.videos;
  209. }
  210. @FieldResolver(() => VideoMedia)
  211. async videoMedias(@Root() r: Block): Promise<VideoMedia[]> {
  212. const result = await getConnection()
  213. .getRepository(Block)
  214. .findOne(r.id, { relations: ['videoMedias'] });
  215. if (!result || !result.videoMedias) {
  216. throw new Error('Unable to find result for Block.videoMedias');
  217. }
  218. return result.videoMedias;
  219. }
  220. }