Eklee.Azure.Functions.GraphQl 0.2.0

The purpose of this library is to help developers with implementing a GraphQl based Azure Function with dependency injection support.

There is a newer version of this package available.
See the version list below for details.
Install-Package Eklee.Azure.Functions.GraphQl -Version 0.2.0
dotnet add package Eklee.Azure.Functions.GraphQl --version 0.2.0
<PackageReference Include="Eklee.Azure.Functions.GraphQl" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Eklee.Azure.Functions.GraphQl --version 0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Introduction

The purpose of this library is to help developers with implementing a GraphQl based Azure Function with dependency injection support.

DI Usage

In order to leverage this library, there are 3 steps. You would want to setup your DI, apply the ExecutionContextDependencyInjection attribute, and inject the ExecutionContext as a parameter in your function.

Step 1: Setup DI

The first step is to setup your DI via the Autofac Module. Be sure to register your schema using the extension method RegisterGraphQl. You can then register the types used in your schema.

using Autofac;

namespace FunctionApp1
{
    public class MyModuleConfig : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGraphQl<BooksSchema>();
            builder.RegisterType<BooksQuery>();
            ...
        }
    }
}

Step 2/3: Setup ExecutionContextDependencyInjection attribute on said function and inject ExecutionContext.

The second step is to apply the ExecutionContextDependencyInjection on your function and tell it which Module type you would like. Next, you can inject the ExecutionContext which internally carries the function instance Id.

public static class BooksGraphFunction
{
    [ExecutionContextDependencyInjection(typeof(MyModule))]
    [FunctionName("graph")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "books/graph")] HttpRequest req,
        ILogger log,
        ExecutionContext executionContext)
    {	

Process GraphQl Request Usage:

Simply leverage the extension method ProcessGraphQlRequest.

return await executionContext.ProcessGraphQlRequest(req);

For more information about dependency injection support, visit: https://github.com/seekdavidlee/Eklee-Azure-Functions-Http

Caching Usage:

You can enable built-in caching capabilities per GraphQl best practices based on object type and instance Id. Please follow the steps below:

In your Module setup, use the extension method EnableGraphQlCache. Note that MemoryDistributedCache is just an example. In a production senario, you may choose something like Azure Redis.

builder.EnableGraphQlCache<MemoryDistributedCache>();

You can use a convention based approach to identify cache instances based on object type and instance Id by decorating with the KeyAttribute on your object type.

using System.ComponentModel.DataAnnotations;
...
    public class Book
    {
        [Key]
        public string Id { get; set; }

In your Query resolvers, you can use the extension method ResolverWithCache to create a resolver with caching support.

The first "book" query is based on the Book object type's Id property to derive "id" from the context as a key to get the instance Id value. It is cached for 10 seconds.

The second "books" query will derive "category" from the context as a key to get the instance Id value. It is cached for 10 seconds.

Once the cache expires, the respository query will be executed and persisted into the cache for the duration of time specified.

using GraphQL.Types;

namespace Eklee.Azure.Functions.GraphQl.Example.BusinessLayer
{
    public class BooksQuery : ObjectGraphType<object>
    {
        public BooksQuery(BooksRepository booksRepository, IGraphQlCache graphQlCache)
        {
            Name = "Query";

            Field<BookType>("book",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the book" }),
                resolve: graphQlCache.ResolverWithCache(key => booksRepository.GetBook((string)key), 10));

            Field<ListGraphType<BookType>>("books",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "category", Description = "category of the book" }),
                resolve: graphQlCache.ResolverWithCache(key => booksRepository.GetBooks((string)key), 10, "category"));

        }
    }
}

Tracing support:

To enable support for tracing, please add set EnableMetrics configuration to true under GraphQl.

{
    ...
    "GraphQl": {
      "EnableMetrics": "true" 
    } 
}

Introduction

The purpose of this library is to help developers with implementing a GraphQl based Azure Function with dependency injection support.

DI Usage

In order to leverage this library, there are 3 steps. You would want to setup your DI, apply the ExecutionContextDependencyInjection attribute, and inject the ExecutionContext as a parameter in your function.

Step 1: Setup DI

The first step is to setup your DI via the Autofac Module. Be sure to register your schema using the extension method RegisterGraphQl. You can then register the types used in your schema.

using Autofac;

namespace FunctionApp1
{
    public class MyModuleConfig : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGraphQl<BooksSchema>();
            builder.RegisterType<BooksQuery>();
            ...
        }
    }
}

Step 2/3: Setup ExecutionContextDependencyInjection attribute on said function and inject ExecutionContext.

The second step is to apply the ExecutionContextDependencyInjection on your function and tell it which Module type you would like. Next, you can inject the ExecutionContext which internally carries the function instance Id.

public static class BooksGraphFunction
{
    [ExecutionContextDependencyInjection(typeof(MyModule))]
    [FunctionName("graph")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "books/graph")] HttpRequest req,
        ILogger log,
        ExecutionContext executionContext)
    {	

Process GraphQl Request Usage:

Simply leverage the extension method ProcessGraphQlRequest.

return await executionContext.ProcessGraphQlRequest(req);

For more information about dependency injection support, visit: https://github.com/seekdavidlee/Eklee-Azure-Functions-Http

Caching Usage:

You can enable built-in caching capabilities per GraphQl best practices based on object type and instance Id. Please follow the steps below:

In your Module setup, use the extension method EnableGraphQlCache. Note that MemoryDistributedCache is just an example. In a production senario, you may choose something like Azure Redis.

builder.EnableGraphQlCache<MemoryDistributedCache>();

You can use a convention based approach to identify cache instances based on object type and instance Id by decorating with the KeyAttribute on your object type.

using System.ComponentModel.DataAnnotations;
...
    public class Book
    {
        [Key]
        public string Id { get; set; }

In your Query resolvers, you can use the extension method ResolverWithCache to create a resolver with caching support.

The first "book" query is based on the Book object type's Id property to derive "id" from the context as a key to get the instance Id value. It is cached for 10 seconds.

The second "books" query will derive "category" from the context as a key to get the instance Id value. It is cached for 10 seconds.

Once the cache expires, the respository query will be executed and persisted into the cache for the duration of time specified.

using GraphQL.Types;

namespace Eklee.Azure.Functions.GraphQl.Example.BusinessLayer
{
    public class BooksQuery : ObjectGraphType<object>
    {
        public BooksQuery(BooksRepository booksRepository, IGraphQlCache graphQlCache)
        {
            Name = "Query";

            Field<BookType>("book",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the book" }),
                resolve: graphQlCache.ResolverWithCache(key => booksRepository.GetBook((string)key), 10));

            Field<ListGraphType<BookType>>("books",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "category", Description = "category of the book" }),
                resolve: graphQlCache.ResolverWithCache(key => booksRepository.GetBooks((string)key), 10, "category"));

        }
    }
}

Tracing support:

To enable support for tracing, please add set EnableMetrics configuration to true under GraphQl.

{
    ...
    "GraphQl": {
      "EnableMetrics": "true" 
    } 
}

Release Notes

Added support for caching and enabling tracing.

Version History

Version Downloads Last updated
0.15.3 32 7/14/2019
0.15.2 39 7/7/2019
0.15.1 35 7/6/2019
0.14.1 48 6/16/2019
0.14.0 77 5/18/2019
0.13.1 92 3/31/2019
0.13.0 77 3/31/2019
0.12.1 91 3/24/2019
0.11.0 83 3/10/2019
0.10.0 88 3/3/2019
0.9.1 93 2/26/2019
0.9.0 96 2/24/2019
0.8.0 92 2/23/2019
0.7.2 95 2/20/2019
0.7.1 101 2/17/2019
0.6.3 112 2/10/2019
0.5.5 137 1/20/2019
0.5.4 173 1/13/2019
0.5.1 116 1/7/2019
0.4.6 138 12/31/2018
0.4.5 112 12/28/2018
0.4.4 185 12/27/2018
0.3.0 132 11/4/2018
0.2.0 139 11/4/2018
0.1.0 114 10/30/2018
Show less