AddProduct mutation (#59)

This commit is contained in:
Niklas Fondberg
2021-10-05 10:03:13 +02:00
committed by GitHub
parent 4e55166828
commit 491ac6e64e
5 changed files with 38 additions and 0 deletions
+18
View File
@@ -1,5 +1,6 @@
import { BaseSQLDataSource } from './BaseSQLDataSource';
import { camelcase } from 'stringcase';
import slug from 'slug';
import * as sql from './sql';
import {
Product,
@@ -220,6 +221,23 @@ export class ProductAPI extends BaseSQLDataSource {
);
}
async addProduct(name: string, batch: string): Promise<number> {
const path = batch ? batch + ' ' + name : name;
const idres = await this.knex
.table('product-products')
.insert({
path: slug(path),
browsable: 0,
visible: 0,
})
.returning('productid');
const id = idres[0];
this.updateProductField(id, 'artNo', `e${id}`);
await this.updateProductField(id, 'name', name);
await this.updateProductField(id, 'batch', batch);
return id;
}
async updateProductInfo(
productId: number,
info: ProductInfoInput,
+7
View File
@@ -84,6 +84,13 @@ export const productQueryTypeDefs = {
///////////////////
// Mutations below
export const productMutationTypeDefs = {
async addProduct(_, { name, batch }, { dataSources }) {
const id = await (<ProductAPI>dataSources.productApi).addProduct(
name,
batch,
);
return (<ProductAPI>dataSources.productApi).getProduct(id);
},
async productInfo(_, { productId, info }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateProductInfo(
productId,
+1
View File
@@ -431,6 +431,7 @@ type IdNumberResult {
}
type Mutation {
addProduct(name: String!, batch: String): Product
productInfo(productId: Int!, info: ProductInfoInput!): Product
productFocusPoint(
productId: Int!