This is a work-in-progress 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!
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 repository.
> npm install
> npm start
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 async Task<string> Execute(
Schema schema,
object rootObject,
string query,
string operationName = null,
Inputs inputs = null)
{
var executer = new DocumentExecuter();
var writer = new DocumentWriter();
var result = await executer.ExecuteAsync(schema, rootObject, query, operationName, inputs);
return writer.Write(result);
}
var schema = new StarWarsSchema();
var query = @"
query HeroNameQuery {
hero {
name
}
}
";
var result = await 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
- Unions
- Arguments
- Variables
- Fragments
- Directives
- Include
- Skip
- Custom
- Enumerations
- Input Objects
- Mutations
- Subscriptions
- Async execution
- 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 fragment names
- Unique input field names
- Unique operation names
- Unique variable names
- Variables are input types
- Variables in allowed position
- __typename
- __type
- name
- kind
- description
- fields
- interfaces
- possibleTypes
- enumValues
- inputFields
- ofType
- __schema
- types
- queryType
- mutationType
- subscriptionType
- directives
npm run setVersion 0.10.0
write release notes in release-notes.md
git commit/push
download nuget from AppVeyor
upload nuget package to github
upload nuget package to nuget.org