Skip to content

Invenietis/graphql-dotnet

 
 

Repository files navigation

GraphQL for .NET

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

This is a work-in-progress implementation of Facebook's GraphQL in .NET.

This project uses Antlr4 for the GraphQL grammar definition.

Installation

You can install the latest version via NuGet.

PM> Install-Package GraphQL

GraphiQL

There is a sample web api project hosting the GraphiQL interface. npm install and build webpack from the root of the project.

> npm install
> webpack --progress

Usage

Define your type system with a top level query object.

public class StarWarsSchema : Schema
{
  public StarWarsSchema()
  {
    Query = new StarWarsQuery();
  }
}

public class StarWarsQuery : ObjectGraphType
{
  public StarWarsQuery()
  {
    var data = new StarWarsData();
    Name = "Query";
    Field<CharacterInterface>(
      "hero",
      resolve: context => data.GetDroidById("3")
    );
  }
}

public class CharacterInterface : InterfaceGraphType
{
  public CharacterInterface()
  {
    Name = "Character";
    Field<NonNullGraphType<StringGraphType>>("id", "The id of the character.");
    Field<NonNullGraphType<StringGraphType>>("name", "The name of the character.");
    Field<ListGraphType<CharacterInterface>>("friends");
    ResolveType = (obj) =>
    {
      if (obj is Human)
      {
        return new HumanType();
      }
      return new DroidType();
    };
  }
}

public class DroidType : ObjectGraphType
{
  public DroidType()
  {
    var data = new StarWarsData();
    Name = "Droid";
    Field<NonNullGraphType<StringGraphType>>("id", "The id of the droid.");
    Field<NonNullGraphType<StringGraphType>>("name", "The name of the droid.");
    Field<ListGraphType<CharacterInterface>>(
        "friends",
        resolve: context => data.GetFriends(context.Source as StarWarsCharacter)
    );
    Interface<CharacterInterface>();
  }
}

Executing a query.

public string Execute(
  Schema schema,
  object rootObject,
  string query,
  string operationName = null,
  Inputs inputs = null)
{
  var executer = new DocumentExecuter();
  var writer = new DocumentWriter();

  var result = executer.Execute(schema, rootObject, query, operationName, inputs);
  return writer.Write(result);
}

var schema = new StarWarsSchema();

var query = @"
  query HeroNameQuery {
    hero {
      name
    }
  }
";

var result = Execute(schema, null, query);

Console.Writeline(result);

// prints
{
  "data": {
    "hero": {
      "name": "R2-D2"
    }
  }
  "errors": []
}

Roadmap

Grammar / AST

  • Grammar and AST for the GraphQL language should be complete.

Operation Execution

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

Validation

  • Not started

Schema Introspection

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

Packages

No packages published

Languages

  • C# 84.4%
  • CSS 12.6%
  • ANTLR 2.2%
  • Other 0.8%