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);
}
// 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)
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]);
+6 -1
View File
@@ -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,
+13
View File
@@ -21,3 +21,16 @@ export const keywordsQueryTypeDefs = {
keywords: getKeywords,
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
productGroup(productId: Int!, groupIds: [Int]): Product
productKeywords(productId: Int!, keywordIds: [Int]): Product
addKeyword(name: String!, type: KeywordType): Keyword
}