Answers.Body: <p>You can run a <a href="https://craftcms.com/docs/3.x/graphql.html#the-categories-query" rel="nofollow noreferrer">categories query</a> and pass it your product id(s) as well as the category group id (though that's not necessary) to fetch all categories related to your products.</p>
  <p>The query:</p>
  <pre class="lang-graphql prettyprint-override"><code>query getRelatedBrands($relatedTo: [QueryArgument] = &quot;&quot;, $groupId: [QueryArgument] = &quot;&quot;) {
    categories(groupId: $groupId, relatedTo: $relatedTo) {
      id
    }
  }
  </code></pre>
  <p>and the variables:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;relatedTo&quot;: [13306, 13638],
    &quot;groupId&quot;: [1]
  }
  </code></pre>
  <p>Which would return:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;data&quot;: {
      &quot;categories&quot;: [
        {
          &quot;id&quot;: &quot;3&quot;
        },
        {
          &quot;id&quot;: &quot;234&quot;
        }
      ]
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-27T16:00:18.217Z
Answers.Text: Thanks for your answer. The issue with  doing it this way, as far as I can tell, is that if you were to search over many products (e.g. thousands) you are going to end up having huge GraphQL requests - unless I am missing something?
Answers.UserDisplayName: Seb Jones
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-25T11:55:28.553Z
Answers.LastActivityDate: 2021-01-25T11:55:28.553Z
Answers.UserDisplayName: Oli
Answers.Body: <p>If I understand correctly you are searching for products with a <code>$searchTerm</code> query variable and you want a separate list of the unique brands related to those products.</p>
  <p>You could use a categories GraphQL query with the <a href="https://craftcms.com/docs/3.x/graphql.html#relations" rel="nofollow noreferrer">relatedToEntries</a> argument. Pass in <code>search: $searchTerm</code> like you would with an element query.</p>
  <pre><code>query Query($searchTerm: String) {
    brands: categories(group: &quot;brand&quot;, relatedToEntries: [{section: &quot;product&quot;, search: $searchTerm}] {
      id
    }
  }
  </code></pre>
  <p>Should return a list like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;data&quot;: {
      &quot;brands&quot;: [
        {
          &quot;id&quot;: &quot;3&quot;
        },
        {
          &quot;id&quot;: &quot;5&quot;
        },
        {
          &quot;id&quot;: &quot;19&quot;
        },
        ...
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-26T09:35:08.683Z
Answers.LastActivityDate: 2022-02-26T09:35:08.683Z
Answers.UserDisplayName: Alex Roper
Body: <p>Suppose I have some entries in a section called 'product' that are related to a category call 'brand' - I know I can get the brands that are related to each product that match a search term using GraphQL like so:</p>
  <pre><code>query Query($searchTerm: String) {
    entries(search: $searchTerm, section: &quot;product&quot;) {
      ... on product_product_Entry {
        brand {
          id
        }
      }
    }
  }
  </code></pre>
  <p>Which returns something like this:</p>
  <pre><code>{
    &quot;data&quot;: {
      &quot;entries&quot;: [
        {
          &quot;brand&quot;: [
            {
              &quot;id&quot;: &quot;3&quot;
            }
          ]
        },
        {
          &quot;brand&quot;: [
            {
              &quot;id&quot;: &quot;5&quot;
            }
          ]
        },      
        {
          &quot;brand&quot;: [
            {
              &quot;id&quot;: &quot;3&quot;
            }
          ]
        },
        ...
  </code></pre>
  <p>What I'm looking to do is get all the brands that are related to the products matching the search, but obviously if I use the above query then the response will get very big very quick. Is it possible to get just the distinct brand id values returned from the above GraphQL query? Or am I better off making a module with a controller action and querying the database that way?</p>
  
Tags: <graphql>
Title: Fetching Unique Categories related to Entries via GraphQL
ThreadText: # Fetching Unique Categories related to Entries via GraphQL
  
  (Asked by: username_0 on 2021-01-25)
  
  Suppose I have some entries in a section called 'product' that are related to a category call 'brand' - I know I can get the brands that are related to each product that match a search term using GraphQL like so:
  
  ```
  query Query($searchTerm: String) {
    entries(search: $searchTerm, section: "product") {
      ... on product_product_Entry {
        brand {
          id
        }
      }
    }
  }
  
  ```
  
  Which returns something like this:
  
  ```
  {
    "data": {
      "entries": [
        {
          "brand": [
            {
              "id": "3"
            }
          ]
        },
        {
          "brand": [
            {
              "id": "5"
            }
          ]
        },      
        {
          "brand": [
            {
              "id": "3"
            }
          ]
        },
        ...
  
  ```
  
  What I'm looking to do is get all the brands that are related to the products matching the search, but obviously if I use the above query then the response will get very big very quick. Is it possible to get just the distinct brand id values returned from the above GraphQL query? Or am I better off making a module with a controller action and querying the database that way?
  
  **Tags:** graphql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-01-25. Score: 3
  
  You can run a [categories query](https://craftcms.com/docs/3.x/graphql.html#the-categories-query) and pass it your product id(s) as well as the category group id (though that's not necessary) to fetch all categories related to your products.
  
  The query:
  
  ```graphql
  query getRelatedBrands($relatedTo: [QueryArgument] = "", $groupId: [QueryArgument] = "") {
    categories(groupId: $groupId, relatedTo: $relatedTo) {
      id
    }
  }
  
  ```
  
  and the variables:
  
  ```json
  {
    "relatedTo": [13306, 13638],
    "groupId": [1]
  }
  
  ```
  
  Which would return:
  
  ```json
  {
    "data": {
      "categories": [
        {
          "id": "3"
        },
        {
          "id": "234"
        }
      ]
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for your answer. The issue with  doing it this way, as far as I can tell, is that if you were to search over many products (e.g. thousands) you are going to end up having huge GraphQL requests - unless I am missing something?  
  
  ---
  
  ### Answer by username_2 on 2022-02-26. Score: 1
  
  If I understand correctly you are searching for products with a `$searchTerm` query variable and you want a separate list of the unique brands related to those products.
  
  You could use a categories GraphQL query with the [relatedToEntries](https://craftcms.com/docs/3.x/graphql.html#relations) argument. Pass in `search: $searchTerm` like you would with an element query.
  
  ```
  query Query($searchTerm: String) {
    brands: categories(group: "brand", relatedToEntries: [{section: "product", search: $searchTerm}] {
      id
    }
  }
  
  ```
  
  Should return a list like this:
  
  ```json
  {
    "data": {
      "brands": [
        {
          "id": "3"
        },
        {
          "id": "5"
        },
        {
          "id": "19"
        },
        ...
  
  ```
  
  
  ---
  
  