GraphQL Books Sample - Hot Chocolate
Sebastian Szvetecz - CN innovation - sebastian.szvetecz@cninnovation.com
This repository contains a sample project that demonstrates how to use Hot Chocolate to create a GraphQL API in ASP.NET Core.
The /src directory contains the source code for the project.
- BookSample.GraphQL (BookSample.GraphQL.sln)
- BookSample.GraphQL
This project contains the GraphQL API. It uses Hot Chocolate to create a GraphQL schema and resolvers. - BookSample.Data
This project contains the data access layer. It uses Entity Framework Core to interact with the database.
It is used by the GraphQL API to fetch data from the database. - BookSample.ReviewApiClient
This project contains a client for the ReviewAPI. It uses HttpClient to make HTTP requests to the ReviewAPI. Kiota is used to generate the client code.
It is used by the GraphQL API to fetch reviews and ratings for books.
- BookSample.GraphQL
- BookSample.ReviewAPI (BookSample.ReviewAPI.sln)
This project contains an small API that provides reviews and ratings for books.
The following diagram shows a schematic overview of the architecture:
graph LR
A[Client] <-- Queries \n Mutations \n Subscriptions --> B
subgraph backend [Backend]
B[BookSample.GraphQL]
B -- BookSample.ReviewAPIClient --> C[BookSample.ReviewAPI]
B -- BookSample.Data --> D[(Postgres \n DB)]
end
style A stroke-width:2px,stroke-dasharray: 10 5
-
BookSample.Aspire (BookSample.Aspire.sln) In this sample application Aspire is used to improve, among others, the development experience.
In BookSample.Aspire.AppHost the following components are configured:
- Database A PostgreSQL database is used to store the data. In Development mode, Aspire will use a Docker container to run the database. So an installation of PostgreSQL is not required.
- Database-Seeder The database seeder is used to seed the database with sample data.
- Reviews-API The Review API as described above.
- GraphQL-API The GraphQL API as described above.
For running the application including all components, the following steps are required:
- Install Docker Desktop
- Open your command-line and navigate to the root directory of the repository
cd [the directory of the cloned repo]
- Install the Aspire Workload by running
dotnet workload install aspire --version 8.0.0-preview.5.24201.12
in the command-line
In this sample application we currently use the preview-version 5 of Aspire. Make sure that the versions of the Aspire-workload and the installed Aspire-Nuget-packages are matching. - Run the following commands in the command-line:
cd ./src/BookSample.Aspire/BookSample.Aspire.AppHost
dotnet run
- After starting the Aspire AppHost, the following services are available:
- Aspire Dashboard: https://localhost:17054
- GraphQL API and GraphQL Playground: https://localhost:7089/graphql
- Reviews API: https://localhost:7079/reviews
- Open https://localhost:7089/graphql in your browser to test the first GraphQL queries, mutations, and subscriptions in the GraphQL Playground.