From 9e2ff31b6506fa39afe966e81c123f7c9811bfc6 Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Thu, 21 Apr 2022 11:59:35 +0200 Subject: [PATCH] Add category locale names (#112) --- src/datasources/category-api.ts | 18 ++++++++++++++++-- src/resolvers/categories-resolver.ts | 3 +++ src/schema.graphql | 1 + src/types/category-types.ts | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/datasources/category-api.ts b/src/datasources/category-api.ts index 11d81b9..cf6603b 100644 --- a/src/datasources/category-api.ts +++ b/src/datasources/category-api.ts @@ -1,9 +1,9 @@ -import { SQLDataSource } from 'datasource-sql'; import { Category } from '../types/category-types'; +import { BaseSQLDataSource } from './BaseSQLDataSource'; const MINUTE = 60; -export class CategoryAPI extends SQLDataSource { +export class CategoryAPI extends BaseSQLDataSource { constructor(config) { super(config); } @@ -48,6 +48,20 @@ WHERE product_category.product_id = ? .cache(MINUTE); } + async getLocaleNameForId(id: number): Promise { + const res = await this.cachedRaw( + ` + SELECT json_object_agg(ec.locale, ec.name) as locale_names + FROM v_esales_categorytree_i18n ec + WHERE ec.id = ${id} + `, + ) + .cache(MINUTE * 5) + .then((data) => data.rows); + + return res[0].locale_names; + } + /** * TODO: should we have these? * category keywords diff --git a/src/resolvers/categories-resolver.ts b/src/resolvers/categories-resolver.ts index d76ad7f..d01a4e8 100644 --- a/src/resolvers/categories-resolver.ts +++ b/src/resolvers/categories-resolver.ts @@ -13,6 +13,9 @@ const Category = { return (dataSources.productApi).getCategoryProducts(id); } }, + async localeNames({ id }, _, { dataSources, auth }) { + return (dataSources.categoryApi).getLocaleNameForId(id); + }, }; async function getCategories(_, _args, { dataSources }) { diff --git a/src/schema.graphql b/src/schema.graphql index 70373d3..0ad4b0c 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -216,6 +216,7 @@ type Category { Default to false """ products(includeAllSubCategories: Boolean): [Product] + localeNames: JSON } type Batch { diff --git a/src/types/category-types.ts b/src/types/category-types.ts index fd52758..8909213 100644 --- a/src/types/category-types.ts +++ b/src/types/category-types.ts @@ -8,4 +8,5 @@ export interface Category { childCount: number; lft: number; rgt: number; + localeNames: Array>; }