Skip to main content

Run a GraphQL backend as an Azure Function

I typically run my GraphQL backends as an Azure App Service or Azure Container app, but today I had to create a really small API. I was wondering, could I use an Azure Function to host this API? Let's find out in this post.

Creating an Azure Function App

Let’s start by creating our Azure Function App. I’ll use Visual Studio in this example but you could perfectly use Visual Studio Code as well.

  • Open Visual Studio and choose Create a new project.
  • In Create a new project, enter functionsin the search box, choose the Azure Functions template, and then select Next.

  • In Configure your new project, specify a project name and click Next.

  • in Additional Information, set the following values:
    • Functions worker: .NET 6 (there is no reason to run this as an isolated worker)
    • Function: HTTP Trigger
    • Authorization level: Anonymous 

    • Click on Create to create our Function app and the HTTP Trigger function.
    • We’ll update our function later but let’s keep it for now just to test if our function works.
      • Run the function app and call the API(you can find the url in the output of the console).

      Integrate the HotChocolate GraphQL server

      Starting from HotChocolate 11, integrating with an Azure Function App became easy thanks to the HotChocolate.AzureFunctions nuget package.

      • Right click on Dependencies and choose Manage NuGet packages… from the context menu.
      • Search for HotChocolate.Azure and install the HotChocolate.AzureFunctions NuGet package.

      • Update our function to listen on the ‘/graphql’ route and invoke the IGraphQLRequestExecutor:
      • We also need to introduce a Startup class to configure our GraphQL middleware. Therefore we first need to add reference to the Microsoft.Azure.Functions.Extensions nuget package (https://www.nuget.org/packages/Microsoft.Azure.Functions.Extensions).
      • Add a Startup.cs file and paste the following code (notice the specific AddGraphQLFunction() method):
      • We also add a Query class to manage our GraphQL types and queries:
      • Run the Function app locally and browse to the /graphql endpoint. Banana Cake Pop will be opened and we can try to execute a GraphQL query:

        You could further optimize this Azure Function for quick startups by using distributed caches for the query engine. But I’ll leave that for another post.

        Remark: I noticed that I could have saved myself some time and used the HotChocolate templates:

        D:\Projects\Test\GraphQLFunctionApp2>dotnet new -i HotChocolate.Templates
        The following template packages will be installed:
           HotChocolate.Templates

        Success: HotChocolate.Templates::12.12.1 installed the following templates:
        Template Name                  Short Name   Language  Tags
        -----------------------------  -----------  --------  -----------------
        HotChocolate GraphQL Function  graphql-azf  [C#]      Web/GraphQL/Azure
        HotChocolate GraphQL Server    graphql      [C#]      Web/GraphQL

        D:\Projects\Test\GraphQLFunctionApp2>dotnet new graphql-azf
        The template "HotChocolate GraphQL Function" was created successfully.

        Deploy our GraphQL function to Azure

        Almost there, let's take the last mile and deploy our GraphQL function.

        • Right click on the Azure Function and choose Publish from the context menu.
        • In the Publish window, choose Azure and click Next.

        • In the Specific Target window, choose Azure Function App(Linux) and click Next.

        • Click on the '+' sign to create a new Azure Function app on Azure.

        • Enter all the details and click on Create. This can take a while to have your App Service created.

        • Click on Finish to continue.

        • Choose Pubxml as your deployment type and hit Finish to complete the process.

        • Hit on Publish to publish the Azure Function App. Once this is done you can browse to the graphql endpoint of your Azure Function URL(/api/graphql in this example.

        You can find the complete example code here: https://github.com/wullemsb/GraphQLFunctionAppExample

        Popular posts from this blog

        Azure DevOps/ GitHub emoji

        I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

        Kubernetes–Limit your environmental impact

        Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

        Podman– Command execution failed with exit code 125

        After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...