# GraphQL Content Relationship Queries for Multiple Items

**URL:** <https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367>\
**Category:** Developing with Prismic\
**Tags:** graphql\
**Created:** [October 21, 2020, 3:14am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367 "2020-10-21T03:14:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://community.prismic.io/u/john1)\
**Post date:** [October 21, 2020, 3:14am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/1 "2020-10-21T03:14:18Z")

</div>

If I'm understanding [this document](https://prismic.io/docs/graphql/query-the-api/filtering-by-a-content-relationshiplink-field) correctly, Prismic does not allow querying of content relationships by UID. And the limitation looks even more severe - you can only get one document (by ID) back at a time?

> **You must use the document ID**
> 
> When querying by a Content relationship field, the value passed to the argument can only be the ID of a given document. If you use a different value, for example: a UID field, the query won't work.

In my example, I'd like to get n News articles from South Carolina (I don't know how many there will be):

```
  allNewss( where: { locations : "south-carolina" }) {
    edges {
      node {
        title
        _meta {
          id
          uid
          type
          lang
        }
      }
    }
  }

```

What's the workaround here? How do I get a variable number of my news items from your endpoint?

---

<div class="post-metadata">

**Author:** ![marcellothearcane](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/marcellothearcane/32/224_2.png) [@marcellothearcane](https://community.prismic.io/u/marcellothearcane)\
**Post date:** [October 21, 2020, 5:35am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/2 "2020-10-21T05:35:05Z")

</div>

What type is the `south-carolina` field?

You could use tags, you then query like this:

```auto
query {
  allNews (tags: "south-carolina") {
    edges {
      node {
        title
        _meta {
          id
          uid
          type
          lang
        }
      }
    }
  }
}

```

You can query multiple tags too - see the docs:

> **[Query by tags with the Prismic GraphQL API - Prismic](https://prismic.io/docs/graphql/query-the-api/query-by-tags)**

---

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://community.prismic.io/u/john1)\
**Post date:** [October 21, 2020, 3:48pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/3 "2020-10-21T15:48:50Z")

</div>

Yeah but the tags are pages themselves, I need content on those. They arent just organizational to the system.

`south-carolina` is a location in Prismic.

---

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://community.prismic.io/u/john1)\
**Post date:** [October 21, 2020, 10:09pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/4 "2020-10-21T22:09:27Z")

</div>

**PROBLEM** :

- I need a tagging content-type for my site based on physical location, call it `locations`. My spec requires each location have it's own page along with being a tag on other content-types. This rules out the [Prismic tag system](https://intercom.help/prismicio/en/articles/2690038-prismic-s-default-tagging-system), because it cannot be used as a content-type or generate pages strangely.

- The [recommendation from Prismic](https://user-guides.prismic.io/en/articles/2033431-create-a-custom-tagging-system) if you need true tags is to create a custom content-type as a tag. Once that is set up, you can model a `group` in another content-type to create a multi-select of content-relationships. This allows you to tag between content-types now (ie, 'I want to tag a `news` item with three `locations`').

- As noted in this thread and [in the Prismic docs](https://prismic.io/docs/graphql/query-the-api/filtering-by-a-content-relationshiplink-field), if you're using GraphQL you are out of luck. You can't look inside a `group` with aGraphQL union for whatever reason. So, to recap, you can build a multi-tag set up on the admin side but you just can't even query for it.

Obviously, this sucks. It seems like a huge oversight for a CMS but we're all developers here and are used to encountering weird circumstances. So let's get into a solution.

**SOLUTION**

If you keep digging in the Prismic docs, you will find that you can [query for a group of content relationships](https://prismic.io/docs/javascript/query-the-api/query-by-a-field) inside of content-type...but it only works in REST for whatever reason. So to actually fetch tags articles within a content-type, you need to use both GraphQL and then do a callback with `id` of the tag INTO a REST query. That's a mouthful - let's break it down since Prismic doesn't seem to want to.

**CODE**

Let's assume you're on an individual instance of a tag page (`/locations/[uid].js`) and want to display 3 news articles tagged with that location. Getting the location is pretty easy in GraphQL - take note of the `id` inside of `_meta` - you will need that in a second.

```auto
query LocationByUID($uid: String!, $lang: String!) {
    locations(uid: $uid, lang: $lang) {
      header_image
      location
      intro
      _meta {
        uid
        id
        type
      }
   }
}

```

You want to take that response and store it into a response variable - like:  
`const locationResults = [data]`

The part of the response you need for the REST call would be `locations._meta.id` in my example, because content-relationship queries require using the `id`, not the `uid` - again this isn't explained, go take it up with Prismic.

The REST query to get `news` items by the `location` is pretty weird looking, very atypical at least to me. Pay attention to the `Prismic.Predicates.at('my.news.locations.location', data.locations._meta.id)` line, which effectively means 'look for news items tagged by the location id from the last query` - effectively a GraphQL callback:

```auto
  await Prismic.getApi(REF_API_URL).then(
    function(api) {  
      return api.query(
        [
          Prismic.Predicates.at('document.type', 'news'),
          Prismic.Predicates.at('my.news.locations.location', data.locations._meta.id),
        ],
        { orderings : '[my.news.publication_date desc]' }
      );
    }).then(
    function(resNews) {
      locationResults.push(resNews.results)
    }
  );

```

Taken altogether, you can now get a single `location` tag page with n number of `news` items tagged by location (using a multi-select tagging system):

```auto
export async function getSingleLocationsPage(uid, previewData) {
  const data = await fetchAPI(`
  query LocationByUID($uid: String!, $lang: String!) {
    locations(uid: $uid, lang: $lang) {
      header_image
      location
      intro
      _meta {
        uid
        id
        type
      }
    }
  }
  `,
    {
      previewData,
      variables: {
        uid,
        lang: API_LOCALE,
      },
    }
  )

  // Due to limitations with the GraphQL endpoint (can't query tags in groups)
  // We need to do a second call to get linked news, topics and grants per location
  // Build array for single location data
  const locationResults = [data]

  await Prismic.getApi(REF_API_URL).then(
    function(api) {  
      return api.query(
        [
          Prismic.Predicates.at('document.type', 'news'),
          Prismic.Predicates.at('my.news.locations.location', data.locations._meta.id),
        ],
        { orderings : '[my.news.publication_date desc]' }
      );
    }).then(
    function(resNews) {
      locationResults.push(resNews.results)
    }
  );

  return locationResults
}

```

This works pretty well in my testing but obviously feel hacky. Good luck coding peeps.

---

<div class="post-metadata">

**Author:** ![marcellothearcane](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/marcellothearcane/32/224_2.png) [@marcellothearcane](https://community.prismic.io/u/marcellothearcane)\
**Post date:** [October 22, 2020, 5:56am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/5 "2020-10-22T05:56:11Z")

</div>

Prismic GraphQL is still in (stable) beta at the moment, which is why not everything is available yet.

@phil, maybe worth a note to the development team?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [October 23, 2020, 9:38am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/7 "2020-10-23T09:38:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [October 27, 2020, 8:06pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/8 "2020-10-27T20:06:03Z")

</div>



---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [October 27, 2020, 8:09pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/9 "2020-10-27T20:09:39Z")

</div>

Re-opened for @mandy

> [@On another topic](https://community.prismic.io/t/2447/1):
>
> There is a much better solution : if the linked document is not group, the posted can use the ID (not the UI) to query it.  
> The solution marked as accepted is only necessary if the poster needs to retrieve a document inside of a group

---

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://community.prismic.io/u/john1)\
**Post date:** [October 27, 2020, 8:33pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/11 "2020-10-27T20:33:18Z")

</div>

Be careful @mandy, that's a trap if you're using it as a tagging system. You can't query for multiple tags the way you're doing it - you could only apply one tag in a `where` GraphQL query. The inability to query groups means you can't make a tagging setup and use GraphQL with Prismic. Wasted a whole day figuring this out.

---

<div class="post-metadata">

**Author:** ![mandy](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@mandy](https://community.prismic.io/u/mandy)\
**Post date:** [October 28, 2020, 7:13pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/12 "2020-10-28T19:13:59Z")

</div>

Yes, it should be clarified that the solution I mentioned is if you only want to assign **one** linked document. So in the case where only one location, category, etc. will be allowed. I wanted to mention this since it could save someone quite a bit of trouble if they don't need to have more than one type of linked document assigned, and therefore have to deal with the limitations of groups.

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [November 6, 2020, 3:44pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/13 "2020-11-06T15:44:35Z")

</div>

This issue has been closed due to inactivity. [Flag to reopen](https://community.prismic.io/t/re-open-a-thread/2175).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [March 17, 2021, 9:46am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/14 "2021-03-17T09:46:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![tommy.zrce](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@tommy.zrce](https://community.prismic.io/u/tommy.zrce)\
**Post date:** [March 17, 2021, 11:43am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/16 "2021-03-17T11:43:51Z")

</div>

Thanks for your solution mixing GraphQL + REST @john1 !  
I think what I want to achieve is related to this post.

Basically 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.

Will the API provide in the near future a way to get that data in one query? Or maybe this is already possible (with GraphQuery? GraphQL? REST?)

For now I have a GraphQuery:

```auto
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'm trying to build on top of Prismic but if those queries are not supported I'd sadly have to look for another CMS 😕

I don't know if this is what you mentionned @john1 but I was trying to:

```auto
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:

```auto
const productsPosts = await Client().query([
    Prismic.Predicates.at("document.type", "post"),
    Prismic.Predicates.at("my.post.products.product", "YBpFzd5MAltX4"),
  ]);

```

this works. So yeah I can't query all the posts that reference a list of products.  
@Phil can you confirm?

If that's the case, 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.

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?

@john1 Did you find another way to do this or you had to implement this with another CMS?

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [March 25, 2021, 4:51pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/18 "2021-03-25T16:51:46Z")

</div>

Being Answered here:

> [@Inverse relations - Reverse relationships queries](https://community.prismic.io/t/inverse-relations-reverse-relationships-queries/4660):
>
> Hello, I initially posted in [GraphQL Content Relationship Queries for Multiple Items](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367) 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 al…

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [March 25, 2021, 4:52pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/19 "2021-03-25T16:52:06Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [April 22, 2021, 4:02am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/20 "2021-04-22T04:02:13Z")

</div>



---

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://community.prismic.io/u/john1)\
**Post date:** [April 22, 2021, 4:02am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/21 "2021-04-22T04:02:58Z")

</div>

@tommy.zrce - my solution above still stands as the only way I've gotten this to work.

@Phil - any updates on bringing this functionality to GraphQL?

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [April 22, 2021, 9:42am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/22 "2021-04-22T09:42:02Z")

</div>

Actually yes there's been progress, the functionality to query by a relationship in a group has been developed and we're are slowly rolling it out.

We can't activate it on a per repo basis for the moment, but once we can we'll update everyone in the following thread:

> [@How do you query by a content relationship in a group, but in graphql?](https://community.prismic.io/t/how-do-you-query-by-a-content-relationship-in-a-group-but-in-graphql/1963/15):
>
> Hello everyone, @jason, @tim, @alex.martens and @prismic5 I'm happy to announce that we're now working on releasing a canary version of this change in the GraphQL schema: being able to use Group fields as filtering options. We'll be doing a progressive release of this feature for every single repository. It's not possible to have this activated on your repository by request right now, we'll update you here once this changes. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [April 29, 2021, 9:11am UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/24 "2021-04-29T09:11:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [December 9, 2021, 4:21pm UTC](https://community.prismic.io/t/graphql-content-relationship-queries-for-multiple-items/2367/25 "2021-12-09T16:21:00Z")

</div>


