Create Azure Functions with GraphQL Support
December 05, 2021 by Anuraj
DotNetCore GraphQL Serverless Azure Azure Functions
This post is about creating .NET Core Azure Functions with GraphQL Support. For supporting GraphQL we are using the HotChocolate
package. First we need create an Azure Function using the func init GraphQL-Azure-Function --dotnet
command. Or if you’re using VSCode create a function with dotnet runtime and HTTP Trigger. Next we need to add the HotChocolate.AzureFunctions
package - which will help you to configure the Azure Function with GraphQL attribute. Here is the code for the Azure Function.
Next we need to configure the startup class for the Azure Function so that the IGraphQLRequestExecutor
instance can be injected to the Run
method. We need to add reference of Microsoft.Azure.Functions.Extensions
package. I already a wrote a blog post on How to add startup class for an Azure Function. Once we added the package, create a class Startup
and add the following code.
And finally we need to implement the Query
class, here is the implementation.
Now we completed the implementation. Lets run the function using func start
command. You will see a page like this once you browse the http://localhost:7071/api/graphql
URL.
This way we will be able to implement GraphQL in Azure Functions. It is a new feature in HotChocolate package which offers out of the box support for Azure Functions. And if you install HotChocolate templates, you can install them using dotnet new -i HotChocolate.Templates
command and you can create an Azure Function with GraphQL support with this dotnet new graphql-azf
command.
You can find the source code in GitHub
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub