Like what we’re doing? Star us on GitHub!

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

Stupidism
7 months ago
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.

  • jmikrut
    Payload Team
    7 months ago

    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
  • Stupidism
    7 months ago

    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?

  • Stupidism
    7 months ago

    @jmikrut Any suggestions?

  • jmikrut
    Payload Team
    7 months ago

    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
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More