# GraphQL - Query by Content Relationship using a variable

**URL:** <https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350>\
**Category:** Developing with Prismic\
**Tags:** graphql\
**Created:** [May 6, 2020, 4:14pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350 "2020-05-06T16:14:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah.ledu](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sarah.ledu](https://community.prismic.io/u/sarah.ledu)\
**Post date:** [May 6, 2020, 4:14pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/1 "2020-05-06T16:14:11Z")

</div>

Hello !  
I am trying to filter results by content relationship field. I am following the documentation here : [Query Data with GraphQL - Prismic](https://prismic.io/docs/graphql/query-the-api/filtering-by-a-content-relationshiplink-field)

But instead of having the id of the doc hard coded I need to make it a variable.  
I have tried to use prismic.load but I must be using it wrong as it is not working.

Here is my query:  
export const query = graphql` query BlogArticleQuery($lang: String!, $uid: String!, $child: String) { prismic { article: blog_article(lang: $lang, uid: $uid) { ...BlogArticle } recommendation: allBlog_recommendationss { edges { node { recommendation_title } } } categories: allBlog_categorys(where: { child: $child }) { edges { node { category_name child { ... on PRISMIC_Blog_category { category_name _linkType _meta { uid id } } } } } } } } `

and inside my component I have:

```
useEffect(() => {
    
      prismic
        .load({
          variables: { child: article.category._meta.id }
        })
        .then(response => {
          console.log(response)
        }).catch(err=> console.log(err))
    
  })

```

But it is not working ☹

Could you help me out ? Thanks a lot in advance !

---

<div class="post-metadata">

**Author:** ![Levi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/levi/32/24_2.png) [@Levi](https://community.prismic.io/u/Levi)\
**Post date:** [May 7, 2020, 11:10am UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/2 "2020-05-07T11:10:05Z")

</div>

Hi @sarah.ledu, I’m sorry that this isn’t working for you. Can you share a link to your Prismic GraphQL Explorer with this query filled in. Hopefully with this we’ll be able to figure out why this isn’t working for you. Thanks!

---

<div class="post-metadata">

**Author:** ![sarah.ledu](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sarah.ledu](https://community.prismic.io/u/sarah.ledu)\
**Post date:** [May 7, 2020, 12:41pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/3 "2020-05-07T12:41:58Z")

</div>

Hi Levi,  
Thanks for your answer. Here is a screenshot of my query in the explorer with one ID hard coded. !

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/prismic/original/1X/698b5fdacfe30a64cf2329eec395d5e37c74aa31.png)

Let me know if you need more info.  
Thanks !

---

<div class="post-metadata">

**Author:** ![Levi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/levi/32/24_2.png) [@Levi](https://community.prismic.io/u/Levi)\
**Post date:** [May 11, 2020, 1:45pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/4 "2020-05-11T13:45:17Z")

</div>

Hi Sarah, I’m not sure what the issue might be. I’ll need to try to recreate this locally. Can you send me your project files, so I can run your website and test this? You can either send me a zip file of your project or a link to github (or similar). If you don’t want to share it publicly here, you can send me a direct message.

---

<div class="post-metadata">

**Author:** ![sarah.ledu](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sarah.ledu](https://community.prismic.io/u/sarah.ledu)\
**Post date:** [May 12, 2020, 8:20am UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/6 "2020-05-12T08:20:01Z")

</div>

Hi Levi,  
I’d like to send you my zip file in a private message but it looks like I can’t send you private message. I cannot post a file either, because I’m new, it seems.  
Could you tell me how to proceed ?  
Thanks in advance !

---

<div class="post-metadata">

**Author:** ![Levi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/levi/32/24_2.png) [@Levi](https://community.prismic.io/u/Levi)\
**Post date:** [May 12, 2020, 8:54am UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/7 "2020-05-12T08:54:35Z")

</div>

@sarah.ledu I sent you a private message. Hopefully it will work now.

---

<div class="post-metadata">

**Author:** ![hugo.villain](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/hugo.villain/32/186_2.png) [@hugo.villain](https://community.prismic.io/u/hugo.villain)\
**Post date:** [May 12, 2020, 3:14pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/9 "2020-05-12T15:14:34Z")

</div>

Hey @sarah.ledu

in order for this call to work, you would have to also pass it a `BlogArticle` fragment.  
Can you try something like this:

```javascript
import { BlogArticle as BlogArticleFragment } from '../../fragments/BlogArticle'

useEffect(() => {
      prismic
        .load({
          variables: {
            child: article.category._meta.id,
            fragments: [BlogArticleFragment]
          }
        })
        .then(response => {
          console.log(response)
        }).catch(err=> console.log(err))
    
  }, [])

```

In the same way, if you realise you want to play another query, import it like I imported the fragment and pass it explicitly to the `load` method.

Hope it helps ✌

---

<div class="post-metadata">

**Author:** ![sarah.ledu](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sarah.ledu](https://community.prismic.io/u/sarah.ledu)\
**Post date:** [May 13, 2020, 1:25pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/10 "2020-05-13T13:25:41Z")

</div>

Thanks for your reply @hugo.villain !  
I haven’t succeed to make it work yet though. Do I have to add the variable $fragments somewhere in the query as well ? If yes where ?

---

<div class="post-metadata">

**Author:** ![hugo.villain](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/hugo.villain/32/186_2.png) [@hugo.villain](https://community.prismic.io/u/hugo.villain)\
**Post date:** [May 18, 2020, 12:16pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/11 "2020-05-18T12:16:41Z")

</div>

You shouldn’t have to alter your query to make this work.  
What happens when you copy-paste this portion of code?  
Do you still end up with errors 500?

---

<div class="post-metadata">

**Author:** ![Fares](https://avatars.discourse-cdn.com/v4/letter/f/71e660/32.png) [@Fares](https://community.prismic.io/u/Fares)\
**Post date:** [September 21, 2020, 3:23pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/13 "2020-09-21T15:23:39Z")

</div>

This issue has been closed due to inactivity.

---

<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, 5:38pm UTC](https://community.prismic.io/t/graphql-query-by-content-relationship-using-a-variable/350/14 "2021-12-09T17:38:40Z")

</div>


