Queries
Let us start to add more complex query types into the API. We will create a GraphQL Query to get all the users and user statuses.
First create a Query class which uses the DbContext to access the Users and UserStatuses table like below.
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.
Finally we remove the simple hello world query and add the newly created QueryType class into the schema like below on line 40.
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.
Last updated