How do I query for all documents that have the same content relationship?

While the example below works, it is inefficient and does not allow me to sort within the query.

const articles = await client.query([
        Prismic.Predicates.at("document.type", "article"),
        Prismic.Predicates.at("my.article.author", `${props.id}`),
      ]);
      const reviews = await client.query([
        Prismic.Predicates.at("document.type", "review"),
        Prismic.Predicates.at("my.review.author", `${props.id}`),
      ]);

I'd life for it to be a single query somewhat like this, but I do not know how to get the author part working since it refers to articles and review. Is there a way to accomplish this like the line with the comment?

const response= await client.query([
        Prismic.Predicates.at("document.type", "article"),
        Prismic.Predicates.at("my.document.author", `${props.id}`), //my.document.author is not possible
      ]);

Hello @lis.rizvanolli

Thank you for reaching out to us.

By observing your query, I understand the following points:

  1. You have 3 document types: Article, Review, and Author
  2. Both Article and Review have content relationships with Author documents.
  3. You want to have a single query, which can work on 2 document types (Article and Review) that have a content relationship with Author.
  4. You are facing issues with sorting as well in Predicates.

Here are my response points:

  1. You can not combine queries for 2 document types in one single query here.
  2. You can do sorting for response by using predicates like this.

Can you give more details about the content structure you have? After that, I may propose something.

Thank you,

Priyanka

I understand how to sort using the method you provided, but that is useless in my situation. My intention is to combine both articles and reviews into one array sorted by date. If I cannot query them both together my only option is to use a sort function after the query because otherwise I would have two separately sorted arrays of articles and reviews respectively.

Hello @lis.rizvanolli

As you've discovered there is no way currently to combine both articles and reviews into one array. The only way is the one you suggested: making two separate queries and combining the results.

Thanks,

Priyanka

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.