EntityGraphQL 3.0.4

.NET 5.0 .NET Standard 2.1
Install-Package EntityGraphQL -Version 3.0.4
dotnet add package EntityGraphQL --version 3.0.4
<PackageReference Include="EntityGraphQL" Version="3.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityGraphQL --version 3.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EntityGraphQL, 3.0.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityGraphQL as a Cake Addin
#addin nuget:?package=EntityGraphQL&version=3.0.4

// Install EntityGraphQL as a Cake Tool
#tool nuget:?package=EntityGraphQL&version=3.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Entity GraphQL

A GraphQL library for .NET Core

Build Nuget

Jump into the https://entitygraphql.github.io/ for documentation and to get started.

Entity GraphQL is a .NET Core (netstandard 2.1) library that allows you to easily build a GraphQL API on top of your data with the extensibility to bring multiple data sources together in the single GraphQL schema.

It can also be used to execute simple LINQ-style expressions at runtime against a given object which provides powerful runtime configuration.

Please explore, give feedback or join the development.

If you're looking for a dotnet library to generate code to query an API from a GraphQL schema see https://github.com/lukemurray/DotNetGraphQLQueryGen

Installation

Via Nuget

Nuget

Quick Start with Entity Framework

Note: There is no dependency on EF. Queries are compiled to IQueryable or IEnumberable linq expressions. EF is not a requirement - any ORM working with LinqProvider or an in-memory object will work - although EF well is tested.

1. Define your data context (in this example an EF context)

public class DemoContext : DbContext {
  public DemoContext(DbContextOptions options) : base(options)
  {
  }

  protected override void OnModelCreating(ModelBuilder builder) {
    // Set up your relations
  }

  public DbSet<Property> Properties { get; set; }
  public DbSet<PropertyType> PropertyTypes { get; set; }
  public DbSet<Location> Locations { get; set; }
}

public class Property {
  public uint Id { get; set; }
  public string Name { get; set; }
  public PropertyType Type { get; set; }
  public Location Location { get; set; }
}

public class PropertyType {
  public uint Id { get; set; }
  public string Name { get; set; }
  public decimal Premium { get; set; }
}

public class Location {
  public uint Id { get; set; }
  public string Name { get; set; }
}

2. Create a route

Here is an example for a ASP.NET. You will also need to install EntityGraphQL.AspNet to use MapGraphQL. You can also build you own endpoint, see docs.

Nuget

public class Startup {
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddDbContext<DemoContext>(opt => opt.UseInMemoryDatabase());
      // This registers a SchemaProvider<DemoContext>
      services.AddGraphQLSchema<DemoContext>();
  }

  public void Configure(IApplicationBuilder app, DemoContext db)
  {
      app.UseRouting();
      app.UseEndpoints(endpoints =>
      {
          // default to /graphql endpoint
          endpoints.MapGraphQL<DemoContext>();
      });
  }
}

This sets up 1 end point:

  • POST at /graphql where the body of the post is a GraphQL query
  • You can authorize that route how you would any ASP.NET route. See Authorization below for details on having parts of the schema requiring Authorization/Claims

Note - As of version 1.1+ the EntityGraphQL.AspNet extension helper uses System.Text.Json. Previous versions used JSON.NET.

3. Build awesome applications

You can now make a request to your API. For example

  POST localhost:5000/graphql
  {
    properties { id name }
  }

Will return the following result.

{
  "data": {
    "properties": [
      {
        "id": 11,
        "name": "My Beach Pad"
      },
      {
        "id": 12,
        "name": "My Other Beach Pad"
      }
    ]
  }
}

Maybe you only want a specific property

  {
    property(id: 11) {
      id name
    }
  }

Will return the following result.

{
  "data": {
    "property": {
      "id": 11,
      "name": "My Beach Pad"
    }
  }
}

If you need a deeper graph or relations, just ask

  {
    properties {
      id
      name
      location {
        name
      }
      type {
        premium
      }
    }
  }

Will return the following result.

{
  "data": {
    "properties": [
      {
        "id": 11,
        "name": "My Beach Pad",
        "location": {
          "name": "Greece"
        },
        "type": {
          "premium": 1.2
        }
      },
      {
        "id": 12,
        "name": "My Other Beach Pad",
        "location": {
          "name": "Spain"
        },
        "type": {
          "premium": 1.25
        }
      }
    ]
  }
}

Visit documentation for more information.

Using expressions else where (EQL)

Lets say you have a screen in your application listing properties that can be configured per customer or user to only show exactly what they are interested in. Instead of having a bunch of checkboxes and complex radio buttons etc. you can allow a simple EQL statement to configure the results shown. Or use those UI components to build the query.

  // This might be a configured EQL statement for filtering the results. It has a context of Property
  (type.id = 2) or (type.id = 3) and type.name = "Farm"

This would compile to (Property p) => (p.Type.Id == 2 || p.Type.Id == 3) && p.Type.Name == "Farm";

This can then be used in various Linq functions either in memory or against an ORM.

// we create a schema provider to compile the statement against our Property type
var schemaProvider = SchemaBuilder.FromObject<Property>();
var compiledResult = EntityQueryCompiler.Compile(myConfigurationEqlStatement, schemaProvider);
// you get your list of Properties from you DB
var thingsToShow = myProperties.Where(compiledResult.LambdaExpression);

Another example is you want a customised calculated field. You can execute a compiled result passing in an instance of the context type.

// You'd take this from some configuration
var eql = @"if location.name = ""Mars"" then (cost + 5) * type.premium else (cost * type.premium) / 3"
var compiledResult = EntityQueryCompiler.Compile(eql, schemaProvider);
var theRealPrice = compiledResult.Execute<decimal>(myPropertyInstance);

Versioning

We do our best to follow Semantic Versioning:

Given a version number MAJOR.MINOR.PATCH, an increment in:

  • MAJOR version is when we make incompatible API changes,
  • MINOR version is when we add functionality in a backwards compatible manner, and
  • PATCH version is when we make backwards compatible bug fixes.

Contribute & Join the Development

Please do. Pull requests are very welcome. See the open issues for bugs or features that would be useful.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on EntityGraphQL:

Package Downloads
EntityGraphQL.AspNet

Contains ASP.NET extensions and middleware for EntityGraphQL

Zen.Web.GraphQL

Package Description

SiteCauldron.GenericAPI

SiteCauldron Generic API utilities for Web MVC applications. Provides a few controllers to cover most use cases, so that you mostly won't have to program a CRUD again.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.4 34 8/5/2022
3.0.3 114 8/4/2022
3.0.2 52 8/1/2022
3.0.1 45 8/1/2022
3.0.0 166 7/31/2022
2.3.2 244 7/18/2022
2.3.1 413 7/14/2022
2.3.0 392 7/4/2022
2.2.0 699 7/1/2022
2.1.5 622 6/13/2022
2.1.4 102 6/11/2022
2.1.3 596 6/6/2022
2.1.2 172 5/23/2022
2.1.1 377 5/18/2022
2.1.0 92 5/17/2022
2.0.3 90 5/9/2022
2.0.2 214 5/6/2022
2.0.1 89 5/5/2022
2.0.0 140 4/29/2022
2.0.0-beta7 64 4/27/2022
2.0.0-beta6 58 4/25/2022
2.0.0-beta5 1,474 4/14/2022
2.0.0-beta4 96 4/13/2022
2.0.0-beta3 57 4/13/2022
2.0.0-beta2 101 4/11/2022
2.0.0-beta1 1,954 4/7/2022
1.2.0 6,721 2/15/2022
1.1.2 163 2/11/2022
1.1.1 205 2/9/2022
1.1.0 1,103 1/13/2022
1.1.0-beta2 68 1/8/2022
1.1.0-beta1 68 1/8/2022
1.0.3 127 12/28/2021
1.0.2 6,827 10/20/2021
1.0.1 2,625 9/30/2021
1.0.0 199 9/28/2021
1.0.0-beta2 185 9/15/2021
1.0.0-beta1 136 9/13/2021
0.70.0 2,684 8/30/2021
0.69.0 650 8/18/2021
0.69.0-beta7 172 8/12/2021
0.69.0-beta6 179 8/10/2021
0.69.0-beta5 125 8/9/2021
0.69.0-beta4 151 8/6/2021
0.69.0-beta3 154 8/5/2021
0.69.0-beta2 828 7/24/2021
0.69.0-beta1 236 7/16/2021
0.68.1 1,025 4/5/2021
0.66.1 8,859 10/5/2020
0.66.0 501 9/22/2020
0.65.0 303 9/17/2020
0.64.0 882 9/1/2020
0.63.0 1,000 6/10/2020
0.63.0-beta3 1,254 6/4/2020
0.63.0-beta2 267 6/4/2020
0.63.0-beta1 258 6/3/2020
0.62.0 439 5/27/2020
0.61.0 357 5/14/2020
0.60.0 345 5/9/2020
0.60.0-beta3 584 5/4/2020
0.60.0-beta1 497 4/22/2020
0.50.0 2,408 4/1/2020
0.50.0-beta1 303 3/24/2020
0.40.0 506 3/19/2020
0.32.1 635 3/3/2020
0.32.0 943 2/14/2020
0.31.0 693 2/6/2020
0.30.0 1,500 1/23/2020
0.29.0 344 1/21/2020
0.28.1 700 1/8/2020
0.28.0 431 1/8/2020
0.27.2 337 1/8/2020
0.27.1 332 1/7/2020
0.27.0 357 1/6/2020
0.26.0 4,174 1/2/2020
0.25.0 993 11/19/2019
0.24.0 407 11/12/2019
0.23.3 419 11/4/2019
0.23.2 390 11/2/2019
0.23.1 385 10/31/2019
0.23.0 362 10/30/2019
0.22.0 359 10/30/2019
0.21.0 1,679 9/3/2019
0.20.1 392 8/28/2019
0.20.0 379 8/28/2019
0.19.1 510 8/26/2019
0.19.0 380 8/25/2019
0.18.4 548 8/19/2019
0.18.3 533 7/30/2019
0.18.2 625 7/26/2019
0.18.1 495 7/18/2019
0.18.0 471 7/17/2019
0.17.0 639 7/8/2019
0.16.2 922 6/1/2019
0.16.1 490 5/27/2019
0.16.0 568 5/21/2019
0.15.8 639 4/11/2019
0.15.7 509 4/11/2019
0.15.6 9,646 3/15/2019
0.15.5 482 3/14/2019
0.15.4 514 3/8/2019
0.15.3 524 3/6/2019
0.15.2 562 2/22/2019
0.15.1 804 2/14/2019
0.15.0 654 2/5/2019
0.14.4 654 1/29/2019
0.14.3 609 1/25/2019
0.14.2 578 1/22/2019
0.14.1 572 1/21/2019
0.14.0 629 1/14/2019
0.13.1 585 1/9/2019
0.13.0 607 1/3/2019
0.12.1 669 12/18/2018
0.12.0 564 12/13/2018
0.11.0 629 11/13/2018