Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

graphql-dotnet

> 后端框架
Open source

GraphQL for .NET

6.0K stars0 likes2 views
WebsiteGitHub

About

GraphQL for .NET

GraphQL for .NET

:heart: Become a backer! :heart:

This is an implementation of GraphQL, a query language and execution engine originally created by Facebook in 2012, in .NET.

The GraphQL specification is now being developed and maintained by the GraphQL Foundation, established in 2019 to support the GraphQL ecosystem. You can also find the specification's source and discussions on GitHub at graphql/graphql-spec.

This project uses a lexer/parser originally written by Marek Magdziak and released with a MIT license. Thank you Marek!

Provides the following packages:

Package Downloads NuGet Latest
GraphQL
GraphQL.SystemTextJson
GraphQL.NewtonsoftJson
GraphQL.MemoryCache
GraphQL.DataLoader
GraphQL.MicrosoftDI

You can get all preview versions from GitHub Packages. Note that GitHub requires authentication to consume the feed. See here.

Documentation

  1. http://graphql-dotnet.github.io - documentation site that is built from the docs folder in the master branch.
  2. https://graphql.org/learn - learn about GraphQL, how it works, and how to use it.

Debugging

All packages generated from this repository come with embedded pdb and support Source Link. If you are having difficulty understanding how the code works or have encountered an error, then it is just enough to enable Source Link in your IDE settings. Then you can debug GraphQL.NET source code as if it were part of your project.

Installation

1. GraphQL.NET engine

This is the main package, the heart of the repository in which you can find all the necessary classes for GraphQL request processing.

> dotnet add package GraphQL

2. Serialization

For serialized results, you'll need an IGraphQLSerializer implementation. We provide several serializers (or you can bring your own).

> dotnet add package GraphQL.SystemTextJson
> dotnet add package GraphQL.NewtonsoftJson

Note: You can use GraphQL.NewtonsoftJson with .NET Core 3+, just be aware it lacks async writing capabilities so writing to an ASP.NET Core 3.0 HttpResponse.Body will require you to set AllowSynchronousIO to true as per this announcement; which isn't recommended.

3. Document Caching

The recommended way to setup caching layer (for caching of parsed GraphQL documents) is to inherit from IConfigureExecution interface and register your class as its implementation. We provide in-memory implementation on top of Microsoft.Extensions.Caching.Memory package.

> dotnet add package GraphQL.MemoryCache

For more information see Document Caching.

4. DataLoader

DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

> dotnet add package GraphQL.DataLoader

For more information see DataLoader.

Note: Prior to version 4, the contents of this package was part of the main GraphQL.NET package.

5. Subscriptions

DocumentExecuter can handle subscriptions as well as queries and mutations. For more information see Subscriptions.

6. Dependency Injection

To easily configure GraphQL.NET with the Microsoft dependency injection provider, you can use the GraphQL.MicrosoftDI package. This package provides a AddGraphQL extension method to register the necessary services. This package can also be used with other dependency injection providers that support the Microsoft.Extensions.DependencyInjection abstraction such as Autofac, Castle Windsor, and StructureMap.

> dotnet add package GraphQL.MicrosoftDI

You can then configure GraphQL.NET in your Startup.cs file like this:

csharp
public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQL(b => b
        .AddSchema<MySchema>()
        .AddSystemTextJson()
        .AddDataLoader()
    );
}

For more information see Dependency Injection.

Examples

Project / Repository Description
GraphQL.Net Sample projects focused on showcasing features of the core GraphQL library, an implementation of the GraphQL specification.
GraphQL.Net Server Sample projects highlighting features of the server package, including utilities for integrating a GraphQL server with .NET Web APIs.
Examples Community-provided examples. These may not represent officially supported patterns but show how others use the library.
GraphQL.Net Client Example implementations for the GraphQL client library.
Full Stack App Template Production-ready template for building full-stack applications with .NET, React, TypeScript, and GraphQL.NET. Features Microsoft OAuth, Entity Framework Core, automated CI/CD, type-safe development from database to UI, and monorepo architecture with single deployment.

You can also try an example of GraphQL demo server inside this repo - GraphQL.Harness. It supports the popular IDEs for managing GraphQL requests and exploring GraphQL schema:

  • Altair
  • Firecamp
  • GraphiQL
  • Voyager

Ahead-of-time compilation

GraphQL.NET supports ahead-of-time (AOT) compilation for execution of code-first schemas with .NET 7+. This allows for use within iOS and Android apps, as well as other environments where such features as JIT compilation or dynamic code generation are not available. It may be necessary to explicitly instruct the AOT compiler to include the .NET types necessary for your schema to operate correctly. Of particular note, your query, mutation and subscription types' constructors may be trimmed; register them in your DI engine to prevent this. Also, Field(x => x.MyField) for enumeration values will require manually adding a mapping reference via RegisterTypeMapping<MyEnum, EnumerationGraphType<MyEnum>>(). Please see the GraphQL.AotCompilationSample for a simple demonstration of AOT compilation. Schema-first and type-first schemas have additional limtations and configuration requirements. AOT compilation has not been tested with frameworks other than .NET 7+ on Windows and Linux (e.g. Xamarin).

Training

  • API Development in .NET with GraphQL - Glenn Block demonstrates how to use the GraphQL.NET framework to build a fully functional GraphQL endpoint.
  • Building GraphQL APIs with ASP.NET Core by Roland Guijt

Upgrade Guides

  • 7.x to 8.x
  • 5.x to 7.x
  • 4.x to 5.x
  • 3.x to 4.x
  • 2.4.x to 3.x
  • 0.17.x to 2.x
  • 0.11.0
  • 0.8.0

Basic Usage

Define your schema with a top level query object then execute that query.

Fully-featured examples can be found here.

Hello World

csharp
using System;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Types;
using GraphQL.SystemTextJson; // First add PackageReference to GraphQL.SystemTextJson

var schema = Schema.For(@"
  type Query {
    hello: String
  }
");

var root = new { Hello = "Hello World!" };
var json = await schema.ExecuteAsync(_ =>
{
  _.Query = "{ hello }";
  _.Root = root;
});

Console.WriteLine(json);

Schema First Approach

This example uses the GraphQL schema language. See the documentation for more examples and information.

csharp
public class Droid
{
  public string Id { get; set; }
  public string Name { get; set; }
}

public class Query
{
  [GraphQLMetadata("droid")]
  public Droid GetDroid()
  {
    return new Droid { Id = "123", Name = "R2-D2" };
  }
}

var schema = Schema.For(@"
  type Droid {
    id: ID
    name: String
  }

  type Query {
    droid: Droid
  }
", _ => {
    _.Types.Include<Query>();
});

var json = await schema.ExecuteAsync(_ =>
{
  _.Query = "{ droid { id name } }";
});

Parameters

…

Roadmap

Grammar / AST

  • Grammar and AST for the GraphQL language should be compatible with the October 2021 specification.

Operation Execution

  • Scalars
  • Objects
  • [x

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#apidotnet-coregraphiqlgraphql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架