This commit is contained in:
Niklas Fondberg
2021-04-06 09:56:18 +02:00
parent a1280f4c0e
commit 4532fbc853
12 changed files with 5506 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
export const dbConfig = {
client: 'pg',
connection: 'postgresql://fondberg:@docker.for.mac.localhost/photowall',
pool: {
min: 1,
max: 10,
},
};
+85
View File
@@ -0,0 +1,85 @@
import { SQLDataSource } from 'datasource-sql';
const MINUTE = 60;
interface Order {
id: number;
inserted: Date;
paid: boolean;
confirmed: boolean;
delivered: boolean;
newsletter: boolean;
creditInvoice: boolean;
canceled: boolean;
reminder: boolean;
deliveredDate: Date;
countryCode: string;
language: string;
deliveryCountryCode: string;
deliveryMethod: string;
deliveryPrice: string;
deliveryVat: number;
customerType: string;
paymentType: string;
attention: string;
currency: string;
exchangeRate: number;
exchangeRateEur: number;
invoiceId: string;
contractCustomerId: number;
vatNumber: string;
comment: string;
cancelsOrderId: number;
cancelledByOrderId: number;
orderlogId: number;
resellerStore: string;
orderSum: number;
klarnaOrderId: string;
pwintyId: number;
email: string;
phone: string;
deliveryEmail: string;
deliveryPhone: string;
customerFirstname: string;
customerLastname: string;
customerEmail: string;
customerCellphone: string;
billingAddressId: number;
deliveryAddressId: number;
flyer_ids: string;
market: string;
locale: string;
}
export class OrderAPI extends SQLDataSource {
constructor(config) {
super(config);
}
async getOrders() {
const data = await this.knex
.select('*')
.from('orders')
.where('inserted', '>=', '2021-03-01T00:00:00Z')
.cache(MINUTE);
console.log(data);
return [
{ id: 1, countryCode: 'SE' },
{ id: 2, countryCode: 'DK' },
];
}
async getOrder(id: number) {
const data = await this.knex
.select('*')
.from('orders')
.where('id', id)
.cache(MINUTE);
console.log(data);
return { id: 2, countryCode: 'DK' };
}
}
+35
View File
@@ -0,0 +1,35 @@
import dotenv from 'dotenv';
dotenv.config();
import { ApolloServer } from 'apollo-server';
import { dbConfig } from './config';
import { typeDefs } from './schema';
import resolvers from './resolvers';
import { OrderAPI } from './datasources/order';
// set up any dataSources our resolvers need
const dataSources = () => ({
orderApi: new OrderAPI(dbConfig),
});
const context = async ({ req }) => {
return { data: { foo: 'bar' } };
};
async function main() {
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources,
context,
introspection: true,
playground: true,
});
await server.listen();
console.log('Server is running on http://localhost:4000');
}
main();
+11
View File
@@ -0,0 +1,11 @@
// (parent, args, ctx, info)
const orders = {
orders: (_, __, { dataSources }) => dataSources.orderApi.getOrders(),
order: (_, { id }, { dataSources }) => dataSources.orderApi.getOrder(id),
};
const resolvers = {
Query: { ...orders },
};
export default resolvers;
+15
View File
@@ -0,0 +1,15 @@
import { gql } from 'apollo-server';
const typeDefs = gql`
type Query {
orders: [Order]
order(id: ID!): Order
}
type Order {
id: ID!
countryCode: String
}
`;
export { typeDefs };