How to query and filter more than one record/post type in a single query

Iā€™m trying to query the GraphQL api to find any record of certain types that match a certain filter.

Two common example cases:
Case 1: Search by slug
I know the ā€œslugā€ of a page/record, but I donā€™t know its modelType, how can I query the GraphQL api to find any record that has that slug? (this can happen if you have a ā€œpagesā€ record type, but you also have special unique pages record types, like landingPages, or aboutPage, or contactPage).

Case 2: Search by category
I have a Category, and Articles OR Projects could have that category. How could I search for any Article or Project that has Category X? Searching them separately isnā€™t a good option, because sorting and pagination would be thrown off. My expectations is something like the current Modular Content Fields, using ... on Project {}

Something like this would make sense:
https://graphql.org/learn/queries/#meta-fields

1 Like

Hello @maikueo welcome! :slight_smile:

Currently we donā€™t have the Meta fields option that youā€™ve linked.

What I would do in your case is something like this:

landings: allLandingPages(filter: { slug: { eq: "bike" } }) {
  title
}
pages: allPages(filter: { slug: { eq: "bike" } }) {
  title
}

and similarly on the category search, except you need to get the category ID first. We cannot do ā€œdeep filteringā€ right now and thereā€™s a feature request for that. Please vote that or comment if you have more ideas.

Hi @mat_jack1!
Thank you. I think that would be helpful in some cases! :slight_smile:

For the Category search case though, having them split into two queries would mean that paginating and sorting wouldnā€™t really be possible.
And building a custom site search would also be hard without mixed type queries (I know there is the Site Search widget here https://www.datocms.com/docs/guides/installing-site-search/widget which would probably work for a lot of cases, but if you want to make a search that shows more things like images or certain props, or a search that has more capabilities I guess it would need to be filtered by type first)

Great work on the CMS by the way :slight_smile: It is probably one of the friendliest UIā€™s and really the best suited to making websites Iā€™ve seen so far.

1 Like

I see what you are saying, yes, that meta option would be useful.

Iā€™ve noted that down internally and weā€™ll discuss that on the next development iteration. Cannot say right now if we are going to pick that up. If you want to publicly track that you can open a feature request maybe

any progress on meta field support?

no @rislam we didnā€™t get any feature request in the end, so we didnā€™t consider this feature very requested. Please open one if you want, thank you!

Iā€™ve opened a feature request, thank you. this is absolutely necessary stuff for us.

1 Like