HotChocolate GraphQL Azure Function NET 5.0 (isolated process)
This article shows running HotChocolate GraphQL server on C# functions using .NET 5.0, which run out-of-process from the Azure Functions runtime. You’ll learn how to create, debug locally, and publish these .NET isolated process functions to Azure.
Prerequisites
· An Azure account with an active subscription. Create an account for free.
· Azure Functions Core Tools version 3.0.3381, or a later version.
· Azure CLI version 2.20, or a later version.
· Visual Studio Code on one of the supported platforms.
· The C# extension for Visual Studio Code.
· The Azure Functions extension for Visual Studio Code, version 1.3.0 or newer.
Create a local function project
- Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project… icon.
2. Choose a directory location for your project workspace and choose Select.
3. Provide the following information at the prompts:
a. Select a language for your function project: Choose C#
.
b. Select a .NET runtime: Choose .NET 5 isolated
.
c. Select a template for your project’s first function: Choose HTTP trigger
.
d. Provide a function name: Type HttpExample
.
e. Provide a namespace: Type GraphQL.Functions
.
f. Authorization level: Choose Anonymous
, which enables anyone to call your function endpoint. To learn about authorization level, see Authorization keys.
g. Select how you would like to open your project: Choose Add to workspace
.
4. Using this information, Visual Studio Code generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. To learn more about files that are created, see Generated project files.
HotChocolate
is an implementation of GraphQL and a framework for writing GraphQL servers in .Net Core.
Packages that we need:
Now let us start developing, the first thing we need to do is create our models, repositories & query classes.
Add HotChocolate GraphQL middleware for azure function.
Next, we need to update ConfigureServices inside Program.cs class.
Filtering, Sorting & Projections are part of HotChocolate.Data package.
⚠️ Note: If you use more than one middleware, keep in mind that ORDER MATTERS. The correct order is UsePaging > UseProjections > UseFiltering > UseSorting
Run the function locally
Now, it’s pretty much done. Just run the solution locally to test the outcome.
To call your function, press F5 to start the function app project. Output from Core Tools is displayed in the Terminal panel. Your app starts in the Terminal panel. You can see the URL endpoint of your HTTP-triggered function running locally.
We can use postman to test the Graph API endpoints
- Get All user profiles
2.Filtering
3.Sorting
Conclusion
As of now I’ve implemented filtering & sorting. Hopefully, I’ll come-up with more articles on some of the advanced concepts like Projection, Stitched Schema & Authorization.
You can get the source from GitHub using this link:
https://github.com/chandsalam1108/GraphQLAzFunctionNet5.git
Happy Coding!!!