GraphQL
on
.NET Core

TheDull

REST

Types

type StarWarsQuery {
  hero: Droid
}

type Droid {
  id: String!
  name: String
}
type Character {
  name: String!
  appearsIn: [Episode]!
}

type Starship {
  id: ID!
  name: String!
  length(unit: LengthUnit = METER): Float
}

Schemas and Queries

schema {
  query: Query
  mutation: Mutation
}
query {
  hero {
    name
  }
  droid(id: "2000") {
    name
  }
}
{
  "data": {
    "hero": {
      "name": "R2-D2"
    },
    "droid": {
      "name": "C-3PO"
    }
  }
}
PM> Install-Package GraphQL -Version 2.0.0-alpha-868

$ dotnet add package GraphQL --version 2.0.0-alpha-868
PM> Install-Package graphiql
$ dotnet add package graphiql
// In Startup.cs

public void Configure(IApplicationBuilder app, ...)
{
    ...

    app.UseGraphiQl();

    app.UseMvc();
}

Queries

public class StarWarsQuery : ObjectGraphType
{
  public StarWarsQuery()
  {
    Field<DroidType>(
      "hero",
      resolve: context => new Droid { Id = "1", Name = "R2-D2" }
    );
  }
}

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

Arguments

public class StarWarsQuery : ObjectGraphType
{
  public StarWarsQuery(IStarWarsData data)
  {
    Field<DroidType>(
      "droid",
      arguments: new QueryArguments(
        new QueryArgument<StringGraphType> { Name = "id" }
      ),
      resolve: context =>
      {
        var id = context.GetArgument<string>("id");
        var objectId = contet.Arguments["id"];
        return data.GetDroidByIdAsync(id);
      }
    );
  }
}

Variables

var variablesJson = // get from request
// `ToInputs` converts the json to the `Inputs` class
var inputs = variablesJson.ToInputs();

var result = await executer.ExecuteAsync(_ =>
{
    _.Inputs = inputs;
});

Mutations (1)

public class StarWarsSchema : Schema
{
    public StarWarsSchema(Func<Type, GraphType> resolveType)
        : base(resolveType)
    {
        Query = (StarWarsQuery)resolveType(typeof (StarWarsQuery));
        Mutation = (StarWarsMutation)resolveType(typeof (StarWarsMutation));
    }
}

public class HumanInputType : InputObjectGraphType
{
    public HumanInputType()
    {
        Name = "HumanInput";
        Field<NonNullGraphType<StringGraphType>>("name");
        Field<StringGraphType>("homePlanet");
    }
}

Mutations (2)

/// <example>
/// This is an example JSON request for a mutation
/// {
///   "query": "mutation ($human:HumanInput!)
///        { createHuman(human: $human) { id name } }",
///   "variables": {
///     "human": {
///       "name": "Boba Fett"
///     }
///   }
/// }
/// </example>
public class StarWarsMutation : ObjectGraphType<object>
{
    public StarWarsMutation(StarWarsData data)
    {
        Field<HumanType>(
            "createHuman",
            arguments: new QueryArguments(
                new QueryArgument<NonNullGraphType<HumanInputType>> {Name = "human"}
            ),
            resolve: context =>
            {
                var human = context.GetArgument<Human>("human");
                return data.AddHuman(human);
            });
    }
}

Interfaces

interface Character {
  id: ID!
  name: String!
  friends: [Character]
}
public class CharacterInterface : InterfaceGraphType<StarWarsCharacter>
{
  public CharacterInterface()
  {
    Name = "Character";
    Field(d => d.Id).Description("The id of the character.");
    Field(d => d.Name, nullable: true).Description("The name of the character.");
    Field<ListGraphType<CharacterInterface>>("friends");
  }
}

Interfaces - Implementation

type Droid implements Character {
  id: ID!
  name: String!
  friends: [Character]
  primaryFunction: String
}
public class DroidType : ObjectGraphType<Droid>
{
  public DroidType(IStarWarsData data)
  {
    Name = "Droid";
    Description = "A mechanical creature in the Star Wars universe.";

    Field(d => d.Id).Description("The id of the droid.");
    Field(d => d.Name, nullable: true).Description("Name of the droid.");

    Field<ListGraphType<CharacterInterface>>(
      "friends",
      resolve: context => data.GetFriends(context.Source)
    );
    Field(d => d.PrimaryFunction, nullable: true).Description("Primary function of the droid.");

    Interface<CharacterInterface>();
  }
}

Unions

public class CatOrDog : UnionGraphType
{
  public CatOrDog()
  {
    Type<Cat>();
    Type<Dog>();
  }
}

Subscriptions

subscription comments($repoName: String!) {
  newComments(repoName: $repoName) {
    content
    postedBy {
      username
    }
    postedAt
  }
}

Clients

[   ]

GraphQL + .NET Core

By gabby_tee

GraphQL + .NET Core

  • 90
Loading comments...

More from gabby_tee