📨
SlackClone
  • Introduction
  • Server
    • Server Introduction
    • Getting Started
      • Install .NET Core 3
      • Create a Project
      • Install Dependencies - Mongo
      • Install Dependencies - PostgreSQL
        • PostgreSQL Install - Windows
        • Connect to PostgreSQL DB - pgAdmin
    • API
      • Setup HotChocolate Server
      • Add Playground
      • Data Model - PostgreSQL
        • PostgreSQL Entities
        • Database Context
        • Queries
          • Filtering
          • Sorting
        • Mutations
        • Subscriptions
      • Data Model - Mongo
        • Mongo Entities
        • Queries
  • Authorization
  • Authentication
  • Error Handling
  • Testing
  • Backend Summary
  • Client
    • Client Introduction
    • Getting Started
      • Install Dependencies
      • Create Project
    • App
      • Routing
      • Queries
        • Pagination
        • Sorting
        • Filtering
      • Mutations
      • Subscriptions
      • Authentication
    • Frontend Summary
Powered by GitBook
On this page
  1. Server
  2. API
  3. Data Model - PostgreSQL
  4. Queries

Filtering

PreviousQueriesNextSorting

Last updated 5 years ago

Note: Be sure to install the HotChocolate.Types.Filters NuGet package.

Next we will add filtering to the users query, so we can filter on fields provided in the entity.

This is simply done by adding .UseFiltering() on the field we want to filter, like seen on 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()).UseFiltering();
            descriptor.Field(t => t.GetUserStatuses());
        }
    }
}

Now we can run the server again and test what we have added. Reading the schema documentation you can see the filtering capabilities added to users query.

Lets test the filtering with the following query in the playground.

{
  users(where: { firstName_starts_with: "D" }) {
    id
    firstName
    lastName
    fullName
    email
    username
  }
}

If everything went well you should see the following response.

{
  "data": {
    "users": [
      {
        "id": "1964199a-e8be-440a-a178-8998d114fd12",
        "firstName": "Donald",
        "lastName": "Trump",
        "fullName": "Donald Trump",
        "email": "dtrump@us.gov",
        "username": "dtrump"
      }
    ]
  }
}