EntityGraphQL.AspNet 4.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityGraphQL.AspNet --version 4.1.0                
NuGet\Install-Package EntityGraphQL.AspNet -Version 4.1.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="EntityGraphQL.AspNet" Version="4.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityGraphQL.AspNet --version 4.1.0                
#r "nuget: EntityGraphQL.AspNet, 4.1.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 EntityGraphQL.AspNet as a Cake Addin
#addin nuget:?package=EntityGraphQL.AspNet&version=4.1.0

// Install EntityGraphQL.AspNet as a Cake Tool
#tool nuget:?package=EntityGraphQL.AspNet&version=4.1.0                

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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 EntityGraphQL.AspNet:

Package Downloads
Dino.GraphqlLib

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EntityGraphQL.AspNet:

Repository Stars
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
Version Downloads Last updated
5.5.1 0 11/14/2024
5.5.0 498 10/25/2024
5.4.6 2,954 9/23/2024
5.4.5 832 9/13/2024
5.4.4 121 9/12/2024
5.4.3 1,208 9/3/2024
5.4.2 177 9/2/2024
5.4.1 3,704 7/15/2024
5.4.0 3,552 6/2/2024
5.3.0 4,425 5/7/2024
5.2.1 3,928 4/8/2024
5.2.0 4,318 3/19/2024
5.2.0-beta2 447 2/11/2024
5.2.0-beta1 2,392 11/16/2023
5.1.1 4,507 1/24/2024
5.1.0 4,357 11/16/2023
5.0.1 4,457 9/28/2023
5.0.0 2,359 8/2/2023
5.0.0-beta1 326 6/17/2023
4.3.1 146 11/22/2023
4.3.0 6,111 3/14/2023
4.2.1 1,800 12/22/2022
4.2.0 1,208 11/16/2022
4.1.2 657 10/20/2022
4.1.1 597 10/18/2022
4.1.0 565 10/13/2022
4.0.1 996 10/5/2022
4.0.0 1,544 9/10/2022
4.0.0-beta2 615 9/8/2022
4.0.0-beta1 165 9/6/2022
3.0.5 810 8/16/2022
3.0.4 561 8/6/2022
3.0.3 1,070 8/4/2022
3.0.2 752 8/1/2022
3.0.1 431 8/1/2022
3.0.0 577 7/31/2022
2.3.2 505 7/18/2022
2.3.1 875 7/14/2022
2.3.0 695 7/4/2022
2.2.0 480 7/1/2022
2.1.5 1,913 6/13/2022
2.1.4 441 6/11/2022
2.1.3 1,004 6/6/2022
2.1.2 566 5/23/2022
2.1.1 758 5/18/2022
2.1.0 469 5/17/2022
2.0.3 485 5/9/2022
2.0.2 499 5/6/2022
2.0.1 486 5/5/2022
2.0.0 497 4/29/2022
2.0.0-beta7 172 4/27/2022
2.0.0-beta6 162 4/25/2022
2.0.0-beta5 166 4/14/2022
2.0.0-beta4 183 4/13/2022
2.0.0-beta3 159 4/13/2022
2.0.0-beta2 178 4/11/2022
2.0.0-beta1 178 4/7/2022
1.2.1 6,915 2/20/2022
1.2.0 495 2/15/2022
1.1.2 468 2/11/2022
1.1.1 692 2/9/2022
1.1.0 622 1/13/2022
1.1.0-beta2 194 1/8/2022
1.1.0-beta1 181 1/8/2022
1.0.2 550 10/20/2021
1.0.1 12,106 9/30/2021
1.0.0 412 9/28/2021
1.0.0-beta2 222 9/15/2021