0

I am trying to filter GraphQL query result by joined table column (field). Relationship between tables is Many-to-Many (with bridge table of course) and I want to filter Advertisements by the Cities related to them.

I am using C#, .NET 6.0 and EF Core with HotChocolate NuGet package for GraphQL. Just a reminder, filtering by main table columns works just fine. Here is how my Query class looks like: enter image description here

And here are the queries I tried: enter image description here

enter image description here

enter image description here

Here are them typed:

{
  advertisements(where: { and: [{ type: { eq: SERVICE } }, {or: [{cities.name: { {eq:"London"} }}]}] }) {
    title
    id
    owner {
      firstName
      lastName
    }
    description
    rateType
    price
    categories {
      name
    }
    cities {
      name
    }
  }
}

{
  advertisements(where: { and: [{ type: { eq: SERVICE } }, {or: [{cities: {name: {eq:"London"}} }]}] }) {
    title
    id
    owner {
      firstName
      lastName
    }
    description
    rateType
    price
    categories {
      name
    }
    cities {
      name
    }
  }
}

{
  advertisements(where: { type: { eq: SERVICE } }) {
    title
    id
    owner {
      firstName
      lastName
    }
    description
    rateType
    price
    categories {
      name
    }
    cities(where: {name: {eq: "London"}}) {
      name
    }
  }
}
Mackovic
  • 37
  • 8

1 Answers1

0

The filter statements has to be in "advertisements"

advertisements(where:{ type: { eq: SERVICE }} cities: {name: {eq: "London"}})

Also, in your Query class you can remove the include statements and instead use [UseProjection]

[UseProjection]
[UseFiltering]
public IQueryable<Advertisement> GetAdvertisements([Service] HomyDbContext context) => context.Advertisement;
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24