-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic version of GraphQL server returning latest home sensors reading
- Loading branch information
Showing
10 changed files
with
2,132 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"rules": { | ||
"max-len": 1, | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { BigQuery } = require('@google-cloud/bigquery'); | ||
|
||
class HomeSensorAPI { | ||
constructor() { | ||
this.bigqueryClient = new BigQuery(); | ||
} | ||
|
||
async getLatestReading() { | ||
const sqlQuery = `SELECT | ||
temp as temperature, | ||
humd as humidity, | ||
UNIX_MILLIS(TIMESTAMP(time)) as timestamp | ||
FROM pi3_dht22_dataset.dht22_data tbl | ||
ORDER BY tbl.time DESC | ||
LIMIT 1`; | ||
|
||
const queryOptions = { | ||
query: sqlQuery, | ||
location: 'US', | ||
}; | ||
|
||
const [rows] = await this.bigqueryClient.query(queryOptions); | ||
|
||
return({ | ||
data: rows[0], | ||
}); | ||
} | ||
} | ||
|
||
module.exports.HomeSensorAPI = HomeSensorAPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
const { HomeSensorAPI } = require('./home-sensor'); | ||
|
||
const getDataSources = () => ({ | ||
homeSensorAPI: new HomeSensorAPI(), | ||
}); | ||
|
||
module.exports.getDataSources = getDataSources; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { gql } = require('apollo-server-express'); | ||
|
||
const typeDefs = gql` | ||
extend type Query { | ||
latestSensorReading: SensorReading!, | ||
} | ||
type SensorReading { | ||
temperature: String, | ||
humidity: String, | ||
timestamp: Float, | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
latestSensorReading: async (parent, args, { dataSources }, info) => { | ||
info.cacheControl.setCacheHint({ maxAge: 300, scope: 'PUBLIC' }); | ||
|
||
const response = await dataSources.homeSensorAPI.getLatestReading(); | ||
|
||
return response.data || {}; | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
typeDefs, | ||
resolvers, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const deepmerge = require('deepmerge'); | ||
const { gql } = require('apollo-server-express'); | ||
|
||
const { | ||
resolvers: homeSensorResolvers, | ||
typeDefs: homeSensorTypeDefs, | ||
} = require('./home-sensor'); | ||
|
||
const queryTypeDefs = gql` | ||
type Query { | ||
_empty: String | ||
} | ||
`; | ||
|
||
module.exports = { | ||
typeDefs: [queryTypeDefs, homeSensorTypeDefs], | ||
resolvers: deepmerge({}, homeSensorResolvers), | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
const express = require('express'); | ||
const apicache = require('apicache'); | ||
const cors = require('cors'); | ||
const responseCachePlugin = require('apollo-server-plugin-response-cache'); | ||
const { ApolloServer } = require('apollo-server-express'); | ||
|
||
const { homeSensorLatestHandler } = require('./home-sensor'); | ||
const { getDataSources } = require('./data-sources'); | ||
const { typeDefs, resolvers } = require('./graphql-modules'); | ||
|
||
const app = express(); | ||
const cache = apicache.middleware; | ||
|
||
app.get('/', (req, res) => { | ||
res.send('api I am'); | ||
}); | ||
|
||
app.get('/v1/home-sensor/latest', cache('10 minutes'), homeSensorLatestHandler); | ||
|
||
const port = process.env.PORT || 8080; | ||
|
||
const apollo = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
dataSources: getDataSources, | ||
plugins: [responseCachePlugin()], | ||
}); | ||
|
||
app.use(cors()); | ||
|
||
apollo.applyMiddleware({ app, path: '/graphql' }); | ||
|
||
app.listen(port, () => { | ||
console.log('Listening on port', port); | ||
}); |