This is a work-in-progress implementation of Facebook's GraphQL in .NET.
This project uses Antlr4 for the GraphQL grammar definition.
You can install the latest version via NuGet.
PM> Install-Package GraphQL
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
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");
}
}
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>();
IsTypeOf = value => value is Droid;
}
}
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"
}
}
}
- Grammar and AST for the GraphQL language should be complete.
- Scalars
- Objects
- Lists of objects/interfaces
- Interfaces
- Arguments
- Variables
- Fragments
- Directives
- Enumerations
- Input Objects
- Mutations
- Unions
- Async execution
- Not started
- __typename
- __type
- name
- kind
- description
- fields
- interfaces
- possibleTypes
- enumValues
- inputFields
- ofType
- __schema
- types
- queryType
- mutationType
- directives