Add embryo to products
This commit is contained in:
@@ -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' },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './products-sql';
|
||||||
@@ -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
@@ -7,13 +7,15 @@ import { dbConfig } from './config';
|
|||||||
import { typeDefs } from './schema';
|
import { typeDefs } from './schema';
|
||||||
import resolvers from './resolvers';
|
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);
|
const knexConfig = knexStringcase(dbConfig);
|
||||||
|
|
||||||
// set up any dataSources our resolvers need
|
// set up any dataSources our resolvers need
|
||||||
const dataSources = () => ({
|
const dataSources = () => ({
|
||||||
orderApi: new OrderAPI(knexConfig),
|
orderApi: new OrderAPI(knexConfig),
|
||||||
|
productApi: new ProductAPI(knexConfig),
|
||||||
});
|
});
|
||||||
|
|
||||||
const context = async ({ req }) => {
|
const context = async ({ req }) => {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import orders from './order';
|
import orders from './orders-resolver';
|
||||||
|
import products from './products-resolver';
|
||||||
import { DateTimeScalar } from './datetime-type';
|
import { DateTimeScalar } from './datetime-type';
|
||||||
|
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
Query: { ...orders },
|
Query: { ...orders, ...products },
|
||||||
DateTime: DateTimeScalar,
|
DateTime: DateTimeScalar,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// (parent, args, ctx, info)
|
||||||
|
const products = {
|
||||||
|
products: (_, __, { dataSources }) => dataSources.productApi.getProducts(),
|
||||||
|
};
|
||||||
|
|
||||||
|
export default products;
|
||||||
@@ -6,6 +6,7 @@ const typeDefs = gql`
|
|||||||
type Query {
|
type Query {
|
||||||
orders: [Order]
|
orders: [Order]
|
||||||
order(id: ID!): Order
|
order(id: ID!): Order
|
||||||
|
products: [Product]
|
||||||
}
|
}
|
||||||
|
|
||||||
type Order {
|
type Order {
|
||||||
@@ -56,6 +57,11 @@ const typeDefs = gql`
|
|||||||
market: String
|
market: String
|
||||||
locale: String
|
locale: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Product {
|
||||||
|
id: ID!
|
||||||
|
artNo: String!
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export { typeDefs };
|
export { typeDefs };
|
||||||
|
|||||||
+6
-1
@@ -6,6 +6,11 @@
|
|||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"strictPropertyInitialization": false,
|
"strictPropertyInitialization": false,
|
||||||
"allowJs": true
|
"allowJs": true,
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"name": "typescript-sql-tagged-template-plugin"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user