Add embryo to products

This commit is contained in:
Niklas Fondberg
2021-04-06 15:49:41 +02:00
parent 813f4c8e47
commit 949c641410
10 changed files with 110 additions and 4 deletions
+23
View File
@@ -0,0 +1,23 @@
import { SQLDataSource } from 'datasource-sql';
import * as sql from './sql';
const MINUTE = 60;
export class ProductAPI extends SQLDataSource {
constructor(config) {
super(config);
}
async getProducts() {
const query = sql.products(true, true, 2);
const data = await this.knex.raw(query);
console.log(JSON.stringify(data.rows[0], null, 2));
return [
{ id: 1, artNo: 'SE' },
{ id: 2, artNo: 'DK' },
];
}
}
+1
View File
@@ -0,0 +1 @@
export * from './products-sql';
+62
View File
@@ -0,0 +1,62 @@
// Get syntax highlighting with vscode by installing "Comment tagged templates2
export function products(
visible: boolean | null,
browsable: boolean | null,
limit: number = 100,
) {
const query = /* sql */ `
SELECT products.productid,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
json_agg(
json_build_object(
'printid',
printproducts.printid,
'productid',
printproducts.productid,
'groupid',
printproducts.groupid,
'inserted',
printproducts.inserted,
'updated',
printproducts.updated
)
) AS print_products,
json_agg(
json_build_object(
'id',
blacklist.id,
'product_id',
blacklist.product_id,
'market_id',
blacklist.market_id,
'group_id',
blacklist.group_id,
'created_at',
blacklist.created_at,
'updated_at',
blacklist.updated_at
)
) AS blacklisted
FROM "product-products" products
JOIN "product-printproducts" printproducts ON products.productid = printproducts.productid
JOIN product_blacklist blacklist ON products.productid = blacklist.product_id
${
visible != null
? /* sql */ `
WHERE products.visible = ${visible ? '1' : '0'}
AND products.browsable = ${browsable ? '1' : '0'}`
: ``
}
GROUP BY products.productid
LIMIT ${limit}
`;
return query;
}
+3 -1
View File
@@ -7,13 +7,15 @@ import { dbConfig } from './config';
import { typeDefs } from './schema';
import resolvers from './resolvers';
import { OrderAPI } from './datasources/order';
import { OrderAPI } from './datasources/order-api';
import { ProductAPI } from './datasources/product-api';
const knexConfig = knexStringcase(dbConfig);
// set up any dataSources our resolvers need
const dataSources = () => ({
orderApi: new OrderAPI(knexConfig),
productApi: new ProductAPI(knexConfig),
});
const context = async ({ req }) => {
+3 -2
View File
@@ -1,8 +1,9 @@
import orders from './order';
import orders from './orders-resolver';
import products from './products-resolver';
import { DateTimeScalar } from './datetime-type';
const resolvers = {
Query: { ...orders },
Query: { ...orders, ...products },
DateTime: DateTimeScalar,
};
+6
View File
@@ -0,0 +1,6 @@
// (parent, args, ctx, info)
const products = {
products: (_, __, { dataSources }) => dataSources.productApi.getProducts(),
};
export default products;
+6
View File
@@ -6,6 +6,7 @@ const typeDefs = gql`
type Query {
orders: [Order]
order(id: ID!): Order
products: [Product]
}
type Order {
@@ -56,6 +57,11 @@ const typeDefs = gql`
market: String
locale: String
}
type Product {
id: ID!
artNo: String!
}
`;
export { typeDefs };
+6 -1
View File
@@ -6,6 +6,11 @@
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false,
"allowJs": true
"allowJs": true,
"plugins": [
{
"name": "typescript-sql-tagged-template-plugin"
}
]
}
}