|
1 | | -# simple-aws-sqs-lambda-microservices |
| 1 | +# simple-aws-sqs-lambda-microservices |
| 2 | +Simple [GraphQL](https://graphql.org/) APIs implementation using [Node JS](https://nodejs.org/en/docs/) and [AWS Lambda](https://aws.amazon.com/lambda/) with [Apollo Server](https://www.apollographql.com/docs/apollo-server/) and [MongoDB Atlas](https://www.mongodb.com/docs/atlas/). |
| 3 | + |
| 4 | +This example illustrates how to deploy [GraphQL](https://graphql.org/) APIs using [NodeJS](https://nodejs.org/en/docs/) functions running on [AWS Lambda](https://aws.amazon.com/lambda/) using the traditional [Serverless](https://www.serverless.com/framework/docs/providers/aws/guide/intro) Framework. The deployed functions work with [MongoDB Atlas](https://www.mongodb.com/docs/atlas/). |
| 5 | + |
| 6 | +To work with [GraphQL](https://graphql.org/) features, i.e. **Type Definitions, Mutations, Queries, Resolvers** [Apollo Server](https://www.apollographql.com/docs/apollo-server/) is used. |
| 7 | + |
| 8 | +This Example works with [AWS HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop.html) events. |
| 9 | + |
| 10 | +<!--- |
| 11 | +For faster response with the APIs [Redis](https://redis.io/) *caching* is used. |
| 12 | +For *session tracking* [JSON Web Token (JWT)](https://jwt.io/) is used. |
| 13 | +--> |
| 14 | + |
| 15 | +All *logs* for the function is kept in [AWS Cloudwatch](https://aws.amazon.com/cloudwatch/) i.e *persistent*. |
| 16 | + |
| 17 | +To use the code in this example you **must** have an valid [AWS account](https://aws.amazon.com/account/) and necessary [AWS IAM](https://aws.amazon.com/iam/) roles and programmatic access to an user. You **must** have a [MongoDB Atlas](https://www.mongodb.com/docs/atlas/) account as well as an [AWS EC2](https://aws.amazon.com/ec2/) instance should be installed with [Redis](https://redis.io/). |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Features |
| 22 | +1. [AWS Lambda](https://aws.amazon.com/lambda/) function using [NodeJS](https://nodejs.org/en/docs/) |
| 23 | +2. Function is using latest version of [AWS SDK JavaScript v3](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/welcome.html) with all **ES6+** syntaxes like Promises, `async/await` |
| 24 | + |
| 25 | +<ol start="3"> |
| 26 | + <li> |
| 27 | + Function are deployed using <a href="https://www.serverless.com/framework/docs/providers/aws/guide/intro">Serverless</a> Framework. |
| 28 | + </li> |
| 29 | + <li> |
| 30 | + <code>serverless.json</code> is used for deployment configuration instead of <code>serverless.yml</code>. |
| 31 | + </li> |
| 32 | + <li> |
| 33 | + All the deployment is created in <a href="https://aws.amazon.com/s3/">AWS S3</a> to store the <code>.zip</code> of the function code and <a href="https://aws.amazon.com/cloudformation/">AWS CloudFormation</a> Stack. |
| 34 | + </li> |
| 35 | +</ol> |
| 36 | + |
| 37 | + |
| 38 | +<!--- |
| 39 | +6. For **session tracking** [JWT](https://jwt.io/) is used. |
| 40 | +--> |
| 41 | +6. [AWS HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop.html) are using [AWS API GateWay](https://aws.amazon.com/api-gateway/) |
| 42 | + |
| 43 | +<ol start="7"> |
| 44 | + <li> All data is saved in <a href="https://www.mongodb.com/docs/atlas/">MongoDB Atlas</a> i.e. <i>persistent</i> |
| 45 | + <!--- <li> <strong>Caching</strong> is used for faster response in the APIs. <a href="https://redis.io/">Redis</a> is used for that purpose</li> --> |
| 46 | + <li> This APIs can also be consumed by any <b>Frontend Application</b>.</li> |
| 47 | + <li> To use <a href="https://graphql.org/">GraphQL</a> features <a href="https://www.apollographql.com/docs/apollo-server/">Apollo Server</a> is used |
| 48 | + <li> For the <i>Schema</i> generation <b>Type Definitions</b> are added. <b>Queries</b> are used for the <i>Reading</i> operations while <b>Mutations</b> are added for <i>Mutable</i> operations. |
| 49 | +</ol> |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +11. [NPM](https://www.npmjs.com/) dependencies are used for various purposes. |
| 54 | + |
| 55 | + |
| 56 | +## Usage |
| 57 | + |
| 58 | +First clone the repo |
| 59 | + |
| 60 | +```bash |
| 61 | +$ git clone git@github.com:anijitsahu/simple-apollo-server-graphql-lambda.git |
| 62 | +``` |
| 63 | +Install all the necessary dependencies by going inside the directory |
| 64 | + |
| 65 | +```bash |
| 66 | +$ cd simple-apollo-server-graphql-lambda |
| 67 | +$ npm install |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +### Deployment |
| 72 | + |
| 73 | +In order to deploy the example, you need to run the following command: |
| 74 | + |
| 75 | +``` |
| 76 | +$ serverless deploy |
| 77 | +``` |
| 78 | + |
| 79 | +### Invocation |
| 80 | + |
| 81 | +After successful deployment, you can invoke the deployed **functions / resolvers**. |
| 82 | + |
| 83 | +However, to call using [GraphQL](https://graphql.org/) API you can use any *supported* Client like [Altair GraphQL Client](https://chrome.google.com/webstore/detail/altair-graphql-client/flnheeellpciglgpaodhkhmapeljopja?hl=en) with the `url` and *HTTP Verbs* as shown in Terminal after using `serverless deploy`. |
| 84 | + |
| 85 | +## API Listing |
| 86 | + |
| 87 | +API listing is given below, - |
| 88 | + |
| 89 | +**POST** /url-of-the-deployed-lambda/graphql have the following input JSON |
| 90 | +```javascript |
| 91 | +{ |
| 92 | + "itemPurchased": [ |
| 93 | + { |
| 94 | + "itemName":"Madhur Sugar", |
| 95 | + "qtyPurchased":"7" |
| 96 | + } |
| 97 | + ], |
| 98 | + "storeName": "Grofers" |
| 99 | +} |
| 100 | +``` |
0 commit comments