diff --git a/src/thing/thing.resolver.ts b/src/thing/thing.resolver.ts index 67858b2..f20035a 100644 --- a/src/thing/thing.resolver.ts +++ b/src/thing/thing.resolver.ts @@ -1,7 +1,7 @@ import { HttpService } from '@nestjs/axios'; -import { Args, Query, Resolver } from '@nestjs/graphql'; +import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; import * as camelcase from 'camelcase-object-deep'; -import { firstValueFrom, lastValueFrom, map } from 'rxjs'; +import { firstValueFrom } from 'rxjs'; import { ThingsArgs } from './dto/things.args'; import { Thing } from './thing.model'; @@ -12,8 +12,8 @@ export class ThingResolver { @Query(() => [Thing]) async things(@Args() args: ThingsArgs): Promise { const params = new URLSearchParams({ - page: args.page.toString(), - per_page: args.perPage.toString(), + page: args.page?.toString(), + per_page: args.perPage?.toString(), sort: args.sort, posted_after: args.postedAfter, type: 'trings', @@ -21,22 +21,18 @@ export class ThingResolver { const queryStirng = `/search?${params.toString()}`; - this.httpService.axiosRef.interceptors.response.use((response) => { - response.data = camelcase(response); - return response; - }); + try { + const { data: response } = await firstValueFrom( + this.httpService.get<{ + hits: Thing[]; + total: number; + }>(queryStirng), + ); - type Response = { - data: { - hits: Thing[]; - total: number; - } + return camelcase(response.hits || []); + } catch (error) { + return []; } - const { data: response } = await firstValueFrom( - this.httpService.get(queryStirng), - ); - - return response.data.hits } @Query(() => Thing)