-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathserver.js
135 lines (134 loc) · 3.18 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var express = require('express')
var express_graphql = require('express-graphql')
var { buildSchema } = require('graphql')
var { loadOrder, queryOrders, upsertOrder, loadAccount, upsertAccount, deleteAccount, queryAccounts } = require('./dbutil');
// GraphQL schema
var schema = buildSchema(`
scalar Date
type Query {
account(id: String!): AccountHistory
listAccounts: [Account]
order(id: String!): Order
orders(
location: String
): [Order]
trends: Trends
}
type Mutation {
mutateAccount(input: AccountInput!): AccountHistory
addOrder(input: OrderInput!): Order
}
input OrderInput {
request_date: Date
location: String!
items: [ItemInput]!
account_id: String
source_system: String!
}
input AccountInput {
id: String
email: String
cell: String
name: String
mutation: String
}
input ItemInput {
label: String
type: String
quantity: Int
perUnitPrice: Float
}
type Order {
id: String
request_date: Date
location: String
items: [OrderItem]
account_id: String
customer: Account
source: String
}
type OrderItem {
label: String
type: String
quantity: Int
perUnitPrice: Float
}
type Account {
id: String
email: String
cell: String
name: String
}
type AccountHistory {
id: String
email: String
cell: String
name: String
orders: [Order]
}
type Trends {
locations: [TrendData]
types: [TrendData]
}
type TrendData {
label: String
value: Int
}
`)
var getOrder = (args) => {
var id = args.id
return loadOrder(id)
}
var getOrders = (args) => {
return queryOrders(args.location)
}
var createOrder = (args) => {
var input = args.input
if (!input.request_date)
input.request_date = new Date()
return upsertOrder(input)
}
var crudAccount = (args) => {
var input = args.input
input.mutation = input.mutation.toLowerCase()
if (input.mutation == "delete") {
return deleteAccount(input)
} else {
return upsertAccount(input)
}
}
var getAccount = (args) => {
return loadAccount(args.id)
}
var listAccounts = () => {
return queryAccounts()
}
var getTrends = () => {
return { locations: [], types: [] }
}
var root = {
addOrder: createOrder,
mutateAccount: crudAccount,
order: getOrder,
orders: getOrders,
trends: getTrends,
account: getAccount,
listAccounts: listAccounts
}
// Create an express server and a GraphQL endpoint
var app = express()
app.use('/graphql', express_graphql({
schema: schema,
rootValue: root,
graphiql: true,
formatError: error => ({
message: error.message,
locations: error.locations,
stack: error.stack ? error.stack.split('\n') : [],
path: error.path
})
}));
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Express GraphQL Server Now Running On localhost:${PORT}/graphql`)
});