Creating Serverless GraphQL API with AWS Lambda

Creating Serverless GraphQL API with AWS Lambda

Application programming interfaces, or APIs, provide a helping hand in facilitating communication between various software systems. It is useful for transferring data from one system to another. The serverless architecture offers a trustworthy, faster, and more scalable option to work with the APIs. With the API deployment to the cloud service providers, you do not need to worry about server administration.

 GraphQL is regarded as the query language, which plays an integral role in simplifying client-transfer interactions. AWS Lambda is regarded as the serverless computing platform, along with the pay-for-billing model, which provides a suitable choice to execute the code without the need to worry about server management and provisioning. As you go through this write-up, you will be able to create a serverless gaphQL API endpoint, thereby deploying the same to AWS.

Benefits of the GraphQL API

GraphQL helps create the uniform API across the specific app, which helps resolve the API evolution and the API versioning issues. So,the potential users will be able to eradicate the deprecated fields without the need to affect the existing queries and the schema. The GraphQL APIs offer constant access to the application’s latest features, thereby ensuring more maintainable and cleaner server code while using the single evolving version.

In GraphQL, all the response fields and requests should conform to the previously declared type. It reduces the total number of bugs significantly. The potential client will specify the fields, which are retrieved from the request, and the server will return the fields.

The graphQL ensures that it is easy to request for the potential users the specific posts for the commends in a single query. GraphQL is useful as it helps in making requests to the singular endpoint. The data needs to be returned, and the operation needs to be performed with the query determination, which is specified by the potential users within the request.

Steps for AWS Lambda GraphQL integration

Now, we are going to talk about the steps that facilitate the AWS Lambda graphQL integration, where you will be able to create the GraphQL server, after which it is deployed on AWS Lambda.

Creating a GraphQL server

In the first step, you need to open CLI, after which you should execute the given command to download and install the packages for the AWS Lambda GraphQL Integration. The serverless offline is useful in developing and testing the app locally.

Now, we are going to talk about the steps that help create a GraphQL server:

1.Setting the GraphQL Handler

At first, you should open the specific project in the source code editor to search the handler.js file and modify it. Now, we need to update the given file to add the schema and resolvers to the file and build the Apollo server.

const { ApolloServer } = require(‘apollo-server-lambda’);

const { resolvers } = require(‘./graphql/resolvers’);

const { typeDefs } = require(‘./graphql/schema’);

const server = new ApolloServer({

  typeDefs,

  resolvers,

});

module.exports.graphqlHandler = server.createHandler({

  cors: {

origin: ‘*’,

credentials: true,

  },

});

2.Setting the schema

In the specific use case, you should use the retrieved hardcoded value through GraphQL. The specific schema file defines the look and type of the data. Now, you should create the file, referred to as schema.js, within the folder, referred to as graphql, in the root of the project, after which you should paste the given content on the same.

const { gql } = require(‘apollo-server-lambda’);

module.exports.typeDefs = gql`

  type Orders {

id: String!

amount: Float!

tax: Float!

total: Float!

  }

  type Query {

getOrders: [Orders]

  };

3.Setting the resolvers

Resolvers are regarded as the files, which offer a set of instructions to the GraphQL regarding the queries and the ways to return the data. After this, you should come up with the resolver.js file, beneath the graphql folder, after which the content should be pasted.

module.exports.resolvers = {

  Query: {

getOrders: (_, args) => {

   const orders = [

     {

          id: ‘1977’,

          amount: 10.0,

          tax: 0.5,

          total: 10.5,

     },

     {

          id: ‘1978’,

          amount: 20.0,

          tax: 1.0,

          total: 21.0,

     },

     {

          id: ‘1979’,

          amount: 30.0,

          tax: 1.5,

          total: 31.5,

     },

   ];

   return orders;

},

  },

};

Setup the severeless.yml file.

From the serverless-offline, modify the serverless.yml file and paste the following content into it:

service: serverless-graphql

plugins:

  – serverless-offline

provider:

  name: aws

  runtime: nodejs12.x

  stage: ${opt:stage, ‘dev’}

  region: us-east-1

  memorySize: 128

functions:

  graphql:

handler: handler.graphqlHandler

events:

– http:

        path: graphql

        method: ANY

        cors: true

Local testing

Once everything remains in place, you should execute the GraphQL server in a local manner. Now, you should refer to the project directory root, after which you need to run the given command:

Serverless offline

After this, you should open the postman and use the http://localhost:3000/dev/graphql url to test the specific query to ensure AWS Lambda GraphQL integration.

Deployment to AWS

After testing the local query in a successful manner, you need to deploy the AWS Lambda GraphQL. If you want to deploy GraphQL on the AWS Lambda, you should possess the active AWS account with enough privileges to access Lambda and IAM.

AWS Lambda GraphQL deployment is recognized as a straightforward process. Now, you should execute the below command for deployment to AWS:

Serverless deploy

If the AWS Lambda GraphQL deployment is a grand success, you will witness the output results of the AWS Lambda GraphQL integration.

With the success of the AWS Lambda GraphQL deployment, you should test the AWS end point within Postman. Now, you should offer the given post request along with an AWS Lambda GraphQL API call response. Now, you will receive the endpoint information within the success message present within the terminal.

query {

 orders {

   id

   amount

   tax

   total

 }

}

Following the below steps helps complete AWS Lambda GraphQL integration, in which you need to create the GraphQL server and deploy the same through the AWS Lambda use.

After the successful GraphQL server deployment on the AWS Lambda server, the server will receive different graphQL requests from the potential client, after which it will return the response.