This is a GraphQL server built using latest Apollo Server V4
, Typescript
and Node.js
.
Some of the tools used in this project including
Serverless
for building and deploying serverless applications to cloud platform like AWS Lambda.@as-integrations/aws-lambda
helps you to integrate your Apollo Server with AWS Lambda functions.graphql-codegen
to generate TypeScript types for the GraphQL types in a schema. This can help improve the type safety and developer experience when working with a GraphQL API.graphql-request
to help fetch data from other GraphQL APIs.@apollo/datasource-rest
to help fetch data from REST APIs and helps handle caching.prettier
for linting purpose.nodemon
to hotreload Node.js application when detecting changes in the source code.ts-node
to run TypeScript code directly without needing to compile it to JavaScript.
- Node.js
- npm
- Serverless
- AWS CLI
- Clone this repository
- Navigate to the project directory
- Install the dependencies by running
npm install
- Generate all typescript files by running
npm run prepare
- After
AWS Configure
, deploy the server by runningserverless deploy
, you can specify the stage and region by attaching params like soserverless deploy --stage production --region ap-southeast-2
To start the server in development mode, run npm run start:dev
. This will start the server with hot reloading enabled using nodemon
, so any changes you make to the code will be reflected in the running server.
To start the server in production mode, run npm run start
. This will build the server and start it without hot reloading.
Before deploying, we can use the Serverless CLI to invoke our handler locally to ensure everything is working. We'll do this by mocking an HTTP request with a GraphQL operation. A mockquery.json
file has been provided.
Simply run serverless invoke local -f graphql -p mockquery.json
, if you see some response in the console like this. Congrats, you're good to go!
{
"statusCode": 200,
"headers": {
"cache-control": "no-store",
"content-type": "application/json; charset=utf-8",
"content-length": "78"
},
"body": "{\"data\":{\"getCountry\":{\"name\":\"China\",\"emoji\":\"🇨🇳\",\"native\":\"中国\"}}}\n"
}
npm run sls-offline
: run your Serverless Framework service locally. It allows you to simulate the AWS Lambda and Amazon API Gateway environment locally and test your serverless functions without having to deploy them to the cloud.npm run start:dev
: starts the server in development mode with hot reloading enablednpm run start
: builds and starts the server in production modenpm run compile
: compile the Typescript files into Javascript filesnpm run format
: format all ts, js and json files using prettiernpm run codegen-server
: only generate the needed TypeScript types for the GraphQL schema as a GraphQL servernpm run codegen-client
: only generate the needed TypeScript types for the GraphQL schema as a GraphQL clientnpm run prepare
: generate all needed TypeScript types for the GraphQL schema using graphql-codegen.
To access the GraphQL Playground, go to http://localhost:4000/graphql
in your web browser.
This project is licensed under the [MIT] License - see the LICENSE file for details.