Popularity
9.2
Stable
Activity
9.0
Growing
4,665
199
752

Description

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

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

Programming language: C#
License: MIT License
Tags: API     JSON     Client     Query     graph     GraphQL    
Latest version: v3.2.0

GraphQL for .NET alternatives and similar packages

Based on the "API" category.
Alternatively, view GraphQL for .NET alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of GraphQL for .NET or a related project?

Add another 'API' Package

README

GraphQL for .NET

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

Build status Build status

Backers on Open Collective Sponsors on Open Collective

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!

Documentation

http://graphql-dotnet.github.io

This site is in sync with master branch.

Installation

You can install the latest stable version via NuGet.

> dotnet add package GraphQL

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

Package Downloads Nuget Latest
GraphQL.SystemTextJson Nuget Nuget
GraphQL.NewtonsoftJson Nuget Nuget
> 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 all preview versions from GitHub Packages. Note that GitHub requires authentication to consume the feed. See here.

Examples

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

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

Training

Upgrade Guides

You can see the changes in public APIs using fuget.org.

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

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

Validation

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

Schema Introspection

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

Publishing NuGet packages

The package publishing process is automated with GitHub Actions.

After your PR is merged into master or develop, preview packages are published to GitHub Packages.

Stable versions of packages are published to NuGet when a release is created.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 Become a backer.

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor.


*Note that all licence references and agreements mentioned in the GraphQL for .NET README section above are relevant to that project's source code only.