A beginner-friendly GraphQL API project built with .NET 7 and HotChocolate to demonstrate real-world usage of GraphQL in a .NET environment. This project provides CRUD operations, real-time subscriptions, and GraphQL Voyager for visualization. Perfect for those who want to learn GraphQL and best practices in .NET!
- GraphQL with HotChocolate: Includes Queries, Mutations, and Subscriptions.
- SQLite Database: Lightweight and easy-to-setup database for demo purposes.
- GraphQL Voyager: Visualize your GraphQL schema with an interactive UI.
- Filtering & Sorting: Built-in support for GraphQL filtering and sorting.
- In-Memory Subscriptions: Real-time data updates using WebSockets.
- CORS Support: Configured to handle cross-origin requests.
- Swagger Integration: Explore REST endpoints alongside your GraphQL API.
- Modular Code Design: Follows clean architecture principles and .NET best practices.
- .NET 7
- HotChocolate (GraphQL for .NET)
- Entity Framework Core with SQLite
- GraphQL Voyager for schema visualization
- Swagger/OpenAPI for REST API documentation
- CORS Configuration for flexible API accessibility
- In-Memory Subscriptions for real-time GraphQL operations
ToDoQL
├── Data # Contains the DbContext and Entity models
├── GraphQL # Contains GraphQL schema, resolvers, and types
│ ├── Query.cs # Defines GraphQL queries
│ ├── Mutation.cs # Defines GraphQL mutations
│ ├── Subscription.cs # Defines GraphQL subscriptions
│ ├── ItemType.cs # Type definitions for Item
│ └── ItemListsType.cs# Type definitions for Item Lists
├── Program.cs # Application entry point
├── appsettings.json # Configuration file for database connections
└── Startup.cs # Configures services and middleware
- .NET SDK 7.0+
- SQLite (pre-installed in .NET; no setup required)
- A REST client like Postman or a GraphQL client like Altair
-
Clone the repository:
git clone https://github.com/dipjyotisikder/graphql-example-with-hotchocolate.git cd graphql-example-with-hotchocolate
-
Restore dependencies: Run the following command to restore the required packages:
dotnet restore
-
Apply database migrations
Ensure the SQLite database is set up by applying migrations:dotnet ef database update
-
Run the application
Start the application by running:dotnet run
-
Access the application
Open the following URLs in your browser:- GraphQL Playground: https://localhost:5001/graphql
Explore the GraphQL API and test queries, mutations, and subscriptions. - GraphQL Voyager: https://localhost:5001/graphql-ui
Visualize your GraphQL schema and relationships. - Swagger UI: https://localhost:5001/swagger
Access REST API documentation (if applicable).
- GraphQL Playground: https://localhost:5001/graphql
Use the Playground at /graphql
to run GraphQL queries, mutations, and subscriptions interactively.
Fetch all item lists along with their items:
query {
itemLists {
id
name
items {
id
title
isComplete
}
}
}
Fetch items filtered by their completion status:
query {
items(where: { isComplete: true }) {
id
title
}
}
Add a new item to an existing list:
mutation {
addItem(input: { title: "New Task", itemListId: 1 }) {
id
title
isComplete
}
}
Update an existing item:
mutation {
updateItem(id: 1, input: { title: "Updated Task", isComplete: true }) {
id
title
isComplete
}
}
Listen for changes to items in real time:
subscription {
onItemChanged {
id
title
isComplete
}
}
Contributions are welcome! Feel free to fork this repository, make improvements, and submit a pull request.
This project is licensed under the MIT License.
Thanks to the HotChocolate and .NET communities for providing excellent tools and resources to make GraphQL development accessible for everyone.