Add mutation to add keywords (#24)

This commit is contained in:
Niklas Fondberg
2021-09-06 12:53:51 +02:00
committed by GitHub
parent 8d83205747
commit d5945da7ae
5 changed files with 48 additions and 4 deletions
+25
View File
@@ -68,6 +68,31 @@ ORDER BY keywords.value
return this.knex('product_keyword').insert(fieldsToInsert); return this.knex('product_keyword').insert(fieldsToInsert);
} }
// Mutations
async addKeyword(name: string, type: KeywordType): Promise<number> {
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;
}
} }
/* /*
+3 -3
View File
@@ -214,14 +214,14 @@ export class ProductAPI extends BaseSQLDataSource {
ON CONFLICT (product_id, group_id, market_id) ON CONFLICT (product_id, group_id, market_id)
DO NOTHING`; DO NOTHING`;
const insertsPromises = []; await this.knex.raw(
const deleteQuery = this.knex.raw(
/* sql */ ` /* sql */ `
DELETE FROM product_blacklist WHERE product_id = ? DELETE FROM product_blacklist WHERE product_id = ?
`, `,
[productId], [productId],
); );
insertsPromises.push(deleteQuery);
const insertsPromises = [];
markets.forEach((market) => { markets.forEach((market) => {
market.groupIds.forEach((groupId) => { market.groupIds.forEach((groupId) => {
const query = this.knex.raw(sql, [productId, groupId, market.marketId]); const query = this.knex.raw(sql, [productId, groupId, market.marketId]);
+6 -1
View File
@@ -6,7 +6,11 @@ import {
} from './products-resolver'; } from './products-resolver';
import { designerQueryTypeDefs, designerTypeDefs } from './designers-resolver'; import { designerQueryTypeDefs, designerTypeDefs } from './designers-resolver';
import { categoryQueryTypeDefs, categoryTypeDefs } from './categories-resolver'; import { categoryQueryTypeDefs, categoryTypeDefs } from './categories-resolver';
import { keywordsQueryTypeDefs, keywordTypeDefs } from './keywords-resolver'; import {
keywordsMutationTypeDefs,
keywordsQueryTypeDefs,
keywordTypeDefs,
} from './keywords-resolver';
import { import {
marketsQueryTypeDefs, marketsQueryTypeDefs,
marketTypeDefs, marketTypeDefs,
@@ -26,6 +30,7 @@ const resolvers = {
}, },
Mutation: { Mutation: {
...productMutationTypeDefs, ...productMutationTypeDefs,
...keywordsMutationTypeDefs,
}, },
...orderTypeDefs, ...orderTypeDefs,
...productTypeDefs, ...productTypeDefs,
+13
View File
@@ -21,3 +21,16 @@ export const keywordsQueryTypeDefs = {
keywords: getKeywords, keywords: getKeywords,
keyword: getKeyword, keyword: getKeyword,
}; };
///////////////////
// Mutations below
export const keywordsMutationTypeDefs = {
async addKeyword(_, { name, type }, { dataSources }) {
const id = await (<KeywordAPI>dataSources.keywordApi).addKeyword(
name,
type,
);
console.log(id);
return (<KeywordAPI>dataSources.keywordApi).getKeywordById(id);
},
};
+1
View File
@@ -404,4 +404,5 @@ type Mutation {
): Product ): Product
productGroup(productId: Int!, groupIds: [Int]): Product productGroup(productId: Int!, groupIds: [Int]): Product
productKeywords(productId: Int!, keywordIds: [Int]): Product productKeywords(productId: Int!, keywordIds: [Int]): Product
addKeyword(name: String!, type: KeywordType): Keyword
} }