add fields to Thing

This commit is contained in:
João Geonizeli
2022-08-15 10:37:35 -03:00
parent 0b63f6eae0
commit 3f191a9f0c
5 changed files with 53 additions and 6 deletions

View File

@@ -30,6 +30,7 @@
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"apollo-server-express": "^3.10.1", "apollo-server-express": "^3.10.1",
"axios-cache-adapter": "^2.7.3", "axios-cache-adapter": "^2.7.3",
"camelcase-object-deep": "^1.1.7",
"graphql": "^16.5.0", "graphql": "^16.5.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",

View File

@@ -4,8 +4,19 @@
type Thing { type Thing {
id: Int! id: Int!
url: String!
name: String! name: String!
thumbnail: String! thumbnail: String!
previewImage: String!
likeCount: Int!
makeCount: Int!
commentCount: Int!
"""URL to thingiverse.com page"""
publicUrl: String!
"""ISO 8601 date string"""
createdAt: String!
} }
type Query { type Query {

View File

@@ -1,13 +1,34 @@
import { Field, Int, ObjectType, } from '@nestjs/graphql'; import { Field, Int, ObjectType } from '@nestjs/graphql';
@ObjectType() @ObjectType()
export class Thing { export class Thing {
@Field(() => Int) @Field(() => Int)
id: number; id: number;
@Field(() => String)
url: string;
@Field(() => String) @Field(() => String)
name: string; name: string;
@Field(() => String) @Field(() => String)
thumbnail: string; thumbnail: string;
@Field(() => String)
previewImage: string;
@Field(() => Int)
likeCount: number;
@Field(() => Int)
makeCount: number;
@Field(() => Int)
commentCount: number;
@Field(() => String, { description: 'URL to thingiverse.com page' })
publicUrl: string;
@Field(() => String, { description: 'ISO 8601 date string' })
createdAt: string;
} }

View File

@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios'; import { HttpService } from '@nestjs/axios';
import { Args, Query, Resolver } from '@nestjs/graphql'; import { Args, Query, Resolver } from '@nestjs/graphql';
import { firstValueFrom } from 'rxjs'; import * as camelcase from 'camelcase-object-deep';
import { firstValueFrom, lastValueFrom, map } from 'rxjs';
import { ThingsArgs } from './dto/things.args'; import { ThingsArgs } from './dto/things.args';
import { Thing } from './thing.model'; import { Thing } from './thing.model';
@@ -20,14 +21,22 @@ export class ThingResolver {
const queryStirng = `/search?${params.toString()}`; const queryStirng = `/search?${params.toString()}`;
const { data } = await firstValueFrom( this.httpService.axiosRef.interceptors.response.use((response) => {
this.httpService.get<{ response.data = camelcase(response);
return response;
});
type Response = {
data: {
hits: Thing[]; hits: Thing[];
total: number; total: number;
}>(queryStirng), }
}
const { data: response } = await firstValueFrom(
this.httpService.get<Response>(queryStirng),
); );
return data.hits; return response.data.hits
} }
@Query(() => Thing) @Query(() => Thing)

View File

@@ -1957,6 +1957,11 @@ callsites@^3.0.0:
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73" resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ== integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==
camelcase-object-deep@^1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/camelcase-object-deep/-/camelcase-object-deep-1.1.7.tgz#e6de1c4eaa1cd1d4d2ad3e77fc85e2ad1801f822"
integrity sha512-hKykTR59km6Xo3QOns12em/qScl8N4Q1yAyiJFZ/lfPFHHiOweV5omRL1FWJnNSJL8rXnGZmKC/JSQl/Lyp1pQ==
camelcase@^5.3.1: camelcase@^5.3.1:
version "5.3.1" version "5.3.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"