Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to Query by Slug (or Non-ID Field) in a Relationship with GraphQL?

default discord avatar
turtle40452 years ago
1

Hey all,



I’m working with a Collection: Persons that has a relationship with Locations (fields: name, slug (which is unique)). On the frontend, I’m trying to use a Query (useAllPersonsQuery) to filter persons by a specific location slug, like this:



const { data, loading, error, fetchMore } = useAllPersonsQuery({
  variables: {
    where: {
      'locations.slug': { 
        in: ['berlin'],
      },
    },
  },
});

However, I’m getting the following error:



Object literal may only specify known properties, and 'locations.slug' does not exist in type 'Person_Where'.



This makes perfect sense since the

Person_Where

type only has

locations

and not

locations.slug

. So, filtering by locations using IDs, like so, works:



locations: {
  in: ['66eac7cf765d57eb0238e664'],
}


Is There a Way to Filter by a Different Field than ID (e.g., Slug) in a Where Query?

  • discord user avatar
    jarrod_not_jared
    2 years ago

    This is a known limitation of our GraphQL implementation as of right now. There is a GitHub discussion out there if you search for it in our repo.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.