Lets start to add more complex query types into the API. We will create a GraphQL Query to get all the users and user status states.
First create a Query class which uses the DbContext to access the Users and UserStatuses table like below.
./GraphQL/Query.cs
using System.Linq;
using SlackClone.Entities;
namespace SlackClone.GraphQL
{
public class Query
{
private readonly SlackCloneDbContext dbContext;
public Query(SlackCloneDbContext _dbContext)
{
dbContext = _dbContext;
}
public IQueryable<User> GetUsers()
{
return dbContext.Users;
}
public IQueryable<UserStatus> GetUserStatuses()
{
return dbContext.UserStatuses;
}
}
}
Next we create a QueryType Class like below.
./GraphQL/QueryType.cs
using HotChocolate.Types;
namespace SlackClone.GraphQL
{
public class QueryType : ObjectType<Query>
{
protected override void Configure(
IObjectTypeDescriptor<Query> descriptor)
{
descriptor.Field(t => t.GetUsers());
descriptor.Field(t => t.GetUserStatuses());
}
}
}
Finally we remove the simple hello world query and add the newly created QueryType class into the schema like below on line 40.
./Startup.cs
using SlackClone.Entities;
using SlackClone.GraphQL;
using HotChocolate;
using HotChocolate.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace SlackClone
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime.
// Use this method to add services to the container.
// For more information on how to configure your application,
// visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// Adds the Db context
services.AddEntityFrameworkNpgsql()
.AddDbContext<SlackCloneDbContext>(opt =>
opt.UseNpgsql(
Configuration.GetConnectionString("SlackCloneDb"))
);
// Adds GraphQL Schema
services.AddGraphQL(services =>
SchemaBuilder.New()
.AddServices(services)
.AddQueryType<QueryType>()
.Create());
}
// 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.UseDeveloperExceptionPage();
}
// Adds GraphQL Service
app.UseGraphQL();
// Adds Playground IDE
app.UsePlayground();
}
}
}
Once the schema has been modified we can run the project and access the playground to see our created query.
Running the following query will provide the initial user statuses we added to the database.
{
userStatuses {
id
status
description
rank
}
}
{
"data": {
"userStatuses": [
{
"id": "5015a867-bf66-49e1-8131-afa1cbcb65d7",
"status": "Available",
"description": "User is online",
"rank": 1
},
{
"id": "f8b5a781-e5c8-4373-820c-013c1bd5460a",
"status": "Busy",
"description": "User is busy",
"rank": 2
},
{
"id": "d6cf9102-324c-4f98-b9b2-af21fa2ed065",
"status": "Do Not Disturb",
"description": "User oes not want to be disturbed",
"rank": 3
},
{
"id": "826de452-2282-4690-890e-14c21f1a50ff",
"status": "Away",
"description": "User is away",
"rank": 4
},
{
"id": "a591e17f-08d4-47d1-a7e0-6754f149df6f",
"status": "Offline",
"description": "User is offline",
"rank": 5
}
]
}
}