Fetching Unique Categories related to Entries via GraphQL
You can run a 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:
query getRelatedBrands($relatedTo: [QueryArgument] = &quot;&quot;, $groupId: [QueryArgument] = &quot;&quot;) {
  categories(groupId: $groupId, relatedTo: $relatedTo) {
    id
  }
}

and the variables:
{
  &quot;relatedTo&quot;: [13306, 13638],
  &quot;groupId&quot;: [1]
}

Which would return:
{
  &quot;data&quot;: {
    &quot;categories&quot;: [
      {
        &quot;id&quot;: &quot;3&quot;
      },
      {
        &quot;id&quot;: &quot;234&quot;
      }
    ]
  }
}