Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/content/code/language-support/c-net/server/entity-graphql.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
---
name: Entity GraphQL
description: .NET Core GraphQL library. Compiles to IQueryable to easily expose a schema from an existing data model (E.g. from an Entity Framework data model)
description: A GraphQL library for .NET Core. Easily expose you data model as a GraphQL API or bring together multiple data sources into a single GraphQL schema.
url: https://github.com/EntityGraphQL/EntityGraphQL
github: EntityGraphQL/EntityGraphQL
---

```csharp
// expose an exisiting data model with ASP.NET & EF Core
public class Startup {
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
services.AddDbContext<MyDbContext>();
// Build a schema from your data model (See docs on how to extend, modify or build manually as well as merge other data sources).
services.AddSingleton(SchemaBuilder.FromObject<MyDbContext>());
}
}

// expose an endpoint with ASP.NET
[Route("api/[controller]")]
public class QueryController : Controller
{
private readonly MyDbContext _dbContext;
private readonly SchemaProvider<MyDbContext> _schemaProvider;

public QueryController(MyDbContext dbContext, SchemaProvider<MyDbContext> schemaProvider)
{
this._dbContext = dbContext;
this._schemaProvider = schemaProvider;
}

[HttpPost]
public object Post([FromBody]QueryRequest query)
{
try
{
var results = _schemaProvider.ExecuteQuery(query, _dbContext, null, null);
if (results.Errors?.Count > 0)
{
// log error
return StatusCode(StatusCodes.Status500InternalServerError, results);
}
return results;
}
catch (Exception)
{
return HttpStatusCode.InternalServerError;
}
}
}
```