diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index 07b1afb..a15c809 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -621,4 +621,32 @@ export class ProductAPI extends BaseSQLDataSource { promises.push(query); return Promise.all(promises); } + + async addCategoriesToProduct( + productId, + categoryIds, + ): Promise[]> { + const promises = categoryIds.map((categoryId) => + this.knex('product_category').insert({ + product_id: productId, + category_id: categoryId, + }), + ); + return Promise.all(promises); + } + + async removeCategoriesFromProduct( + productId, + categoryIds, + ): Promise[]> { + const promises = categoryIds.map((categoryId) => + this.knex('product_category') + .where({ + product_id: productId, + category_id: categoryId, + }) + .del(), + ); + return Promise.all(promises); + } } diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index d9935f2..f7b5416 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -222,4 +222,24 @@ export const productMutationTypeDefs = { ); return (dataSources.productApi).getProduct(productId); }, + + async addCategoriesToProduct(_, { productId, categoryIds }, { dataSources }) { + await (dataSources.productApi).addCategoriesToProduct( + productId, + categoryIds, + ); + return (dataSources.productApi).getProduct(productId); + }, + + async removeCategoriesFromProduct( + _, + { productId, categoryIds }, + { dataSources }, + ) { + await (dataSources.productApi).removeCategoriesFromProduct( + productId, + categoryIds, + ); + return (dataSources.productApi).getProduct(productId); + }, }; diff --git a/src/schema.graphql b/src/schema.graphql index 7bd2b0c..0c91627 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -466,4 +466,7 @@ type Mutation { removeRelatedProducts(productId: Int!, relatedProductIds: [Int]!): Product addKeyword(name: String!, type: KeywordType): Keyword + + addCategoriesToProduct(productId: Int!, categoryIds: [Int]!): Product + removeCategoriesFromProduct(productId: Int!, categoryIds: [Int]!): Product }