Expose an entity as a query model
[TraxQueryModel] serves an EF Core entity under
discover with paging, filtering, sorting and projection.
- Mark your entity with
[TraxQueryModel]:
using Trax.Effect.Attributes;
[TraxAuthorize] // or [TraxAllowAnonymous]: without a posture the host refuses to start (see Authorization)
[TraxQueryModel(Description = "Player profiles")]
public class PlayerRecord
{
public long Id { get; set; }
public string PlayerId { get; set; } = "";
public string DisplayName { get; set; } = "";
public int Rating { get; set; }
}- Add the entity to a
DbSet<T>on aDbContext:
using Microsoft.EntityFrameworkCore;
public class GameDbContext(DbContextOptions<GameDbContext> options) : DbContext(options)
{
public DbSet<PlayerRecord> Players { get; set; } = null!;
}- Register the DbContext and enable model discovery:
using Microsoft.EntityFrameworkCore;
using Trax.Api.GraphQL.Extensions;
builder.Services.AddDbContextFactory<GameDbContext>(options =>
options.UseNpgsql(connectionString));
builder.Services.AddTraxGraphQL(graphql =>
graphql.AddDbContext<GameDbContext>());For an application's own data, the base to use is a
DomainDataContext registered with
AddDomainDataContext, and it works here unchanged: AddDbContext<T>() names the concrete context
type, and the pooled factory AddDomainDataContext registers serves it.
using Microsoft.EntityFrameworkCore;
using Trax.Effect.Data.Services.DomainContext;
public interface IGameDbContext
{
DbSet<PlayerRecord> Players { get; }
}
public class GameDbContext(DbContextOptions<GameDbContext> options)
: DomainDataContext<GameDbContext>(options), IGameDbContext
{
public DbSet<PlayerRecord> Players => Set<PlayerRecord>();
protected override string Schema => "game";
protected override void ConfigureModel(ModelBuilder modelBuilder) { }
}using Microsoft.EntityFrameworkCore;
using Trax.Api.GraphQL.Extensions;
using Trax.Effect.Data.Services.DomainContext;
builder.Services.AddDomainDataContext<IGameDbContext, GameDbContext>(o => o.UseNpgsql(connectionString));
builder.Services.AddTraxGraphQL(graphql => graphql.AddDbContext<GameDbContext>());The Bookworm sample serves its catalog this way.
This generates a playerRecords query field under discover:
query {
discover {
playerRecords(first: 10, where: { rating: { gte: 1500 } }, order: { rating: DESC }) {
nodes {
playerId
displayName
rating
}
pageInfo {
hasNextPage
endCursor
}
totalCount
}
}
}