Inverse relations - Reverse relationships queries

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 :confused:

Thanks in advance.

Hey Tommy,

Thanks for posting this question! Yes, it is possible to query by relationship, both ways.

I understand that in this example you have:

  • products
  • posts, which link to products

Querying all of the products for each post is relatively straightforward. When you query a post, you will get all of the product links, which include the metadata about each product. If you use fetchLinks, GraphQuery, or GraphQL, you can fetch the products. (Let me know if you'd like more info about how to do that.)

To get all of the posts by product, you can do a query by content relationship. Here's the documentation for how to do that with JavaScript and the Rest API.

To query by Content Relationship, you need the document ID of the product, so you need to make two queries: one to get the product's ID, and then one to get all of the posts that link to that ID.

If you want to get all of the posts that link to any one of the products in the array, you would use the any predicate instead of the at predicate. If you replace any with at, your code snippet might work. (If not, let me know, and I can help debug it.)

Let me know if this makes sense, or if you have any other questions. What you are describing should be completely possible, and I'm happy to help however I can.

Best,
Sam

This issue has been closed as it has been fixed, Flag if it is not the case for you to reopen.

Hey!

I have custom type productPage which has a group of a content relationship field to another custom type accessories.

When I query the content for my custom type accessories, is there a way to find out, to which products it is linked? The other way of my content relationship field specified above, so to speak.

Or to I also have to add a group of a content relationship field to the accessories custom type and also linked them backwards?

So what I want is this:
productPage
- accessories
- accessories
- accessories

and this without having to foster it again in prismic:
accessories
- productPage
- productPage
- productPage

Thanks in advance!
Marco

Ah I think I found the answer in this thread: Inverse relations - Reverse relationships queries - #3 by samlittlefair

Hello @marco3

Thanks for reaching out to us.

That's correct that you need to create inverse relations.

Let me know if you have any further questions.

Thanks,
Priyanka

1 Like