From d5945da7ae79df23088eac4ef52a1d1465d23f61 Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Mon, 6 Sep 2021 12:53:51 +0200 Subject: [PATCH] Add mutation to add keywords (#24) --- src/datasources/keyword-api.ts | 25 +++++++++++++++++++++++++ src/datasources/product-api.ts | 6 +++--- src/resolvers/index.ts | 7 ++++++- src/resolvers/keywords-resolver.ts | 13 +++++++++++++ src/schema.graphql | 1 + 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/datasources/keyword-api.ts b/src/datasources/keyword-api.ts index ff58977..22510f8 100644 --- a/src/datasources/keyword-api.ts +++ b/src/datasources/keyword-api.ts @@ -68,6 +68,31 @@ ORDER BY keywords.value return this.knex('product_keyword').insert(fieldsToInsert); } + + // Mutations + async addKeyword(name: string, type: KeywordType): Promise { + const res = await this.knex('keywords') + .insert({ value: name }) + .returning('id'); + const keywordId = res[0]; + + if (type) { + const typeRes = await this.knex + .select('*') + .from('keywordtypes') + .where('name', type.toString().toLowerCase()); + + if (typeRes.length > 0) { + const typeId = typeRes[0].id; + await this.knex('keyword_type').insert({ + keyword_id: keywordId, + type_id: typeId, + }); + } + } + + return keywordId; + } } /* diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index 00290ea..9c18743 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -214,14 +214,14 @@ export class ProductAPI extends BaseSQLDataSource { ON CONFLICT (product_id, group_id, market_id) DO NOTHING`; - const insertsPromises = []; - const deleteQuery = this.knex.raw( + await this.knex.raw( /* sql */ ` DELETE FROM product_blacklist WHERE product_id = ? `, [productId], ); - insertsPromises.push(deleteQuery); + + const insertsPromises = []; markets.forEach((market) => { market.groupIds.forEach((groupId) => { const query = this.knex.raw(sql, [productId, groupId, market.marketId]); diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 3cd51aa..11a96e5 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -6,7 +6,11 @@ import { } from './products-resolver'; import { designerQueryTypeDefs, designerTypeDefs } from './designers-resolver'; import { categoryQueryTypeDefs, categoryTypeDefs } from './categories-resolver'; -import { keywordsQueryTypeDefs, keywordTypeDefs } from './keywords-resolver'; +import { + keywordsMutationTypeDefs, + keywordsQueryTypeDefs, + keywordTypeDefs, +} from './keywords-resolver'; import { marketsQueryTypeDefs, marketTypeDefs, @@ -26,6 +30,7 @@ const resolvers = { }, Mutation: { ...productMutationTypeDefs, + ...keywordsMutationTypeDefs, }, ...orderTypeDefs, ...productTypeDefs, diff --git a/src/resolvers/keywords-resolver.ts b/src/resolvers/keywords-resolver.ts index a11900c..0c36b2d 100644 --- a/src/resolvers/keywords-resolver.ts +++ b/src/resolvers/keywords-resolver.ts @@ -21,3 +21,16 @@ export const keywordsQueryTypeDefs = { keywords: getKeywords, keyword: getKeyword, }; + +/////////////////// +// Mutations below +export const keywordsMutationTypeDefs = { + async addKeyword(_, { name, type }, { dataSources }) { + const id = await (dataSources.keywordApi).addKeyword( + name, + type, + ); + console.log(id); + return (dataSources.keywordApi).getKeywordById(id); + }, +}; diff --git a/src/schema.graphql b/src/schema.graphql index f529a8c..86767de 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -404,4 +404,5 @@ type Mutation { ): Product productGroup(productId: Int!, groupIds: [Int]): Product productKeywords(productId: Int!, keywordIds: [Int]): Product + addKeyword(name: String!, type: KeywordType): Keyword }