在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):EntityGraphQL/EntityGraphQL开源软件地址(OpenSource Url):https://github.com/EntityGraphQL/EntityGraphQL开源编程语言(OpenSource Language):C# 99.7%开源软件介绍(OpenSource Introduction):Entity GraphQLA GraphQL library for .NET CoreJump 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 InstallationVia Nuget Quick Start with Entity FrameworkNote: There is no dependency on EF. Queries are compiled to 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 routeHere is an example for a ASP.NET. You will also need to install EntityGraphQL.AspNet to use 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:
Note - As of version 1.1+ the EntityGraphQL.AspNet extension helper uses System.Text.Json. Previous versions used JSON.NET. 3. Build awesome applicationsYou can now make a request to your API. For example
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
Will return the following result. {
"data": {
"property": {
"id": 11,
"name": "My Beach Pad"
}
}
} If you need a deeper graph or relations, just ask
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 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); Contribute & Join the DevelopmentPlease do. Pull requests are very welcome. See the open issues for bugs or features that would be useful. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论