Fetching from Databases
Hot Chocolate is not bound to a specific database, pattern, or architecture. You can call any data source from your resolvers. This page shows a practical example of fetching data from a database and exposing it through a GraphQL API.
Hot Chocolate provides integrations for Entity Framework Core, MongoDB, and other databases. These integrations add convenience on top of the core resolver model but are not required.
Fetching from MongoDB
In this example, you inject a MongoDB collection into a resolver and query it directly.
// Models/Book.cspublic class Book{ public Guid Id { get; set; } public string Title { get; set; } public string Author { get; set; }}
// Types/BookQueries.cs[QueryType]public static partial class BookQueries{ public static async Task<Book?> GetBookByIdAsync( Guid id, IMongoCollection<Book> collection, CancellationToken ct) => await collection.Find(x => x.Id == id).FirstOrDefaultAsync(ct);}
// Program.csbuilder .AddGraphQL() .AddTypes();
Fetching from Entity Framework Core
When using EF Core, inject your DbContext directly into resolvers. Hot Chocolate's EF Core integration registers the context correctly for concurrent resolver execution.
// Types/BookQueries.cs[QueryType]public static partial class BookQueries{ public static async Task<Book?> GetBookByIdAsync( int id, CatalogContext db, CancellationToken ct) => await db.Books.FindAsync([id], ct);
[UsePaging] [UseProjection] [UseFiltering] [UseSorting] public static IQueryable<Book> GetBooks(CatalogContext db) => db.Books;}
When you return IQueryable<T>, the pagination, projection, filtering, and sorting middleware translate to native SQL queries. The database handles the heavy lifting.
Learn more about the Entity Framework integration
Next Steps
- Need to batch database calls? See DataLoader.
- Need to optimize SQL queries? See Projections.
- Need to integrate with MongoDB? See MongoDB Integration.
- Need to integrate with EF Core? See Entity Framework Integration.