Demo is working

This commit is contained in:
2022-02-22 19:41:55 +00:00
parent 4898a09126
commit 61f638d8da
5 changed files with 1070 additions and 2 deletions
+29
View File
@@ -0,0 +1,29 @@
//config
require('dotenv').config();
//express for testing
const express = require('express');
const app = express();
//uses text input
app.use(express.text());
//test the library
const sineQL = require('sineql');
const schema = require('./schema.js');
const queryHandlers = require('./query-handlers.js');
//omit 'createHandlers', 'updateHandlers' or 'deleteHandlers' to disable those methods
const sine = sineQL(schema, { queryHandlers });
//open the endpoint
app.post('/sineql', async (req, res) => {
const [code, result] = await sine(req.body);
res.status(code).send(result);
});
//startup
const port = process.env.WEB_PORT || 4000;
app.listen(port, err => {
console.log(`listening to *:${port}`);
});
+50
View File
@@ -0,0 +1,50 @@
const fetch = require('node-fetch');
const Weather = async (query, typeGraph) => {
let q = null;
if (query.city && query.city.match) {
q = query.city.match;
} else if (query.latitude && query.latitude.match && query.longitude && query.longitude.match) {
q = `${query.latitude.match},${query.longitude.match}`;
} else {
throw 'Unknown location';
}
const response = await fetch(`http://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_KEY}&q=${q}`);
const data = await response.json();
//return value
const result = {
//
};
if (query.last_updated) {
result['last_updated'] = data.current.last_updated;
}
if (query.temp_c) {
result['temp_c'] = data.current.temp_c;
}
if (query.temp_f) {
result['temp_f'] = data.current.temp_f;
}
if (query.condition) {
result['condition'] = data.current.condition.text;
}
if (query.wind_mph) {
result['wind_mph'] = data.current.wind_mph;
}
if (query.wind_kph) {
result['wind_kph'] = data.current.wind_kph;
}
if (query.wind_dir) {
result['wind_dir'] = data.current.wind_dir;
}
return result;
};
module.exports = {
Weather,
};
+16
View File
@@ -0,0 +1,16 @@
module.exports =
`
type Weather {
String city
Float latitude
Float longitude
String last_updated
Float temp_c
Float temp_f
String condition
Float wind_mph
Float wind_kph
String wind_dir
}
`;