search.resolver.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 { SearchFTSService } from './search.service';
  6. import { ChannelWhereInput, VideoWhereInput, } from '../../../generated';
  7. @ObjectType()
  8. export class SearchFTSOutput {
  9. @Field(type => SearchSearchItem)
  10. item!: typeof SearchSearchItem
  11. @Field(type => Float)
  12. rank!: number
  13. @Field(type => String)
  14. isTypeOf!: string
  15. @Field(type => String)
  16. highlight!: string
  17. }
  18. export const SearchSearchItem = createUnionType({
  19. name: "SearchSearchResult",
  20. types: () => [
  21. Channel,
  22. Video,
  23. ],
  24. });
  25. @Resolver()
  26. export default class SearchFTSResolver {
  27. constructor(@Inject('SearchFTSService') readonly service: SearchFTSService) {}
  28. @Query(() => [SearchFTSOutput])
  29. async search(
  30. @Arg('text') query: string,
  31. @Arg('limit', () => Int, { defaultValue: 5 }) limit: number,
  32. @Arg('skip', () => Int, { defaultValue: 0 }) skip: number,
  33. @Arg('whereChannel', { nullable: true }) whereChannel?: ChannelWhereInput,
  34. @Arg('whereVideo', { nullable: true }) whereVideo?: VideoWhereInput,
  35. ):Promise<Array<SearchFTSOutput>>{
  36. return this.service.search(query, limit, skip, whereChannel,whereVideo,);
  37. }
  38. }