How to make an API that uses GraphQL queries in Serverless

Oscar de la Hera Gomez
Four flowers that represent Typescript, Serverless, AWS Lambda and GraphQL side by side. Beneath them sits the text "GraphQL Query."

A step by step tutorial on making an API that implements a GraphQL Query protected by an Authorization Bearer token using Typescript & Serverless.

The following tutorial walks you through how to setup a Serverless & Typescript project to work with GraphQL requests and demonstrates how to make a request that uses an authorization token to gather all the Subscribers from a Craft CMS.

We recommend cloning or downloading our Open Source Serverless & Typescript starter project and checking out the main branch and carrying out the steps outlined below. All changes can be found on the tutorial/graphql-query branch.

git clone

Step One: Install graphql-request

A screenshot of Terminal showing you how to run "yarn add graphql-request graphql" along with the response that the terminal gives to install the npm package.

In Terminal, set the current directory to that of your Typescript & Serverless project, and add the graphql-request npm package by running the following line:

yarn add graphql-request graphql

Step Two: Complete the Serverless function

A screenshot of VSCode with the complete code. It has been provided at the end of this how to step with directions.

A | Import the package

A screenshot of VSCode showing you how to import the module.

At the top of your Serverless function add the following line

import { GraphQLClient, gql } from "graphql-request";

If you are new to Typescript, we recommend that you consult our recommended practices for creating structured Serverless Projects & functions, using the links below.

B | Make your Serverless function asynchronous

A screenshot of VSCode showing how we made our function asynchronous.

Add async to your function to make sure that it can work asynchronously, by using similar code to the one below:

const handler = async (event: any, context: any, callback: any) => {}

C | Create a GraphQL client

A screenshot of VSCode showing how we setup our GraphQL Client. Sample code available below.

In your Serverless function, setup a GraphQL client using code similar to the one below:

Please note that it is strongly advised that you use Environment variables for the endpoint and token.

D | Write your query

A screenshot of VSCode showing you how to add your query.

Using the GraphiQL, which can be accessed at, determine the schema of the entry that you wish to create.

In our case, it looks similar to the one below

Implement this into your Serverless function using code similar to the one below:

E | Request and process the data

A screenshot of VSCode showing you how to make the GraphQL request and handle its response. Sample code available below.

Using the GraphQL Client, request the query and process the response and error using code similar to the one below:

The sample code for the complete function can be found below.

Please note that we used our standardized response and log system as part of this final function.

Step Three: Test

A screenshot of Terminal showing the response of the local invocation of the function.

When possible, we strongly recommend testing your Serverless function locally by invoking it through the Terminal. If the function requires to be deployed for testing, we recommend deploying and testing a development stage before sending it out to production.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details