Files
graphql-api-js/src/resolvers/products-resolver.ts
T

131 lines
3.8 KiB
TypeScript

import { ProductAPI } from '../datasources/product-api';
import {
Product,
ProductListResult,
ProductsFilterInput,
} from '../types/product-types';
import { CategoryAPI } from '../datasources/category-api';
import { Category } from '../types/category-types';
import { KeywordAPI } from '../datasources/keyword-api';
import { Keyword } from '../types/keyword-types';
import { MarketLocaleAPI } from '../datasources/market-locale-api';
import { InteriorAPI } from '../datasources/interior-api';
import { InteriorsLambdaAPI } from '../datasources/interiors-lambda-api';
const ProductBlacklist = {
async market(parent, _args, { dataSources }) {
return (<MarketLocaleAPI>dataSources.marketLocaleApi).getMarketById(
parent.marketId,
);
},
};
const Product = {
// (parent, args, ctx, info)
async categories({ id }, _args, { dataSources }): Promise<Array<Category>> {
return (<CategoryAPI>dataSources.categoryApi).getProductCategories(id);
},
async designer({ designerId }, _args, { dataSources }) {
if (!designerId) {
return null;
}
return dataSources.designerApi.getDesignerById(designerId);
},
async keywords({ id }, _args, { dataSources }): Promise<Array<Keyword>> {
return (<KeywordAPI>dataSources.keywordApi).getProductKeywords(id);
},
async related({ id }, _args, { dataSources }) {
return (<ProductAPI>dataSources.productApi).getRelatedProducts(id);
},
};
const PrintProduct = {
async interiors({ id }, _args, { dataSources }) {
return (<InteriorAPI>dataSources.interiorApi).getPrintProductInteriors(id);
},
};
async function getProductsResult(
_,
input: ProductsFilterInput,
{ dataSources },
): Promise<ProductListResult> {
const total = (<ProductAPI>dataSources.productApi).getProductsTotal(input);
const items = (<ProductAPI>dataSources.productApi).getProducts(input);
return {
pagination: {
limit: input.pagination.limit,
offset: 0, // TODO: this will be implemented later if the client app needs it
total: total,
},
items: items,
};
}
async function getProduct(_, { id }, { dataSources }): Promise<Product> {
return (<ProductAPI>dataSources.productApi).getProduct(id);
}
export const productTypeDefs = {
ProductBlacklist,
Product,
PrintProduct,
};
export const productQueryTypeDefs = {
products: getProductsResult,
product: getProduct,
};
///////////////////
// Mutations below
export const productMutationTypeDefs = {
async productInfo(_, { productId, info }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateProductInfo(
productId,
info,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async productFocusPoint(
_,
{ productId, focusXpoint2, focusYpoint2 },
{ dataSources },
) {
await (<ProductAPI>dataSources.productApi).updateFocusPoint(
productId,
focusXpoint2,
focusYpoint2,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async productBlacklisting(_, { productId, markets }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateBlacklisting(
productId,
markets,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async productGroup(_, { productId, groupIds }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateGroups(
productId,
groupIds,
);
const product = await (<ProductAPI>dataSources.productApi).getProduct(
productId,
);
await (<InteriorsLambdaAPI>(
dataSources.interiorsLambdaApi
)).generateNewInteriors(product);
return product;
},
async productKeywords(_, { productId, keywordIds }, { dataSources }) {
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
productId,
keywordIds,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
};