Skip to content

dlukez/graphql-dotnet-annotations

Repository files navigation

GraphQL.Annotations

NuGet MyGet Pre Release MyGet Build Status

Attribute-based schema definitions for GraphQL in .NET.

Have a look through the sample app and leave some feedback.

Running the sample app

cd samples/GraphQL.Annotations.StarWarsApp/
dotnet build
dotnet ef database update
dotnet run

Annotating your models

Full example

// Model/QueryRoot.cs

[GraphQLObject]
public class QueryRoot : IDisposable
{
    public StarWarsContext Db = new StarWarsContext();

    [GraphQLFunc]
    public IEnumerable<Droid> Droids(ResolveFieldContext context)
    {
        var db = context.GetDataContext();
        return db.Droids.ToList();
    }

    [GraphQLFunc]
    public IEnumerable<Human> Humans(ResolveFieldContext context)
    {
        var db = context.GetDataContext();
        return db.Humans.ToList();
    }

    public void Dispose()
    {
        Db.Dispose();
    }
}

// Model/Droid.cs

[GraphQLObject]
public class Droid : ICharacter
{
    [GraphQLField]
    public int DroidId { get; set; }

    [GraphQLField]
    public string Name { get; set; }

    [GraphQLField]
    public string PrimaryFunction { get; set; }

    [GraphQLFunc]
    public IEnumerable<ICharacter> Friends(ResolveFieldContext context)
    {
        var db = context.GetDataContext();
        return db.Friendships
            .Where(f => f.DroidId == ((Droid)context.Source).DroidId)
            .Select(f => f.Human);
    }
}

Todo

  • Include Scalar example.
  • Fill out this readme.

About

Attribute-based schema definitions for GraphQL in .NET.

Resources

License

Stars

Watchers

Forks

Packages

No packages published