diff --git a/src/content/code/language-support/c-net/server/entity-graphql.md b/src/content/code/language-support/c-net/server/entity-graphql.md index 9cf13bd0f1..32f4b27e28 100644 --- a/src/content/code/language-support/c-net/server/entity-graphql.md +++ b/src/content/code/language-support/c-net/server/entity-graphql.md @@ -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(); + // 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()); + } +} +// expose an endpoint with ASP.NET +[Route("api/[controller]")] +public class QueryController : Controller +{ + private readonly MyDbContext _dbContext; + private readonly SchemaProvider _schemaProvider; + + public QueryController(MyDbContext dbContext, SchemaProvider 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; + } + } +} +```