Is it possible to query a document with a slug field of a relationship?

default discord avatar
Stupidism
last year
1 1

For example, in a classic article/blog site, we have tags related to articles.

Article {
  ...
  tags: Tag[];
}

Tag {
  id: string;
  slug: string;
  name: string;
}

And we have a tag page showing all the articles with this tag like this: https://getjerry.com/tag/driver-licenses

In this page, I need to query the tag.id from tag.slug first and then use the tag.id to query articles.

Is it possible to query articles with tag.slug directly?

I think this is a common feature that other CMS like contentful supports.

  • discord user avatar
    jmikrut
    Payload Team
    last year

    Yes, this is possible! Right now, it's only possible in the REST API and Local APIs due to how complex the query types would get in GraphQL (lots of query possibilities) but in REST you can do:

    /api/articles?where[tag.slug][in]=some-tag
    

    I think in your case you probably have many tags, which is why I used in rather than equals.

    But this should work great!

    3 replies
  • default discord avatar
    Stupidism
    last year

    Aha, it's not doable through graphql then.

    I'm trying to achieve this by using slug as id with custom id feature directly.

    I'm wondering if this solution has any defects?

  • default discord avatar
    Stupidism
    last year

    @jmikrut Any suggestions?

  • discord user avatar
    jmikrut
    Payload Team
    last year

    Hey @Stupidism this would work very well. Custom IDs are pretty cool.

    You should totally give that a shot and let me know how it works, but note, that at some point in the future we will certainly be supporting "nested queries" in GraphQL as well. Probably soon-ish. Can't guarantee, but it's a commonly requested feature.

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.