├── .gitignore ├── LICENSE ├── README.md ├── _redirects ├── functions └── graphql.js ├── netlify.toml ├── nodemon.json ├── package-lock.json ├── package.json ├── src ├── index.ts └── server.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /*.env 3 | npm-debug.log 4 | .DS_Store 5 | /templates 6 | # Local Netlify folder 7 | .netlify 8 | /functions/bundle -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Khalil Stemmler 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-typescript-graphql-netlify-starter 2 | ⚡A serverless TypeScript GraphQL API deployed on Netlify 3 | 4 | [![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/stemmlerjs/serverless-typescript-graphql-netlify-starter) 5 | 6 | ## Installation 7 | 8 | Clone and run npm install to install the dependencies. 9 | 10 | ```bash 11 | npm install 12 | ``` 13 | 14 | ### Start locally 15 | 16 | To run the project locally, use the `start` command. 17 | 18 | ```bash 19 | npm run start 20 | ``` 21 | ## About 22 | 23 | This project uses both `apollo-server` and `apollo-server-lambda` so that you can write your GraphQL resolvers and type definitions in one place, yet run GraphQL servers locally and serverlessly. 24 | 25 | Check out [server.ts](https://github.com/stemmlerjs/serverless-typescript-graphql-netlify-starter/blob/master/src/server.ts) for how it works. 26 | -------------------------------------------------------------------------------- /_redirects: -------------------------------------------------------------------------------- 1 | / /.netlify/functions/graphql 200! -------------------------------------------------------------------------------- /functions/graphql.js: -------------------------------------------------------------------------------- 1 | 2 | const { createLambdaServer } = require("./bundle/server") 3 | 4 | const graphQLServer = createLambdaServer(); 5 | 6 | exports.handler = graphQLServer.createHandler({ 7 | cors: { 8 | origin: '*' 9 | } 10 | }); 11 | 12 | -------------------------------------------------------------------------------- /netlify.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | command = "npm run build" 3 | functions = "functions" -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src", "functions"], 3 | "ext": ".ts,.js", 4 | "ignore": [], 5 | "exec": "ts-node --transpile-only ./src/index.ts" 6 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-typescript-graphql-netlify-starter", 3 | "version": "1.0.0", 4 | "description": "A simple serverless GraphQL API built with TypeScript and deployed on Netlify.", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "tsc", 8 | "start": "nodemon", 9 | "codegen": "graphql-codegen --config codegen.yml" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/stemmlerjs/serverless-typescript-graphql-netlify-starter.git" 14 | }, 15 | "keywords": [ 16 | "typescript", 17 | "graphql", 18 | "api", 19 | "netlify" 20 | ], 21 | "author": "Khalil Stemmler ", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/stemmlerjs/serverless-typescript-graphql-netlify-starter/issues" 25 | }, 26 | "homepage": "https://github.com/stemmlerjs/serverless-typescript-graphql-netlify-starter#readme", 27 | "devDependencies": { 28 | "@graphql-codegen/cli": "^1.12.2", 29 | "@graphql-codegen/introspection": "1.12.2", 30 | "@graphql-codegen/typescript": "1.12.2", 31 | "@graphql-codegen/typescript-resolvers": "1.12.2", 32 | "nodemon": "^2.0.2", 33 | "ts-node": "^8.6.2", 34 | "typescript": "^3.7.5" 35 | }, 36 | "dependencies": { 37 | "apollo-server": "^2.10.1", 38 | "apollo-server-lambda": "^2.10.1" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | 2 | import { createLocalServer } from "./server" 3 | 4 | const server = createLocalServer(); 5 | 6 | server.listen().then(({ url }) => { 7 | console.log(`🚀 Server ready at ${url}`); 8 | }); -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | 2 | import { ApolloServer, gql } from 'apollo-server' 3 | import { ApolloServer as ApolloServerLambda } from 'apollo-server-lambda' 4 | 5 | const typeDefs = gql` 6 | type Query { 7 | hello: String 8 | } 9 | `; 10 | 11 | const resolvers = { 12 | Query: { 13 | hello: () => "Hi! Love from @stemmlerjs 🤠." 14 | } 15 | }; 16 | 17 | function createLambdaServer () { 18 | return new ApolloServerLambda({ 19 | typeDefs, 20 | resolvers, 21 | introspection: true, 22 | playground: true, 23 | }); 24 | } 25 | 26 | function createLocalServer () { 27 | return new ApolloServer({ 28 | typeDefs, 29 | resolvers, 30 | introspection: true, 31 | playground: true, 32 | }); 33 | } 34 | 35 | export { createLambdaServer, createLocalServer } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "module": "commonjs", 5 | "moduleResolution": "node", 6 | "pretty": true, 7 | "sourceMap": true, 8 | "target": "es2017", 9 | "outDir": "./functions/bundle", 10 | "lib": ["es6"], 11 | "resolveJsonModule": true, 12 | "types": ["node"], 13 | "typeRoots" : ["./node_modules/@types", "./src/@types"], 14 | "experimentalDecorators": true, 15 | "emitDecoratorMetadata": true, 16 | "esModuleInterop": true 17 | }, 18 | "include": [ 19 | "src/*.ts", 20 | "src/**/*.ts", 21 | "src/**/*.js" 22 | ], 23 | "exclude": [ 24 | "node_modules", 25 | "src/**/*.spec.ts", 26 | "src/**/*.spec.js" 27 | ] 28 | } --------------------------------------------------------------------------------