How to Build a GraphQL API in ASP.NET Core 10 Using Hot Chocolate and EF Core (2026)
A GraphQL API exposes a typed schema and lets callers choose the fields they need. Hot Chocolate executes the operation in ASP.NET Core. EF Core translates the catalog query and writes mutations to a database. This example uses a stable ID cursor, a bounded page size and a batched category lookup so it can illustrate how database work changes with the selected fields.
Where GraphQL fits
| Concern | REST | GraphQL |
|---|---|---|
| Response shape | Usually set by the route | Chosen from a typed schema |
| Related data | Separate routes or deliberate expansion | Nested selection and field resolvers |
| Limits | Per-route request limits | Depth, cost, list size and execution limits |
| Authorization | Endpoints and resources | Endpoint, fields, and application resources |
Use GraphQL when different clients need different views of related data. A predictable resource API can remain simpler when the client shape is stable. A GraphQL schema still needs versioning discipline. Add compatible fields, document deprecations, and measure use before removal.
Set up the project
Create a .NET 10 minimal web project. This example uses the Hot Chocolate 15.x API surface. Install a stable 15.x release of HotChocolate.AspNetCore, keep related Hot Chocolate dependencies on the same release line, and add Microsoft.EntityFrameworkCore.Sqlite and Microsoft.AspNetCore.Authentication.JwtBearer from the .NET 10 line. Use dotnet list package --include-transitive to check the resolved graph. The database is a local SQLite file so no server is required. Use migrations in a deployed application.
Set Auth__Authority and Auth__Audience for a trusted issuer and test API. Use a signed access token that targets the configured audience. The mutation also needs the Catalog.Write role in the authenticated identity. If the issuer uses a custom role claim, map it deliberately in the JWT configuration.
Hot Chocolate v15 documentation · EF Core context factory
Define queries, mutations and batch loading
The product resolver returns an ID-ordered page. afterId is the last ID from the preceding response. The optional prefix is a typed argument that EF Core translates into a query. A production API can adopt Hot Chocolate’s filtering, sorting and connection pagination conventions if clients need more flexibility. Keep the order deterministic and constrain which fields are filterable. This sample uses explicit resolver logic so its SQL shape is easy to inspect.
A nested category selection can otherwise cause one query per product. The category DataLoader collects keys within the operation and resolves them together. It creates a new context for a batch, which avoids using the same EF Core context concurrently across field resolvers. Check query count with EF logging in development. Hot Chocolate v15 DataLoader
Send requests and compare responses
POST to /graphql with a bearer token and JSON content type.
With the seeded catalog, expect this data.
Run mutation { addProduct(name: "Case") { id name } } using a caller that has the write role. Then query again and check that the new product appears. An authenticated caller without the role should get a GraphQL error with the FORBIDDEN code. GraphQL can return HTTP 200 alongside operation errors. An anonymous request should fail at the ASP.NET Core authentication boundary. In a local smoke test with SQLite and a fixture OIDC issuer, the anonymous request returned 401, the seeded category query returned the data above, the unprivileged mutation produced FORBIDDEN, and the authorized mutation returned the new item. A production issuer and load limits were not tested.
Bound execution cost and secure the schema
A small operation can request many nested fields, repeat a selection through aliases, or ask for large lists. Cap depth and list size. Review cost analysis and operation limits for the Hot Chocolate version you deploy. Add request budgets, rate limiting, and authorization checks for tenant-owned records. Restrict production tooling as appropriate and avoid exposing internal exception messages to clients.
Start with a query that requests only product names. Then request categories and observe the difference in generated SQL. A useful load test includes concurrent users, worst-case permitted page sizes, and both shallow and nested operations.
Troubleshooting and FAQs
Why do I see a context concurrency error? A DbContext supports one operation at a time. Create contexts per resolver or batch as shown.
Why did a request return 200 and still fail? Inspect the GraphQL errors array and any partial data. HTTP status alone cannot represent every field outcome.
Will sorting by any client-provided field stay fast? No. Offer explicit sort choices and index the fields behind common query paths.
Do I need GraphQL for basic catalog CRUD? REST is also a good fit. See the ASP.NET PB EF Core CRUD tutorial for the corresponding resource-oriented pattern.
Measure the work behind each allowed operation before exposing it broadly. Schema flexibility is valuable when every selected field still respects the same permission and query budgets.
Comments
Post a Comment