fix when quering without page params

This commit is contained in:
João Geonizeli
2022-09-09 18:39:33 -03:00
parent df244377d8
commit 83ebd2933f

View File

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