titles.resolver.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { ObjectType, Field, Float, Int, Arg, Query, Resolver, createUnionType } from 'type-graphql';
  2. import { Inject } from 'typedi';
  3. import { Channel } from '../channel/channel.model';
  4. import { Video } from '../video/video.model';
  5. import { TitlesFTSService } from './titles.service';
  6. @ObjectType()
  7. export class TitlesFTSOutput {
  8. @Field(type => TitlesSearchItem)
  9. item!: typeof TitlesSearchItem
  10. @Field(type => Float)
  11. rank!: number
  12. @Field(type => String)
  13. isTypeOf!: string
  14. @Field(type => String)
  15. highlight!: string
  16. }
  17. export const TitlesSearchItem = createUnionType({
  18. name: "TitlesSearchResult",
  19. types: () => [
  20. Channel,
  21. Video,
  22. ],
  23. });
  24. @Resolver()
  25. export default class TitlesFTSResolver {
  26. constructor(@Inject('TitlesFTSService') readonly service: TitlesFTSService) {}
  27. @Query(() => [TitlesFTSOutput])
  28. async titles(
  29. @Arg('text') query: string,
  30. @Arg('limit', () => Int, { defaultValue: 5 }) limit: number):Promise<Array<TitlesFTSOutput>>{
  31. return this.service.search(query, limit);
  32. }
  33. }