Serverless with GraphQL

Aditya Raman
6 min readSep 9, 2020

--

This article is part of #ServerlessSeptember (https://aka.ms/ServerlessSeptember2020). You’ll find other helpful articles, detailed tutorials, and videos in this all-things-Serverless content collection. New articles from community members and cloud advocates are published every week from Monday to Thursday through September.

Find out more about how Microsoft Azure enables your Serverless functions at https://docs.microsoft.com/azure/azure-functions/

Serverless Compute

Serverless Compute can be thought of Function as a Service (FaaS) or a microservice that is hosted on the Cloud. Behind cloud, the applications are hosted as the stateless compute containers that are event-triggered and ephemeral, but you are not required to manage that server it is dynamically allocated by the service providers like Azure. You are actually using the compute resources which are being triggered by the clients, so that is why you pay only for the compute resources which were used. The best thing about serverless is its ability to auto-scale.

Advantages

  • Lower Operational Cost
  • Easily Scalable
  • Billing is based upon Usage
  • Easy Deployment
  • Low Cost

Disadvantages

  • Complex Architecture
  • Execution Time
  • Execution Frequency

Azure

Azure is a cloud platform, that is used to host applications and manage a lot many services like databases, DNS, etc. It is reliable and easy to use platform. Azure provides different ways to host our application. We can use VMs or serverless. Here we will go with Serverless.

Some Basic Definitions

Azure CLI: The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation.

Azure CLI Commands

Login to Azure

az login

Setting up the subscription
If this step is performed you can skip the subscription argument while creating group, storage and applications. This set as the default subscription on your local system.

# To see all the available subscriptions
az account list
# To set the subscription
account set --subscription <Subscription-ID or Subscription-Name>

Logout of the Account

az logout

Azure Functions: Azure Functions is a serverless application platform. It allows developers to host business logic in different languages like C#, JavaScript, Python, and PowerShell Core. Using this you can use the resources and you will have to pay as per the usage.

Azure DevOps: Azure DevOps is a collection of services for better planning, collaboration and builds faster production level application. This is awesome.

Azure Functions Core Tools: To develop and test the functions locally using a terminal or command prompt. This is required and can be downloaded from here.

The Azure Functions Core Tools provide a local development experience for creating, developing, testing, running, and debugging Azure Functions.

You can view/clone/modify the project from
Azure DevOps:
https://bellatrixdata@dev.azure.com/bellatrixdata/serverless-graphql/_git/serverless-graphql
GitHub: https://github.com/ramanaditya/serverless-graphql.git
Deployed GraphQL API:
https://ramanaditya-graphql.azurewebsites.net/graphql?code=mWybiYT9VGKSkNsZvuzMPajaNiMYvbEBhc6LErNr8hnXdeunrqOR7w==

Creating Functions Project

The command initializes the project and creates a project directory. — worker-runtime denotes the language you want to use in the application

# This will generate a folder containing two files host.json and local.settings.jsonfunc init serverless-graphql --worker-runtime node
cd serverless-graphql

Note: The values of these files can be changed according to the connection strings which you had or will have for DB, storage etc.

Creating Functions Template

To Create a JavaScript HTTP Trigger, this is a sample template to create the HTTP Trigger.

# Here a pre-built template for the Http Trigger will be created
# with two files, function.json and index.js
# We are going to write our logics in the index.js
func new --template "Http Trigger" --name graphql

Replace all the data from your function.json with the following JSON data

{
"disabled": false,
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

Initialize your nodeJs project with npm

npm init
npm init

Install Dependencies

npm install -i apollo-server-azure-functions graphql

Run locally

func start

Create Azure Resource Group

Azure Resource Group is the container to store the resources as the directory in your system. It helps in managing the resources associated with a similar type of solutions or the related one.

az group create --name <Your-Resource-Group-Name> --location eastus

Create Azure Storage

Azure Storage Service is offered by the Azure for a massively scalable object store for data objects, disk storage for Azure virtual machines (VMs), a file system service for the cloud, a messaging store for reliable messaging, and a NoSQL store. They are durable, secure, scalable, managed and accessible.

az storage account create \
--name <name-of-your-storage-bucket> \
--location eastus \
--resource-group <Your-Resource-Group-Name> \
--sku Standard_LRS \
--subscription <your-subscription-id>

Create Function App

Azure Function is a micro service like architecture which allows you to write small pieces of code or business logic to run separately with the event Triggers. They are highly scalable and the billing is with respect to the usage. This allows us to build a scalable application without bothering about managing the servers.

az functionapp create \
--resource-group <Your-Resource-Group-Name> \
--name <Function-App-Name> \
--consumption-plan-location eastus \
--runtime node \
--storage-account <name-of-your-storage-bucket> \
--subscription <your-subscription-id> \
--functions-version 3

Deploying the API

This will publish the application, with the URL as <Function-Name>.azurewebsites.net/

func azure functionapp publish <Function-App-Name>
after publish
Apollo GraphQL Playground

Hola! It has successfully been deployed, you can access your Apollo GraphQL Playground at the invoke URL.

Note: Make sure you put this invoke URL inside the URL section of the playground.

sample query

Yeah, I have built the API for myself, you can check it out at
https://ramanaditya-graphql.azurewebsites.net/graphql?code=mWybiYT9VGKSkNsZvuzMPajaNiMYvbEBhc6LErNr8hnXdeunrqOR7w==

Delete all the created resources

If not in use or being used extensively and you don’t want to get billed for this sample application, delete all the resources you created with the following Azure command.

# It will delete the function app
az functionapp delete \
--resource-group <your-resource-group-name> \
--name <Your-Function-App-Name>
# It will delete the Storage Account
az storage account delete \
--name <Your-Storage-Account-Name> \
--resource-group <your-resource-group-name> \
--yes

#It will delete the Azure Resource
az group delete \
--name <your-resource-group-name> \
--yes

About the Author

Aditya Raman

I am Aditya Raman, Software Engineer and Developer. Also, I am Microsft Learn Student Ambassador. I am an Open Source Contributor who loves DevOps, Backend Engineering and Data Science.

I am working on several live projects which extensively use Data Science.
To contribute to the projects, or if you want me to collaborate in your project, or if you want to get connected with me, please follow the links

--

--

Aditya Raman
Aditya Raman

Written by Aditya Raman

Back End Developer | Software Engineer | DevOps Engineer | Data Science | Microsoft Learn Student Ambassador | Mentor MLH | Full Stack Developer

No responses yet