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. yarn install
and yarn start
from the root of the repository, then run the web project from Visual Studio.
Note: Before running the GraphiQL project: make sure you Build the entire solution so that all the project references get built. (GraphQL, GraphQL-Parser, etc) to avoid missing reference/assembly errors.
> npm install -g yarn
> yarn install
> yarn start
Define your schema with a top level query object then execute that query.
A more full-featured example including all classes required can be found here.
namespace ConsoleApplication
{
using System;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Http;
using GraphQL.Types;
public class Program
{
public static void Main(string[] args)
{
Run();
}
private static async void Run()
{
Console.WriteLine("Hello GraphQL!");
var schema = new Schema { Query = new StarWarsQuery() };
var result = await ExecuteQuery(
schema, @"
query {
hero {
id
name
}
}
");
Console.WriteLine(result);
}
private static async Task<string> ExecuteQuery(
Schema schema,
string query,
object rootObject = null,
string operationName = null,
Inputs inputs = null,
object userContext = null)
{
var executer = new DocumentExecuter();
var writer = new DocumentWriter(indent: true);
var result = await executer.ExecuteAsync(
schema,
rootObject,
query,
operationName,
inputs,
userContext).ConfigureAwait(false);
return writer.Write(result);
}
}
public class Droid
{
public string Id { get; set; }
public string Name { get; set; }
}
public class StarWarsQuery : ObjectGraphType
{
public StarWarsQuery()
{
Name = "Query";
Field<DroidType>(
"hero",
resolve: context => new Droid { Id = "1", Name = "R2-D2" }
);
}
}
public class DroidType : ObjectGraphType
{
public DroidType()
{
Name = "Droid";
Field<NonNullGraphType<StringGraphType>>("id", "The id of the droid.");
Field<StringGraphType>("name", "The name of the droid.");
IsTypeOf = value => value is Droid;
}
}
}
Output
Hello GraphQL!
{
"data": {
"hero": {
"id": "1",
"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
yarn run setVersion 0.12.0
write release notes in release-notes.md
git commit/push
download nuget from AppVeyor
upload nuget package to github
publish nuget from MyGet