3367 add facets subquery to product (#146)

This commit is contained in:
Anders Gustafsson
2023-02-28 10:28:58 +01:00
committed by GitHub
parent 5bc4e92c92
commit c475c1c860
8 changed files with 112 additions and 28 deletions
+37
View File
@@ -1,5 +1,6 @@
import { ProductAPI } from '../datasources/product-api';
import {
Orientation,
PrintProduct,
Product,
ProductListResult,
@@ -20,6 +21,7 @@ import { DesignerAPI } from '../datasources/designer-api';
import { PrintProductDefaultsAPI } from '../datasources/printproduct-defaults-api';
import { PrintProductDefaults } from '../types/printproduct-defaults-types';
import { GraphQLError } from 'graphql';
import { getOrientationString, isRepeatingPattern } from '../datasources/utils';
const ProductBlacklist = {
async market(parent, _args, { dataSources }) {
@@ -45,6 +47,41 @@ const Product = {
async related({ id }, _args, { dataSources }) {
return (<ProductAPI>dataSources.productApi).getRelatedProducts(id);
},
async facets(product: Product, _args, { dataSources }) {
const keywords = await (<KeywordAPI>(
dataSources.keywordApi
)).getProductKeywords(product.id);
const colors = [];
const types = [];
keywords.forEach((keyword) => {
if (keyword.type === 'COLOR') {
colors.push(keyword.value);
} else if (keyword.value === 'typeillustration') {
types.push('illustration');
} else if (keyword.value === 'typephotography') {
types.push('photograph');
}
});
const isRepeating = isRepeatingPattern(product);
if (isRepeating) {
types.push('repeating-pattern');
}
const orientations = isRepeating
? [
Orientation[Orientation.LANDSCAPE],
Orientation[Orientation.STANDING],
Orientation[Orientation.SQUARE],
]
: [getOrientationString(product.fields)];
return [
{ attribute: 'color', values: colors },
{ attribute: 'orientation', values: orientations },
{ attribute: 'type', values: types },
];
},
};
const PrintProduct = {