add things query

This commit is contained in:
João Geonizeli
2022-08-15 09:02:57 -03:00
parent 458d9a1376
commit 0b63f6eae0
11 changed files with 209 additions and 16 deletions

View File

@@ -1,11 +1,22 @@
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { ThingResolver } from './thing/thing.resolver';
import { httpCacheAdapter } from './utils/httpCacheAdapter';
@Module({
imports: [
ConfigModule.forRoot(),
HttpModule.register({
baseURL: 'https://api.thingiverse.com/',
adapter: httpCacheAdapter,
headers: {
Authorization: `Bearer ${process.env.THINGIVERSE_TOKEN}`,
},
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),

View File

@@ -4,8 +4,23 @@
type Thing {
id: Int!
name: String!
thumbnail: String!
}
type Query {
things: [Thing!]!
things(page: Int, perPage: Int, sort: ThingsArgsSort = POPULAR, postedAfter: ThingsArgsPostedAfter): [Thing!]!
thing(id: Float!): Thing!
}
enum ThingsArgsSort {
POPULAR
NEWEST
MAKES
}
enum ThingsArgsPostedAfter {
Last7Days
Last30Days
ThisYear
}

View File

@@ -0,0 +1,36 @@
import { Field, ArgsType, Int, registerEnumType } from '@nestjs/graphql';
export enum ThingsArgsSort {
POPULAR = 'popular',
NEWEST = 'newest',
MAKES = 'makes',
}
registerEnumType(ThingsArgsSort, {
name: 'ThingsArgsSort',
});
export enum ThingsArgsPostedAfter {
Last7Days = 'now-7d',
Last30Days = 'now-30d',
ThisYear = 'now-365d',
}
registerEnumType(ThingsArgsPostedAfter, {
name: 'ThingsArgsPostedAfter',
});
@ArgsType()
export class ThingsArgs {
@Field(() => Int, { nullable: true })
page: number;
@Field(() => Int, { nullable: true })
perPage: number;
@Field(() => ThingsArgsSort, { nullable: true, defaultValue: 'popular' })
sort: ThingsArgsSort;
@Field(() => ThingsArgsPostedAfter, { nullable: true })
postedAfter?: ThingsArgsPostedAfter;
}

View File

@@ -1,7 +1,13 @@
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Field, Int, ObjectType, } from '@nestjs/graphql';
@ObjectType()
export class Thing {
@Field(type => Int)
@Field(() => Int)
id: number;
}
@Field(() => String)
name: string;
@Field(() => String)
thumbnail: string;
}

View File

@@ -1,19 +1,40 @@
import { Query, Resolver } from '@nestjs/graphql';
import { HttpService } from '@nestjs/axios';
import { Args, Query, Resolver } from '@nestjs/graphql';
import { firstValueFrom } from 'rxjs';
import { ThingsArgs } from './dto/things.args';
import { Thing } from './thing.model';
@Resolver(() => Thing)
export class ThingResolver {
constructor() {}
constructor(private readonly httpService: HttpService) {}
@Query(() => [Thing])
async things(): Promise<Thing[]> {
return [
{
id: 1,
},
{
id: 2,
},
];
async things(@Args() args: ThingsArgs): Promise<Thing[]> {
const params = new URLSearchParams({
page: args.page.toString(),
per_page: args.perPage.toString(),
sort: args.sort,
posted_after: args.postedAfter,
type: 'trings',
});
const queryStirng = `/search?${params.toString()}`;
const { data } = await firstValueFrom(
this.httpService.get<{
hits: Thing[];
total: number;
}>(queryStirng),
);
return data.hits;
}
@Query(() => Thing)
async thing(@Args('id') id: number): Promise<Thing> {
const { data } = await firstValueFrom(
this.httpService.get<Thing>(`/things/${id}`),
);
return data;
}
}

View File

@@ -0,0 +1,5 @@
import { setupCache } from 'axios-cache-adapter'
export const httpCacheAdapter = setupCache({
maxAge: 60 * 60 * 1000, // 1 hour
}).adapter