# Queries

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.

{% code title="./GraphQL/Query.cs" %}

```csharp
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;
        }
    }
}

```

{% endcode %}

Next we create a QueryType Class like below.

{% code title="./GraphQL/QueryType.cs" %}

```csharp
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());
        }
    }
}

```

{% endcode %}

Finally we remove the simple hello world query and add the newly created QueryType class into the schema like below on line 40.

{% code title="./Startup.cs" %}

```csharp
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();
        }
    }
}
```

{% endcode %}

Once the schema has been modified we can run the project and access the playground to see our created query.

![](https://3683023892-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LurxKF9uo_T_1SndGac%2F-LuywbO-g_C8BUnauxj3%2F-LuzDwk-MBpr-8o3Y8OI%2Fimage.png?alt=media\&token=8127f378-4518-4c93-91e8-eccbefa5e820)

Running the following query will provide the initial user statuses we added to the database.

```graphql
{
  userStatuses {
    id
    status
    description
    rank
  }
}
```

```graphql
{
  "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
      }
    ]
  }
}
```

![](https://3683023892-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LurxKF9uo_T_1SndGac%2F-LuzHU8_MSfoyGoQghDN%2F-LuzJZhY89xAxIAtjvyN%2Fimage.png?alt=media\&token=c091eabc-0e83-4035-809b-8aa60098e43a)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://arif-hanif.gitbook.io/slackclone/server/api/data-modeling/queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
