graphql-dotnet

GraphQL for .NET

Github星跟踪图

GraphQL for .NET

Build Status
Join the chat at https://gitter.im/graphql-dotnet/graphql-dotnet

NuGet
Nuget
Nuget

Activity
Activity
Activity

Size

This is an implementation of Facebook's GraphQL in .NET.

Now the specification is being developed by the
GraphQL Foundation.

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

Installation

WARNING: The latest stable version 2.4.0 has many known issues that have been fixed in 3.0.0-preview-XXXX versions.
If errors occur, it is recommended that you first check the behavior on the latest available preview version before
reporting a issue. Latest 3.0.0-preview-XXXX versions are backwards incompatible with latest stable 2.4.0 version.
You can see the changes in public APIs using fuget.org.

You can install the latest stable version via NuGet.

> dotnet add package GraphQL

For serialized results, you'll need an IDocumentWriter implementation.
We support GraphQL.SystemTextJson for .NET Core 3+,
GraphQL.NewtonsoftJson
(formerly included in GraphQL), 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.

You can get the latest pre-release packages from the MyGet feed,
where you may want to explicitly pull a certain version using -v.

> dotnet add package GraphQL.SystemTextJson -v 3.0.0-preview-1448

Documentation

http://graphql-dotnet.github.io

Examples

https://github.com/graphql-dotnet/examples

You can also try an example of GraphQL demo server inside this repo - GraphQL.Harness.
It supports the popular IDEs for managing GraphQL requests - GraphQL Playground, GraphiQL and Altair.

Training

Upgrade Guides

Basic Usage

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

Fully-featured examples can be found here.

Hello World

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.

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

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

public class Query
{
  private List<Droid> _droids = new List<Droid>
  {
    new Droid { Id = "123", Name = "R2-D2" }
  };

  [GraphQLMetadata("droid")]
  public Droid GetDroid(string id)
  {
    return _droids.FirstOrDefault(x => x.Id == id);
  }
}

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

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

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

Roadmap

Grammar / AST

Operation Execution

  • Scalars
  • Objects
  • Lists of objects/interfaces
  • Interfaces
  • Unions
  • Arguments
  • Variables
  • Fragments
  • Directives
    • Include
    • Skip
    • Custom
  • Enumerations
  • Input Objects
  • Mutations
  • Subscriptions
  • Async execution

Validation

  • Arguments of correct type
  • Default values of correct type
  • Fields on correct type
  • Fragments on composite types
  • Known argument names
  • Known directives
  • Known fragment names
  • Known type names
  • Lone anonymous operations
  • No fragment cycles
  • No undefined variables
  • No unused fragments
  • No unused variables
  • Overlapping fields can be merged
  • Possible fragment spreads
  • Provide non-null arguments
  • Scalar leafs
  • Unique argument names
  • Unique directives per location
  • Unique fragment names
  • Unique input field names
  • Unique operation names
  • Unique variable names
  • Variables are input types
  • Variables in allowed position
  • Single root field

Schema Introspection

  • __typename
  • __type
    • name
    • kind
    • description
    • fields
    • interfaces
    • possibleTypes
    • enumValues
    • inputFields
    • ofType
  • __schema
    • types
    • queryType
    • mutationType
    • subscriptionType
    • directives

Publishing Nugets

yarn run setVersion 2.0.0
git commit/push
download nuget from AppVeyor
upload nuget package to github
publish nuget from MyGet

Running on OSX with mono

To run this project on OSX with mono you will need to add some configuration.
Make sure mono is installed and add the following to your bash configuration:

export FrameworkPathOverride=/Library/Frameworks/Mono.framework/Versions/4.6.2/lib/mono/4.5/

See the following for more details:

主要指标

概览
名称与所有者graphql-dotnet/graphql-dotnet
主编程语言C#
编程语言C# (语言数: 5)
平台
许可证MIT License
所有者活动
创建于2015-07-06 16:47:15
推送于2025-09-08 13:00:21
最后一次提交2025-09-08 09:00:19
发布数132
最新版本名称8.5.0 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数6k
关注者数179
派生数0.9k
提交数2.6k
已启用问题?
问题数1779
打开的问题数91
拉请求数1911
打开的拉请求数19
关闭的拉请求数395
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?