Git Product home page Git Product logo

eklee-azure-functions-graphql's Introduction

Introduction

The purpose of this library is to help developers with implementing their API on a GraphQL Server running on top of Azure HTTP Function. The library will have resolver support for different Azure-specific repositories like Azure Cosmos DB. If you are not sure what GraphQL is, the best resource would be to review the documentation on https://graphql.org/.

Breaking Changes from 0.30 and above

  • It is recommanded to use 0.32 and higher given 0.31 does not support paging and this is resolved in 0.32.
  • DateTime is now returning ISO 8601 format.
  • We are now on .NET Standard 2.1.

Nuget

You can find this library on nuget: https://www.nuget.org/packages/Eklee.Azure.Functions.GraphQl.

DevOps

Example Project(s)

Getting started

Let's start by exposing a HTTP Function to serve your API via GraphQL server. There are 3 steps.

Step 1: Setup dependency injection (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 your mutation and query used in the schema. Please refer to the topics below for specific details on setting up your mutations and query classes.

using Autofac;

namespace Eklee.Examples
{
    public class MyModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGraphQl<MySchema>();
            builder.RegisterType<MyQuery>();
            builder.RegisterType<MyMutation>();
            ...
        }
    }
}

Misc

For more information about the library used for dependency injection support, check out: https://github.com/seekdavidlee/Eklee-Azure-Functions-Http

Step 2: Setup HTTP function.

The second step is to apply the ExecutionContextDependencyInjection attribute on your HTTP triggered Function and tell it which Module to use. Next, you can inject the ExecutionContext which internally carries the function instance Id. Notice that by convention, we allow both HTTP GET and POST. This, by convention, is what is recommended by GraphQL.

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

The power of GraphQL is that we are able to serve the API via a single HTTP endpoint and consumers need to only know to query for the schema on this endpoint and perform query or mutation operations. Thus, we are giving a generic Route name here called Graph. However, you may want to give it a more domain specific name if you intend to have more than one endpoint.

Step 3: Implement GraphQL server:

Simply leverage the extension method ProcessGraphQlRequest with the HTTP request which will be processed by the GraphQL server.

return await executionContext.ProcessGraphQlRequest(req);

Next Steps: Setup Models and update Mutation and Query classes.

We use a Model-First (with Fluent syntax) to define the GraphQL schema. Description is a required attribute on the model which provides documentation for the model property.

using System.ComponentModel.DataAnnotations;
...
    public class Book
    {
        [Key]
        [Description("Id of the book")]
        public string Id { get; set; }

        [Description("Name of the book")]
        public string Name { get; set; }

Once we have completed these steps, we are ready to start running the Azure HTTP Function.

Other topics:

Recommanded tools:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.