|
1 | 1 | ---
|
2 | 2 | name: Entity GraphQL
|
3 |
| -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) |
| 3 | +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. |
4 | 4 | url: https://github.com/EntityGraphQL/EntityGraphQL
|
5 | 5 | github: EntityGraphQL/EntityGraphQL
|
6 | 6 | ---
|
7 | 7 |
|
| 8 | +```csharp |
| 9 | +// expose an exisiting data model with ASP.NET & EF Core |
| 10 | +public class Startup { |
| 11 | + public void ConfigureServices(IServiceCollection services) |
| 12 | + { |
| 13 | + services.AddControllers().AddNewtonsoftJson(); |
| 14 | + services.AddDbContext<MyDbContext>(); |
| 15 | + // Build a schema from your data model (See docs on how to extend, modify or build manually as well as merge other data sources). |
| 16 | + services.AddSingleton(SchemaBuilder.FromObject<MyDbContext>()); |
| 17 | + } |
| 18 | +} |
8 | 19 |
|
| 20 | +// expose an endpoint with ASP.NET |
| 21 | +[Route("api/[controller]")] |
| 22 | +public class QueryController : Controller |
| 23 | +{ |
| 24 | + private readonly MyDbContext _dbContext; |
| 25 | + private readonly SchemaProvider<MyDbContext> _schemaProvider; |
| 26 | + |
| 27 | + public QueryController(MyDbContext dbContext, SchemaProvider<MyDbContext> schemaProvider) |
| 28 | + { |
| 29 | + this._dbContext = dbContext; |
| 30 | + this._schemaProvider = schemaProvider; |
| 31 | + } |
| 32 | + |
| 33 | + [HttpPost] |
| 34 | + public object Post([FromBody]QueryRequest query) |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + var results = _schemaProvider.ExecuteQuery(query, _dbContext, null, null); |
| 39 | + if (results.Errors?.Count > 0) |
| 40 | + { |
| 41 | + // log error |
| 42 | + return StatusCode(StatusCodes.Status500InternalServerError, results); |
| 43 | + } |
| 44 | + return results; |
| 45 | + } |
| 46 | + catch (Exception) |
| 47 | + { |
| 48 | + return HttpStatusCode.InternalServerError; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
0 commit comments