Gabarito para o passo 'GraphQL: Consultando dados do Master Data'
// node/index.ts
import {
LRUCache,
Service,
ServiceContext,
ParamsContext,
RecorderState,
method,
} from '@vtex/api'
import { Clients } from './clients'
import { productList } from './resolvers/products'
import { updateLiveUsers } from './event/liveUsersUpdate'
import { analytics } from './handlers/analytics'
const TREE_SECONDS_MS = 3 * 1000
const CONCURRENCY = 10
// Create a LRU memory cache for the Status client.
// The @vtex/api HttpClient respects Cache-Control headers and uses the provided cache.
const memoryCache = new LRUCache<string, any>({ max: 5000 })
metrics.trackCache('status', memoryCache)
declare global {
type Context = ServiceContext<Clients, State>
interface State extends RecorderState {
code: number
}
}
export default new Service<Clients, State, ParamsContext>({
clients: {
implementation: Clients,
options: {
default: {
retries: 2,
timeout: 10000,
},
events: {
exponentialTimeoutCoefficient: 2,
exponentialBackoffCoefficient: 2,
initialBackoffDelay: 50,
retries: 1,
timeout: TREE_SECONDS_MS,
concurrency: CONCURRENCY,
},
},
},
events: {
liveUsersUpdate: updateLiveUsers,
},
graphql: {
resolvers: {
Query: {
productList,
},
},
},
routes: {
analytics: method({
GET: [analytics],
}),
},
})
{
"name": "backend-course",
"vendor": "vtex",
"version": "0.0.2",
"title": "Backend Course",
"description": "Reference app for the Backend Course",
"builders": {
"graphql": "1.x",
"docs": "0.x",
"node": "6.x"
},
"scripts": {
"prereleasy": "bash lint.sh"
},
"credentialType": "absolute",
"policies": [
{
"name": "ADMIN_DS"
},
{
"name": "outbound-access",
"attrs": {
"host": "api.vtex.com",
"path": "/dataentities/*"
}
}
],
"dependencies": {
"vtex.events-example": "0.x"
},
"$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema"
}
// node/resolvers/product.ts
import { COURSE_ENTITY } from '../utils/constants'
export const productList = async (
_: any,
{ topN }: { topN: number },
{ clients: { masterdata } }: Context
) =>
masterdata
.scrollDocuments({
dataEntity: COURSE_ENTITY,
fields: ['count', 'slug'],
schema: 'v1',
size: topN,
sort: `count DESC`,
})
.then(({ data }) => data)
# graphql/types/productList.graphql
type ProductList {
slug: String
count: Int
}
# graphql/schema.graphql
type Query {
productList(topN: Int): [ProductView]
}
Help us make this content better!
VTEX IO courses are open source. If you see something wrong, you can open a pull request!
Make a contributionUpdated almost 4 years ago