using System;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Types;
using GraphQL.SystemTextJson; // First add PackageReference to GraphQL.SystemTextJson
public class Program
{
public static async Task Main(string[] args)
{
var schema = Schema.For(@"
type Query {
hello: String
}
");
var json = await schema.ExecuteAsync(_ =>
{
_.Query = "{ hello }";
_.Root = new { Hello = "Hello World!" };
});
Console.WriteLine(json);
}
}
Strawberry Shake removes the complexity of state management and lets you interact with local and remote data through GraphQL.
You can use Strawberry Shake to:
client.GetHero
.Watch(ExecutionStrategy.CacheFirst)
.Subscribe(result =>
{
Console.WriteLine(result.Data.Name);
})
Hot Chocolate takes the complexity away from building a fully-fledged GraphQL server and lets you focus on delivering the next big thing.
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
WebHost
.CreateDefaultBuilder(args)
.ConfigureServices(services =>
services
.AddGraphQLServer()
.AddQueryType<Query>())
.Configure(builder =>
builder
.UseRouting()
.UseEndpoints(e => e.MapGraphQL()))
.Build()
.Run();
public class Query
{
public Hero GetHero() => new Hero();
}
public class Hero
{
public string Name => "Luke Skywalker";
}
// expose an existing data model with ASP.NET & EF Core
public class Startup {
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DemoContext>();
// Auto build a schema from DemoContext. Alternatively you can build one from scratch
services.AddGraphQLSchema<DemoContext>(options =>
{
// modify the schema (add/remove fields or types), add other services
});
}
public void Configure(IApplicationBuilder app, DemoContext db)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
// defaults to /graphql endpoint
endpoints.MapGraphQL<DemoContext>();
});
}
}
The ZeroQL is a high-performance C#-friendly GraphQL client. It supports Linq-like syntax, and doesn’t require Reflection.Emit or expressions. As a result, at runtime provides performance very close to a raw HTTP call.
You can use ZeroQL to:
var userId = 10;
var response = await qlClient.Query(q => q
.User(userId, o => new
{
o.Id,
o.FirstName,
o.LastName
}));