December 05, 2021 by Anuraj Estimated read time : 4 mins
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.