11---
22name : Entity GraphQL
33description : 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- url : https://github.com/EntityGraphQL/EntityGraphQL
4+ url : https://entitygraphql. github.io
55github : EntityGraphQL/EntityGraphQL
66---
77
@@ -10,43 +10,22 @@ github: EntityGraphQL/EntityGraphQL
1010public class Startup {
1111 public void ConfigureServices (IServiceCollection services )
1212 {
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 >());
13+ services .AddDbContext <DemoContext >();
14+ // Auto build a schema from DemoContext. Alternatively you can build one from scratch
15+ services .AddGraphQLSchema <DemoContext >(options =>
16+ {
17+ // modify the schema (add/remove fields or types), add other services
18+ });
19+ }
20+
21+ public void Configure (IApplicationBuilder app , DemoContext db )
22+ {
23+ app .UseRouting ();
24+ app .UseEndpoints (endpoints =>
25+ {
26+ // defaults to /graphql endpoint
27+ endpoints .MapGraphQL <DemoContext >();
28+ });
1729 }
18- }
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- }
5130}
5231```
0 commit comments