Skip to content

Update entity-graphql.md #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2022
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
55 changes: 17 additions & 38 deletions src/content/code/language-support/c-net/server/entity-graphql.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Entity GraphQL
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
url: https://entitygraphql.github.io
github: EntityGraphQL/EntityGraphQL
---

Expand All @@ -10,43 +10,22 @@ github: EntityGraphQL/EntityGraphQL
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>());
services.AddDbContext<DemoContext>();
// Auto build a schema from DemoContext. Alternatively you can build one from scratch
services.AddGraphQLSchema<DemoContext>(options =>
{
// modify the schema (add/remove fields or types), add other services
});
}

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

// 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;
}
}
}
```