GraphQL.AzureFunctionsProxy 11.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GraphQL.AzureFunctionsProxy --version 11.0.0                
NuGet\Install-Package GraphQL.AzureFunctionsProxy -Version 11.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GraphQL.AzureFunctionsProxy" Version="11.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GraphQL.AzureFunctionsProxy --version 11.0.0                
#r "nuget: GraphQL.AzureFunctionsProxy, 11.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GraphQL.AzureFunctionsProxy as a Cake Addin
#addin nuget:?package=GraphQL.AzureFunctionsProxy&version=11.0.0

// Install GraphQL.AzureFunctionsProxy as a Cake Tool
#tool nuget:?package=GraphQL.AzureFunctionsProxy&version=11.0.0                

GraphQL.AzureFunctionsProxy

An (Unofficial) Extension pack for using HotChocolate GraphQL framework within Azure Functions for v11.

Note: Updated Repo & Package names to eliminate conflicts with the core HotChocolate packages.

Overview

This is a extension package for HotChocolate GraphQL framework to enable execution within AzureFunctions using a simple Proxy so that all original functionality of functions endpoints are unaffected.

This is Unofficial but working for most common use cases.

This also includes a working example of the StarWars Project running as an Azure Function and modified only as needed to run as expected (with v11 API)!

Nuget Package (.netcoreapp3.0)

To use this as-is in your project, add the GraphQL.AzureFunctionsProxy NuGet package to your project. and wire up your Starup and AzureFunction endpoint as outlined below...

Demo Site (Star Wars)

This project contains a clone of the HotChocolate GraphQL Star Wars example project (Pure Code First version) running as an AzureFunctions app and mildly updated to use the new v11 API.

HotChocolate has changed the Execution pipeline for v11 API in many ways, and existing AzureFunctions implementation samples don't account for various common use cases like BatchRequests, etc.

NOTES:

  1. NOTE: According to the HotChocolate team on Slack, they will provide an Official AzureFunctions middleware as part of v11 (eventually). 😃
  2. WARNING: Limited Testing has been done on this but I am actively using it on projects, and will update with any findings.

Goals

  • To provide a working approach to using the new v11 API until an official Middleware is provided.
  • Keep this code fully encapsulated so that switching to the official Middleware will be as painless and simple as possible (with a few design assumptions aside).
  • Keep this adaptation layer as lean and DRY as possible while also supporting as much OOTB functionality as possible.
  • Ensures that the Azure Functions paradigm and flexibility are not lost, so that all OOTB C# bindings, DI, and current Function invocation are maintained.

Implementation:

This approach uses a "Middleware Proxy" pattern whereby we provide the functionality of the HotChocolate HTTP middleware via a proxy class that can be injected into the Azure Function, but otherwise do not change the existing AzureFunctions invocation pipeline.

This Proxy exposes an "executor" interface that can process the HttpContext in an AzureFunction. However, any pre/post logic could be added before/after the invocation of the executor proxy IGraphQLAzureFunctionsExecutorProxy.

This proxy is setup by internally configuring a Middleware Proxy that is an encapsulation of the existing HttpPostMiddleware & HttpGetMiddleware configured as a simple pipeline for processing POST requests first and then defaulting back to GET requests, and erroring out if neither are able to handle the request.

Key Elements:

Startup Configuration

  1. The following Middleware initializer must be added into a valid AzureFunctions Configuration 'Startup.cs'
  • All other elements of HotChocolate initialization are the same using the v11 API.
        //Finally Initialize AzureFunctions Executor Proxy here...
        services.AddAzureFunctionsGraphQL();
  • Note: The namespace for this new middleware and proxy classes as needed is:
using HotChocolate.AzureFunctionsProxy
  1. Dependency Inject the new IGraphQLAzureFunctionsExecutorProxy into the Function Endpoint:
using HotChocolate.AzureFunctionsProxy
using....

public class StarWarsFunctionEndpoint
{
    private readonly IGraphQLAzureFunctionsExecutorProxy _graphqlExecutorProxy;

    public StarWarsFunctionEndpoint(IGraphQLAzureFunctionsExecutorProxy graphqlExecutorProxy)
    {
        _graphqlExecutorProxy = graphqlExecutorProxy;
    }
  1. Finally, the IGraphQLAzureFunctionsExecutorProxy can be invoked in the AzureFunction invocation:
        [FunctionName(nameof(StarWarsFunctionEndpoint))]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "graphql")] HttpRequest req,
            ILogger logger,
            CancellationToken cancellationToken
        )
        {
            logger.LogInformation("C# GraphQL Request processing via Serverless AzureFunctions...");

            return await _graphqlExecutorProxy.ExecuteFunctionsQueryAsync(
                req.HttpContext,
                logger,
                cancellationToken
            );
        }

Disclaimers:

  • Subscriptsion were disabled in the example project due to unknown supportability in a serverless environment.
    • The StarWars example uses in-memory subscriptions which are incongruent with the serverless paradigm of AzureFunctions.

Credits:

  • Initial thoughts around design were adapted from OneCyrus' Repo located here.
    • OneCyrus' example is designed around HotChocolate v10 API and the execution & configuration pipelines have changed significantly in the new v11 API.
    • This example also did not support BatchRequests, Extension values, etc...
    • Therefore, rather than manually processing request as this prior example did, this approach is different and leverages alot more OOTB code from HotChocolate.AspNetCore
  • The HotChocolate Slack channel was helpful for searching and getting some feedback to iron this out quickly.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on GraphQL.AzureFunctionsProxy:

Package Downloads
GraphQL.AzureFunctionsProxy.IsolatedProcess

This is a extension package for HotChocolate GraphQL framework to enable execution within AzureFunctions V3 with .Net 5 isolated process model (e.g. out-of-process). It enables the new HotChocolate GraphQL v12 API and provides very easy integration with Azure Functions with maximum support for out-of-the-box HotChocolate functionality -- including Schema Download & Banana Cake Pop Web IDE.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
12.5.0 31,758 1/26/2022 12.5.0 is deprecated because it is no longer maintained.
12.4.1 1,136 1/10/2022
12.0.0.1 1,978 9/28/2021
11.3.8.1 2,306 9/28/2021
11.0.7.1 12,097 2/2/2021
11.0.4.1 2,871 12/18/2020
11.0.1.1 978 12/6/2020
11.0.0.3 247 11/17/2020
11.0.0 307 11/6/2020

Changed Repo & Package names to eliminate conflict risks with the core HotChocolate packages.

Updated this package to now be stable v11.0.0 (no longer preview).  Additional testing and use has showwn this to be stable and actively used in projects in preparation for v11 official release. Bumped HotChocolate version to v11-preview.176.