-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
52 lines (42 loc) · 1.11 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
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
const AWS = require('aws-sdk');
//UPDATE THE REGION IF YOU ARE USING ANYTHING OTHER THAN us-east-1
AWS.config.update({
region: "us-east-1",
});
const dynamoDb = new AWS.DynamoDB.DocumentClient();
var schema = buildSchema(`
type Query {
hello: String
getTemp(station_id: String): String
}
`);
var root = {
hello: () => {
return 'Hello to the whole world from ' + process.env.LOCATION;
},
getTemp: async function ({station_id}) {
params = {
TableName : "weather",
ProjectionExpression:"station_id, observation_unixtime, temp_c",
KeyConditionExpression: "station_id = :station",
ExpressionAttributeValues: {
":station": station_id
},
ScanIndexForward: false,
Limit: 1
};
var data = await dynamoDb.query(params).promise()
return data.Items[0].temp_c
},
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');