Fixed small bugs
This commit is contained in:
@@ -1,2 +1,15 @@
|
|||||||
# graphql-api-js
|
# graphql-api-js
|
||||||
Apollo graphql api server
|
|
||||||
|
Apollo graphql api server for Photowall
|
||||||
|
|
||||||
|
## Run it locally
|
||||||
|
|
||||||
|
Create an .env file (see .env.example for what to set).
|
||||||
|
Run
|
||||||
|
|
||||||
|
```
|
||||||
|
$ npm install
|
||||||
|
$ npm start
|
||||||
|
```
|
||||||
|
|
||||||
|
Then head over to [The playground GUI](https://localhost:4000) in the browser
|
||||||
|
|||||||
+30
-2
@@ -51,7 +51,7 @@
|
|||||||
filter: { limit: 1, dates: { from: "2021-03-01", to: "2021-04-30" } }
|
filter: { limit: 1, dates: { from: "2021-03-01", to: "2021-04-30" } }
|
||||||
) {
|
) {
|
||||||
id
|
id
|
||||||
marketObject {
|
market {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
vat
|
vat
|
||||||
@@ -84,13 +84,41 @@
|
|||||||
{
|
{
|
||||||
order(id: 588274) {
|
order(id: 588274) {
|
||||||
id
|
id
|
||||||
marketObject {
|
market {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
vat
|
vat
|
||||||
currency
|
currency
|
||||||
priceAdjustment
|
priceAdjustment
|
||||||
}
|
}
|
||||||
|
deliveryInformation {
|
||||||
|
address {
|
||||||
|
recipientName
|
||||||
|
companyname
|
||||||
|
address1
|
||||||
|
address2
|
||||||
|
city
|
||||||
|
countryCode
|
||||||
|
stateCountyOrRegion
|
||||||
|
zipcode
|
||||||
|
}
|
||||||
|
email
|
||||||
|
phone
|
||||||
|
}
|
||||||
|
billingInformation {
|
||||||
|
address {
|
||||||
|
recipientName
|
||||||
|
companyname
|
||||||
|
address1
|
||||||
|
address2
|
||||||
|
city
|
||||||
|
countryCode
|
||||||
|
stateCountyOrRegion
|
||||||
|
zipcode
|
||||||
|
}
|
||||||
|
email
|
||||||
|
phone
|
||||||
|
}
|
||||||
rows {
|
rows {
|
||||||
id
|
id
|
||||||
order {
|
order {
|
||||||
|
|||||||
@@ -58,20 +58,28 @@ export class OrderAPI extends BaseSQLDataSource {
|
|||||||
|
|
||||||
getBillingInformation(row): ContactInformation {
|
getBillingInformation(row): ContactInformation {
|
||||||
return {
|
return {
|
||||||
email: 'niklas.fondberg@photowall.se',
|
email: row.email,
|
||||||
phone: '+46761386397',
|
phone: row.phone,
|
||||||
addressId: row.billingAddressId,
|
addressId: row.billingAddressId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getDeliveryInformation(row): ContactInformation {
|
getDeliveryInformation(row): ContactInformation {
|
||||||
return {
|
return {
|
||||||
email: 'niklas.fondberg@photowall.se', // TODO: fix hardcoded
|
email: row.deliveryEmail,
|
||||||
phone: '+46761386397',
|
phone: row.deliveryPhone,
|
||||||
addressId: row.deliveryAddressId,
|
addressId: row.deliveryAddressId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createOrderFromRow(row: any): Order {
|
||||||
|
row.billingInformation = this.getBillingInformation(row);
|
||||||
|
row.deliveryInformation = this.getDeliveryInformation(row);
|
||||||
|
row.marketName = row.market;
|
||||||
|
row.localeName = row.locale;
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
async getOrders(input: Maybe<GeneralInput>): Promise<Array<Order>> {
|
async getOrders(input: Maybe<GeneralInput>): Promise<Array<Order>> {
|
||||||
let query = this.knex.select('*').from('orders');
|
let query = this.knex.select('*').from('orders');
|
||||||
// .where('inserted', '>=', '2021-03-01T00:00:00Z')
|
// .where('inserted', '>=', '2021-03-01T00:00:00Z')
|
||||||
@@ -89,14 +97,9 @@ export class OrderAPI extends BaseSQLDataSource {
|
|||||||
query = query.limit(input?.limit);
|
query = query.limit(input?.limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(query.toString());
|
return await query
|
||||||
return await query.cache(MINUTE).then((rows) =>
|
.cache(MINUTE)
|
||||||
rows.map((row) => {
|
.then((rows) => rows.map((row) => this.createOrderFromRow(row)));
|
||||||
row.billingInformation = this.getBillingInformation(row);
|
|
||||||
row.deliveryInformation = this.getDeliveryInformation(row);
|
|
||||||
return row;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOrderById(id: number): Promise<Order> {
|
async getOrderById(id: number): Promise<Order> {
|
||||||
@@ -106,11 +109,7 @@ export class OrderAPI extends BaseSQLDataSource {
|
|||||||
.where('id', id)
|
.where('id', id)
|
||||||
.first()
|
.first()
|
||||||
.cache(MINUTE)
|
.cache(MINUTE)
|
||||||
.then((row) => {
|
.then((row) => this.createOrderFromRow(row));
|
||||||
row.billingInformation = this.getBillingInformation(row);
|
|
||||||
row.deliveryInformation = this.getDeliveryInformation(row);
|
|
||||||
return row;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOrderRowFieldMapping(): Promise<any> {
|
async getOrderRowFieldMapping(): Promise<any> {
|
||||||
|
|||||||
@@ -55,6 +55,13 @@ export class ProductAPI extends SQLDataSource {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createProductFromRow(row: any) {
|
||||||
|
row.blacklisting = this.getBlacklisting(row);
|
||||||
|
row.printProducts = this.getPrintProducts(row);
|
||||||
|
row.designerId = row.designerid;
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
async getProducts(
|
async getProducts(
|
||||||
limit: Maybe<number>,
|
limit: Maybe<number>,
|
||||||
visible: Maybe<boolean>,
|
visible: Maybe<boolean>,
|
||||||
@@ -63,28 +70,17 @@ export class ProductAPI extends SQLDataSource {
|
|||||||
limit = limit ?? 100;
|
limit = limit ?? 100;
|
||||||
const query = sql.products(limit, visible, browsable);
|
const query = sql.products(limit, visible, browsable);
|
||||||
|
|
||||||
return await this.knex.raw(query).then((data) =>
|
return await this.knex
|
||||||
data.rows.map((row) => {
|
.raw(query)
|
||||||
row.blacklisting = this.getBlacklisting(row);
|
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||||
row.printProducts = this.getPrintProducts(row);
|
|
||||||
row.designerId = row.designerid;
|
|
||||||
return row;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProduct(id: number): Promise<Maybe<Product>> {
|
async getProduct(id: number): Promise<Maybe<Product>> {
|
||||||
const query = sql.product(id);
|
const query = sql.product(id);
|
||||||
|
|
||||||
const res = await this.knex.raw(query).then((data) =>
|
const res = await this.knex
|
||||||
data.rows.map((row) => {
|
.raw(query)
|
||||||
console.log(row);
|
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||||
row.blacklisting = this.getBlacklisting(row);
|
|
||||||
row.printProducts = this.getPrintProducts(row);
|
|
||||||
row.designerId = row.designerid;
|
|
||||||
return row;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return res.find(Boolean);
|
return res.find(Boolean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
import { GraphQLScalarType } from 'graphql';
|
|
||||||
import { Kind } from 'graphql/language';
|
|
||||||
|
|
||||||
// possible switch to https://github.com/Urigo/graphql-scalars
|
|
||||||
const DateTimeScalar = new GraphQLScalarType({
|
|
||||||
name: 'DateTime',
|
|
||||||
description: 'Date time custom scalar type',
|
|
||||||
parseValue(value) {
|
|
||||||
return new Date(value); // value from the client
|
|
||||||
},
|
|
||||||
serialize(value) {
|
|
||||||
return new Date(value).toISOString(); // value sent to the client
|
|
||||||
},
|
|
||||||
parseLiteral(ast) {
|
|
||||||
if (ast.kind === Kind.INT) {
|
|
||||||
return parseInt(ast.value, 10); // ast value is always in string format
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const DateScalar = new GraphQLScalarType({
|
|
||||||
name: 'DateTime',
|
|
||||||
description: 'Date custom scalar type',
|
|
||||||
parseValue(value) {
|
|
||||||
console.log('FROM CLIENT:', value);
|
|
||||||
return new Date(value); // value from the client
|
|
||||||
},
|
|
||||||
serialize(value) {
|
|
||||||
return new Date(value).toISOString().split('T')[0]; // value sent to the client
|
|
||||||
},
|
|
||||||
parseLiteral(ast) {
|
|
||||||
if (ast.kind === Kind.INT) {
|
|
||||||
return parseInt(ast.value, 10); // ast value is always in string format
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const CustomTypes = {
|
|
||||||
DateTime: DateTimeScalar,
|
|
||||||
Date: DateScalar,
|
|
||||||
};
|
|
||||||
@@ -13,7 +13,8 @@ const Order: IResolverObject = {
|
|||||||
async rows({ id }, args, { dataSources }) {
|
async rows({ id }, args, { dataSources }) {
|
||||||
return (<OrderAPI>dataSources.orderApi).getOrderRowsByOrderId(id);
|
return (<OrderAPI>dataSources.orderApi).getOrderRowsByOrderId(id);
|
||||||
},
|
},
|
||||||
async marketObject({ market }, args, { dataSources }) {
|
// TODO change to market
|
||||||
|
async market({ market }, args, { dataSources }) {
|
||||||
return (<OrderAPI>dataSources.orderApi).getMarketByName(market);
|
return (<OrderAPI>dataSources.orderApi).getMarketByName(market);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-3
@@ -94,14 +94,14 @@ const typeDefs = gql`
|
|||||||
customerEmail: String
|
customerEmail: String
|
||||||
customerCellphone: String
|
customerCellphone: String
|
||||||
flyerIds: String
|
flyerIds: String
|
||||||
market: String
|
marketName: String
|
||||||
locale: String
|
localeName: String
|
||||||
billingInformation: ContactInformation
|
billingInformation: ContactInformation
|
||||||
deliveryInformation: ContactInformation
|
deliveryInformation: ContactInformation
|
||||||
billingAddressId: Int
|
billingAddressId: Int
|
||||||
deliveryAddressId: Int
|
deliveryAddressId: Int
|
||||||
rows: [OrderRow]
|
rows: [OrderRow]
|
||||||
marketObject: Market
|
market: Market
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrderRow {
|
type OrderRow {
|
||||||
|
|||||||
Reference in New Issue
Block a user