-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (116 loc) · 3.63 KB
/
index.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
const express = require('express')
const { graphqlHTTP } = require('express-graphql')
const graphql = require('graphql')
const joinMonster = require('join-monster')
require('dotenv').config()
const session = require('express-session')
// Connect to database
const { Client } = require('pg')
const client = new Client({
host: process.env.DB_HOSTNAME,
user: process.env.DB_USER,
password: process.env.DB_PW,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
})
client.connect()
// Define the schema
const Meter = new graphql.GraphQLObjectType({
name: 'Meter',
fields: () => ({
id: { type: graphql.GraphQLID },
device_id: { type: graphql.GraphQLInt },
active_energy_l1: { type: graphql.GraphQLFloat },
active_energy_l2: { type: graphql.GraphQLFloat },
active_energy_l3: { type: graphql.GraphQLFloat },
time_stamp: { type: graphql.GraphQLString }
})
});
Meter._typeConfig = {
sqlTable: 'meter',
uniqueKey: 'id',
}
const QueryRoot = new graphql.GraphQLObjectType({
name: 'Query',
fields: () => ({
hello: {
type: graphql.GraphQLString,
resolve: () => "This is the graphQL API for REFLOW CLUJ"
},
meters: {
type: new graphql.GraphQLList(Meter),
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo, {}, sql => {
return client.query(sql)
})
}
},
meter: {
type: Meter,
args: { device_id: { type: graphql.GraphQLNonNull(graphql.GraphQLInt) } },
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo, {}, sql => {
return client.query(sql)
})
}
},
measurement: {
type: Meter,
args: { id: { type: graphql.GraphQLNonNull(graphql.GraphQLInt) } },
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo, {}, sql => {
return client.query(sql)
})
}
}
})
})
const MutationRoot = new graphql.GraphQLObjectType({
name: 'Mutation',
fields: () => ({
addMeterValues: {
type: Meter,
args: {
device_id: { type: graphql.GraphQLNonNull(graphql.GraphQLInt) },
active_energy_l1: { type: graphql.GraphQLNonNull(graphql.GraphQLFloat) },
active_energy_l2: { type: graphql.GraphQLNonNull(graphql.GraphQLFloat) },
active_energy_l3: { type: graphql.GraphQLNonNull(graphql.GraphQLFloat) },
time_stamp: { type: graphql.GraphQLNonNull(graphql.GraphQLString) }
},
resolve: async (parent, args, context, resolveInfo) => {
try {
return (await client.query("INSERT INTO meter (device_id, active_energy_l1, active_energy_l2, active_energy_l3, time_stamp) VALUES ($1, $2, $3, $4, $5) RETURNING *", [args.device_id, args.active_energy_l1, args.active_energy_l2, args.active_energy_l3, args.time_stamp])).rows[0]
} catch (err) {
throw new Error("Failed to insert new meter value")
}
}
}
})
})
const schema = new graphql.GraphQLSchema({
query: QueryRoot,
mutation: MutationRoot
});
// Create the Express app
const app = express();
const port = process.env.API_PORT;
var memoryStore = new session.MemoryStore();
const keycloak = require('./keycloak-config.js').initKeycloak(memoryStore);
// Set session
app.use(session({
secret: 'some_secret',
resave: false,
saveUninitialized: true,
store: memoryStore }));
app.use(keycloak.middleware());
app.use(
'/',
keycloak.protect(process.env.KEYCLOAK_ALLUSER_LIST.split(", ")),
graphqlHTTP({
schema: schema,
graphiql: true
})
);
app.listen(port, () => {
console.log(`App running on port ${port}.`)}
);