Hello,
I initially posted in GraphQL Content Relationship Queries for Multiple Items but I thinkg it deserves its own post.
I'm trying to query inverse relations or reverse relationships , in particular for one-to-many relationships so that I can query references in both directions.
For example: I have:
- post model: with link to products in a group field
- product model
I'd like to query all the posts with the products associated with each post, but also return for each product a list of all the posts that reference it.
Is it possible to get that data with GraphQuery? GraphQL? REST? or any other way?
For now I have a GraphQuery:
const getPostsQuery = `{
post {
...postFields
post_products {
products {
...on product {
name
// Here I'd like to have in the product returned data something like:
// posts: [{post1...}, {post2...}] -> all the posts that reference the product
// And sometimes I'd like to have only the number of times the product has been referenced, like:
// postCount: 2 -> which means 2 posts has the product in their relation
}
}
}
}
}`
export async function getPosts() {
const posts = await Client().query(
Prismic.Predicates.at("document.type", "post"),
{
graphQuery: getPostsQuery,
}
);
return posts;
}
I tried to:
const productsPosts = await Client().query([
Prismic.Predicates.at("document.type", "post"),
Prismic.Predicates.at("my.post.products.product", productIds), // productIds = array of product ids
]);
And got an Error: Unexpected status code [400] .
If instead I do:
const productsPosts = await Client().query([
Prismic.Predicates.at("document.type", "post"),
Prismic.Predicates.at("my.post.products.product", "YBpFzd5MAltX4"),
]);
this works. So I can't query all the posts that reference a list of products.
I guess the only option would be to first get all the posts, then all the products, then calculate everything myself on server (which is not maintainable when having hundreds of posts and products). Besides we lose pagination. Am I right?
In the worst case where I do everything on server, if I have 300 posts, can I query all 300 in one go or I have to make multiple requests to get all of them? That would be a temporary solution but far from ideal.
I'm trying to build on top of Prismic but if those queries are not supported I'd sadly have to look for another CMS
Thanks in advance.