Introduction
GraphQL.NET is the original and most widely used GraphQL implementation for the .NET platform. It provides a complete server-side toolkit for defining schemas, executing queries, handling mutations, and supporting subscriptions — all in idiomatic C#.
What GraphQL.NET Does
- Defines GraphQL schemas using code-first C# types or schema-first SDL strings
- Executes queries with validation, authorization, and complexity analysis
- Supports subscriptions for real-time data via WebSocket transports
- Integrates with ASP.NET Core through middleware and endpoint routing
- Handles batched queries, persisted queries, and DataLoader for N+1 prevention
Architecture Overview
GraphQL.NET follows a layered architecture: the Schema layer defines types and fields, the Document Executer parses and validates incoming queries against the schema, and Field Resolvers fetch data from underlying sources. The library uses an async-first execution model, resolving fields concurrently where possible. DataLoader batches and caches database calls within a single request.
Self-Hosting & Configuration
- Install core packages:
GraphQL,GraphQL.MicrosoftDI, and a serializer likeGraphQL.SystemTextJson - Register schema and types in the DI container with
services.AddGraphQL() - Map the GraphQL endpoint with
app.UseGraphQL()or custom middleware - Configure authorization with
AuthorizeWithPolicyon fields and types - Enable the GraphiQL or Playground IDE for interactive query testing
Key Features
- Both code-first and schema-first definition approaches
- Built-in query complexity and depth analysis to prevent abuse
- DataLoader pattern for efficient batched data fetching
- Comprehensive validation following the GraphQL specification
- Active community with regular releases and extensive documentation
Comparison with Similar Tools
- Hot Chocolate — Newer .NET GraphQL server with annotation-based types; GraphQL.NET has a longer track record and larger installed base
- Hasura — Auto-generates GraphQL from Postgres; GraphQL.NET gives full control over schema and resolvers
- Apollo Server — Node.js GraphQL server; GraphQL.NET serves the same role in the .NET ecosystem
- Strawberry (Python) — Python GraphQL library; GraphQL.NET targets C# developers
FAQ
Q: Does GraphQL.NET support .NET 8? A: Yes, it targets .NET Standard 2.0+ and works with all modern .NET versions.
Q: How do I prevent expensive queries?
A: Configure MaxDepth and MaxComplexity on the execution options to reject abusive queries.
Q: Can I use it with Entity Framework Core? A: Yes, field resolvers can call EF Core queries, and DataLoader helps batch database access.
Q: What serializers are supported? A: System.Text.Json and Newtonsoft.Json via separate NuGet packages.