From 949c641410400de22dc777f0fab4f50b3db956bd Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Tue, 6 Apr 2021 15:49:41 +0200 Subject: [PATCH] Add embryo to products --- src/datasources/{order.ts => order-api.ts} | 0 src/datasources/product-api.ts | 23 +++++++ src/datasources/sql/index.ts | 1 + src/datasources/sql/products-sql.ts | 62 +++++++++++++++++++ src/index.ts | 4 +- src/resolvers/index.ts | 5 +- .../{order.ts => orders-resolver.ts} | 0 src/resolvers/products-resolver.ts | 6 ++ src/schema.ts | 6 ++ tsconfig.json | 7 ++- 10 files changed, 110 insertions(+), 4 deletions(-) rename src/datasources/{order.ts => order-api.ts} (100%) create mode 100644 src/datasources/product-api.ts create mode 100644 src/datasources/sql/index.ts create mode 100644 src/datasources/sql/products-sql.ts rename src/resolvers/{order.ts => orders-resolver.ts} (100%) create mode 100644 src/resolvers/products-resolver.ts diff --git a/src/datasources/order.ts b/src/datasources/order-api.ts similarity index 100% rename from src/datasources/order.ts rename to src/datasources/order-api.ts diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts new file mode 100644 index 0000000..2727f9b --- /dev/null +++ b/src/datasources/product-api.ts @@ -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' }, + ]; + } +} diff --git a/src/datasources/sql/index.ts b/src/datasources/sql/index.ts new file mode 100644 index 0000000..3cce575 --- /dev/null +++ b/src/datasources/sql/index.ts @@ -0,0 +1 @@ +export * from './products-sql'; diff --git a/src/datasources/sql/products-sql.ts b/src/datasources/sql/products-sql.ts new file mode 100644 index 0000000..8e28b64 --- /dev/null +++ b/src/datasources/sql/products-sql.ts @@ -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; +} diff --git a/src/index.ts b/src/index.ts index 42d29af..b8d5870 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }) => { diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index cd1843f..a6c7c44 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -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, }; diff --git a/src/resolvers/order.ts b/src/resolvers/orders-resolver.ts similarity index 100% rename from src/resolvers/order.ts rename to src/resolvers/orders-resolver.ts diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts new file mode 100644 index 0000000..00d91de --- /dev/null +++ b/src/resolvers/products-resolver.ts @@ -0,0 +1,6 @@ +// (parent, args, ctx, info) +const products = { + products: (_, __, { dataSources }) => dataSources.productApi.getProducts(), +}; + +export default products; diff --git a/src/schema.ts b/src/schema.ts index e46a5ef..d50ae52 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -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 }; diff --git a/tsconfig.json b/tsconfig.json index 1a4b3d1..db19707 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,11 @@ "experimentalDecorators": true, "emitDecoratorMetadata": true, "strictPropertyInitialization": false, - "allowJs": true + "allowJs": true, + "plugins": [ + { + "name": "typescript-sql-tagged-template-plugin" + } + ] } }