forked from tangollama/cis411_lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (144 loc) · 3.58 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const newrelic = require('newrelic')
const { ApolloServer, gql } = require('apollo-server')
const { loadOrder, queryOrders, upsertOrder, loadAccount, upsertAccount, deleteAccount, queryAccounts } = require('./dbutil')
// GraphQL schema
const getOrder = (parent, args, context, info) => {
var id = args.id
return new Promise(resolve => {
newrelic.startSegment('getOrder', false, () => {
loadOrder(id).then(order => {
console.log("resolving order", order)
resolve(order)
})
})
})
}
const getOrders = (parent, args, context, info) => {
return new Promise(resolve => {
newrelic.startSegment('getOrders', false, () => {
queryOrders(args).then(results => {
resolve(results)
})
})
})
}
const createOrder = (parent, args, context, info) => {
var input = args.input
if (!input.request_date)
input.request_date = new Date()
return new Promise(resolve => {
newrelic.startSegment('createOrder', false, () => {
resolve(upsertOrder(input))
})
})
}
const crudAccount = (parent, args, context, info) => {
var input = args.input
input.mutation = input.mutation.toLowerCase()
return new Promise(resolve => {
newrelic.startSegment('crudAccount', false, () => {
if (input.mutation == "delete") {
resolve(deleteAccount(input))
} else {
resolve(upsertAccount(input))
}
})
})
}
const getAccount = (parent, args, context, info) => {
return new Promise(resolve => {
newrelic.startSegment('getAccount', false, () => {
resolve(loadAccount(args.id))
})
})
}
const getAccounts = (parent, args, context, info) => {
return new Promise(resolve => {
newrelic.startSegment('getAccounts', false, () => {
resolve(queryAccounts(args.query))
})
})
}
// The GraphQL schema
const typeDefs = gql`
scalar Date
type Query {
account(id: String!): AccountHistory
accounts(query: String): [Account]
order(id: String!): Order
orders(query: String, location: String, bagel: String
): [Order]
}
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
dateLastOpened: Date
}
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]
}`
// A map of functions which return data for the schema.
const resolvers = {
Query: {
order: getOrder,
orders: getOrders,
account: getAccount,
accounts: getAccounts
},
Mutation: {
addOrder: createOrder,
mutateAccount: crudAccount
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
});