├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json ├── package.json ├── serverless.yml └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | webpack -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Yatin Badal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless + Express + GraphQL 2 | 3 | [READ: Create a Serverless GraphQL server using Express, Apollo Server and AWS Lambda](https://medium.com/@YatinBadal/create-a-serverless-graphql-server-using-express-apollo-server-and-aws-lambda-c3850a2092b5) 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import serverless from "serverless-http"; 3 | import graphiql from "graphql-playground-middleware-express"; 4 | import { ApolloServer, gql } from "apollo-server-express"; 5 | 6 | const typeDefs = gql` 7 | type Query { 8 | hello: String 9 | } 10 | `; 11 | 12 | const resolvers = { 13 | Query: { 14 | hello: () => "world" 15 | } 16 | }; 17 | 18 | const app = express(); 19 | 20 | const server = new ApolloServer({ 21 | typeDefs, 22 | resolvers, 23 | path: "/graphql" 24 | }); 25 | 26 | server.applyMiddleware({ app }); 27 | 28 | app.get("/playground", graphiql({ endpoint: "/graphql" })); 29 | 30 | const handler = serverless(app); 31 | 32 | export { handler }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ServerlessGraphqlExpress", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "dependencies": { 7 | "apollo-server-express": "^2.1.0", 8 | "express": "^4.16.4", 9 | "graphql": "^0.13.2", 10 | "serverless": "^1.32.0", 11 | "serverless-http": "^1.8.0", 12 | "serverless-offline": "^3.31.0", 13 | "serverless-webpack": "^5.2.0" 14 | }, 15 | "devDependencies": { 16 | "babel-loader": "^7.1.5", 17 | "graphql-playground-middleware-express": "^1.7.6", 18 | "webpack": "^4.23.1", 19 | "webpack-node-externals": "^1.7.2" 20 | }, 21 | "scripts": { 22 | "test": "echo \"Error: no test specified\" && exit 1" 23 | }, 24 | "keywords": [], 25 | "author": "", 26 | "license": "ISC" 27 | } 28 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-graphql 2 | 3 | plugins: 4 | - serverless-webpack 5 | - serverless-offline 6 | 7 | custom: 8 | webpack: 9 | webpackConfig: ./webpack.config.js 10 | includeModules: true 11 | 12 | provider: 13 | name: aws 14 | runtime: nodejs8.10 15 | stage: dev 16 | region: eu-west-1 17 | 18 | functions: 19 | graphql: 20 | handler: index.handler 21 | events: 22 | - http: 23 | path: graphql 24 | method: post 25 | cors: true 26 | playground: 27 | handler: index.handler 28 | events: 29 | - http: 30 | path: playground 31 | method: get 32 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | const slsw = require("serverless-webpack"); 3 | const nodeExternals = require("webpack-node-externals"); 4 | 5 | module.exports = { 6 | entry: slsw.lib.entries, 7 | target: "node", 8 | mode: slsw.lib.webpack.isLocal ? "development" : "production", 9 | optimization: { 10 | minimize: false 11 | }, 12 | performance: { 13 | hints: false 14 | }, 15 | devtool: "nosources-source-map", 16 | externals: [nodeExternals()], 17 | module: { 18 | rules: [ 19 | { 20 | test: /\.js$/, 21 | exclude: /node_modules/, 22 | use: [ 23 | { 24 | loader: "babel-loader" 25 | } 26 | ] 27 | } 28 | ] 29 | }, 30 | output: { 31 | libraryTarget: "commonjs2", 32 | path: path.join(__dirname, ".webpack"), 33 | filename: "[name].js", 34 | sourceMapFilename: "[file].map" 35 | } 36 | }; 37 | --------------------------------------------------------------------------------