Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1.  
  2. const graphql = require('graphql');
  3. const tablesFactory = require('./dynamodb/tables');
  4. const MoistureService = require('./services/moisture');
  5.  
  6. const tables = tablesFactory();
  7. const moistureService = MoistureService({ moistureTable: tables.Moisture });
  8.  
  9. const MoistureType = new graphql.GraphQLObjectType({
  10. name: 'MoistureType',
  11. fields: {
  12. date: { type: graphql.GraphQLString },
  13. moisture: { type: graphql.GraphQLFloat },
  14. }
  15. });
  16.  
  17. const schema = new graphql.GraphQLSchema({
  18. query: new graphql.GraphQLObjectType({
  19. name: 'Root',
  20. description: 'Root of the Schema',
  21. fields: {
  22. moisture:
  23. name: 'MoistureQuery',
  24. description: 'Retrieve moisture levels',
  25. type: new graphql.GraphQLList(MoistureType),
  26. args: {
  27. clientId: {
  28. type: graphql.GraphQLString,
  29. },
  30. hours: {
  31. type: graphql.GraphQLInt,
  32. defaultValue: 1
  33. },
  34. },
  35. resolve: (_, args, ast) => {
  36. const hours = args.hours > 0 ? args.hours : 1;
  37. return moistureService.getLastHours(args.clientId, hours);
  38. }
  39. }
  40. }
  41. })
  42. });
  43.  
  44. module.exports.handler = function(event, context, cb) {
  45. console.log('Received event', event);
  46.  
  47. const query = event.body.query;
  48.  
  49. return graphql.query(event.body.query)
  50. .then((response) => {
  51. cb(null, response)
  52. })
  53. .catch((error) => {
  54. cb(error)
  55. });
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement