Last active
September 26, 2016 21:16
-
-
Save johncmckim/a49c9135df662eb4d6eaac1d57d5f90a to your computer and use it in GitHub Desktop.
Garden Aid - Web BFF - GraphQL Lambda
This file contains hidden or 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
const graphql = require('graphql'); | |
const tablesFactory = require('./dynamodb/tables'); | |
const MoistureService = require('./services/moisture'); | |
const tables = tablesFactory(); | |
const moistureService = MoistureService({ moistureTable: tables.Moisture }); | |
const MoistureType = new graphql.GraphQLObjectType({ | |
name: 'MoistureType', | |
fields: { | |
date: { type: graphql.GraphQLString }, | |
moisture: { type: graphql.GraphQLFloat }, | |
} | |
}); | |
const schema = new graphql.GraphQLSchema({ | |
query: new graphql.GraphQLObjectType({ | |
name: 'Root', | |
description: 'Root of the Schema', | |
fields: { | |
moisture: | |
name: 'MoistureQuery', | |
description: 'Retrieve moisture levels', | |
type: new graphql.GraphQLList(MoistureType), | |
args: { | |
clientId: { | |
type: graphql.GraphQLString, | |
}, | |
hours: { | |
type: graphql.GraphQLInt, | |
defaultValue: 1 | |
}, | |
}, | |
resolve: (_, args, ast) => { | |
const hours = args.hours > 0 ? args.hours : 1; | |
return moistureService.getLastHours(args.clientId, hours); | |
} | |
} | |
} | |
}) | |
}); | |
module.exports.handler = function(event, context, cb) { | |
console.log('Received event', event); | |
const query = event.body.query; | |
return graphql.query(schema, event.body.query) | |
.then((response) => { | |
cb(null, response) | |
}) | |
.catch((error) => { | |
cb(error) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment