GraphQL playground

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

public class Author
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Book
{
public int Id { get; set; }
string Title { get; set; }

int AuthorId { get; set; }
Author Author { get; set; }
}
public class LibraryContext : DbContext
{
public virtual DbSet<Book> Books { get; set; }
public virtual DbSet<Author> Authors { get; set; }

public LibraryContext(DbContextOptions<LibraryContext> options) : base(options){}

}
    public class AuthorType: ObjectType<Author>
{
public AuthorType()
{
Name = nameof(Author);
}

/// <inheritdoc />
protected override void Configure(IObjectTypeDescriptor<Author> descriptor)
{
descriptor.Field(x => x.Id).Type<IdType>();
descriptor.Field(x => x.Name).Type<StringType>();
}
}
    public sealed class BookType : ObjectType<Book>
{
public BookType()
{
Name = nameof(Book);
}

/// <inheritdoc />
protected override void Configure(IObjectTypeDescriptor<Book> descriptor)
{
descriptor.Field(x => x.Id).Type<IdType>();
descriptor.Field(x => x.Title).Type<StringType>();
descriptor.Field(x => x.AuthorId).Type<IdType>();
}
}
public class Query
{
public async Task<ICollection<Author>> Authors([FromServices] LibraryContext context)
{
return await context.Authors.ToListAsync();
}

public async Task<ICollection<Book>> Books([FromServices] LibraryContext context)
{
return await context.Books.Include(x => x.Author).ToListAsync();
}
}
    public class CreateAuthorInput
{
public string Name { get; set; }
}
public class Mutation
{
public async Task<Author> CreateAuthor([FromServices] LibraryContext context, CreateAuthorInput inputAuthor)
{
var author = await context.Authors.AddAsync(new Author
{
Name = inputAuthor.Name
});

await context.SaveChangesAsync();

return author.Entity;
}
}
    public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFrameworkInMemoryDatabase()
.AddDbContext<LibraryContext>(context => context.UseInMemoryDatabase("LibraryDB"));

services.AddLogging(builder => builder.AddConsole());

services
.AddGraphQLServer()
.AddType<BookType>()
.AddType<AuthorType>()
.AddMutationType<Mutation>()
.AddQueryType<Query>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UsePlayground();
}

app.UseRouting().UseEndpoints(endpoints => { endpoints.MapGraphQL(); });
}
}
services.AddGraphQLServer()
.AddType<BookType>()
.AddType<AuthorType>()
.AddMutationType<Mutation>()
.AddQueryType<Query>();
app.UsePlayground();app.UseRouting().UseEndpoints(endpoints => { endpoints.MapGraphQL(); });
mutation {
createAuthor(inputAuthor: {
name:"myAuthor"
})
{
id, name
}
}
{
authors{
id, name
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store