Skip to content

Instantly share code, notes, and snippets.

@alexxxmf
Created May 27, 2019 12:47
Show Gist options
  • Save alexxxmf/37c5be0a6b5b7b05f516773a40d25708 to your computer and use it in GitHub Desktop.
Save alexxxmf/37c5be0a6b5b7b05f516773a40d25708 to your computer and use it in GitHub Desktop.
const { ApolloServer, gql } = require("apollo-server-lambda");
const AWS = require("aws-sdk");
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const typeDefs = gql`
type Recipe {
recipeId: Int
recipeName: String
description: String
carbs: Int
preparationTimeMin: Int
preparationTimeMax: Int
difficulty: Int
mainImageEntry: String
foodType: String
}
type Comment {
userName: String
comment: String
}
type Step {
step: String
}
type Rating {
userName: String
rating: Int
}
type RecipeDetails {
recipeId: Int
stepsCount: Int
comments: [Comment]
steps: [Step]
ratings: [Rating]
}
type Query {
hello: String
getRecipes: [Recipe]
getRecipeDetails: RecipeDetails
}
`;
const getRecipes = async () => {
const params = {
TableName: process.env.KETO_RECIPES
};
return new Promise((resolve, reject) => {
dynamoDb.scan(params, (error, result) => {
if (error) {
reject(error);
} else if (!result.Items) {
resolve(null);
} else {
resolve(result.Items);
}
})
});
};
const getRecipeDetails = async (recipeId) => {
const params = {
TableName: process.env.KETO_RECIPE_DETAILS,
Key: { recipeId: recipeId }
};
return new Promise((resolve, reject) => {
dynamoDb.get(params, (error, result) => {
if (error) {
reject(error);
} else if (!result.Item) {
resolve(null);
} else {
resolve(result.Item);
}
})
});
};
const resolvers = {
Query: {
hello: () => 'Hello world!',
getRecipes,
getRecipeDetails
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
}),
});
exports.graphql = server.createHandler();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment