Skip to content

Instantly share code, notes, and snippets.

@johncmckim
Last active September 26, 2016 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncmckim/a49c9135df662eb4d6eaac1d57d5f90a to your computer and use it in GitHub Desktop.
Save johncmckim/a49c9135df662eb4d6eaac1d57d5f90a to your computer and use it in GitHub Desktop.
Garden Aid - Web BFF - GraphQL Lambda
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