Answers.Body: <p>I figured it out. The second option (sending the whole <code>filter</code> as a variable) but instead of passing <code>null</code> or <code>[]</code> simply omitting it (or setting it to <code>undefined</code>) worked. Not sure why <code>null</code> and <code>undefined</code> act differently though.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-10T08:53:09.257Z
Answers.LastActivityDate: 2022-10-10T08:53:09.257Z
Answers.UserDisplayName: Thomas
Body: <p>I have a component which lists events and can be configured to filter by category.</p>
  <p>Query:</p>
  <pre><code>query Events($filter: [String]) {
    entries(
      section: &quot;event&quot;
      relatedToCategories: {group: &quot;group&quot;, slug: $filter}
    ) {
      __typename
    }
  }
  </code></pre>
  <p>Variables:</p>
  <pre class="lang-js prettyprint-override"><code>{
      &quot;filter&quot;: null
  }
  </code></pre>
  <p>The filtering is working fine in most cases except with events that don't have a group category. I haven't found any value for <code>$filter</code> that fetches those. Setting it to an empty array or null only gets the events that have at least one category.</p>
  <p>What's the correct approach?</p>
  <p><strong>Update</strong></p>
  <p>I also tried passing the entire filter object to the query:</p>
  <p>Query:</p>
  <pre><code>query Events($filter: [CategoryCriteriaInput]) {
    entries(
      section: &quot;event&quot;
      relatedToCategories: $filter
    ) {
      __typename
    }
  }
  </code></pre>
  <p>Variables:</p>
  <pre class="lang-js prettyprint-override"><code>{
      &quot;filter&quot;: [{group: &quot;group&quot;, slug: &quot;some-slug&quot;}]
  }
  </code></pre>
  <p>This works when a filter is set but not without one.</p>
  <p>Passing <code>null</code> or <code>[]</code> throws this error: <code>Calling unknown method: craft\\elements\\db\\EntryQuery::relatedToCategories()</code>
  Passing <code>[{}]</code> again only returns the entries which have any category set.</p>
  
Tags: <categories><relations><relatedto><graphql>
Title: GraphQL - Optional relatedTo value
ThreadText: # GraphQL - Optional relatedTo value
  
  (Asked by: username_0 on 2022-09-28)
  
  I have a component which lists events and can be configured to filter by category.
  
  Query:
  
  ```
  query Events($filter: [String]) {
    entries(
      section: "event"
      relatedToCategories: {group: "group", slug: $filter}
    ) {
      __typename
    }
  }
  
  ```
  
  Variables:
  
  ```js
  {
      "filter": null
  }
  
  ```
  
  The filtering is working fine in most cases except with events that don't have a group category. I haven't found any value for `$filter` that fetches those. Setting it to an empty array or null only gets the events that have at least one category.
  
  What's the correct approach?
  
  **Update**
  
  I also tried passing the entire filter object to the query:
  
  Query:
  
  ```
  query Events($filter: [CategoryCriteriaInput]) {
    entries(
      section: "event"
      relatedToCategories: $filter
    ) {
      __typename
    }
  }
  
  ```
  
  Variables:
  
  ```js
  {
      "filter": [{group: "group", slug: "some-slug"}]
  }
  
  ```
  
  This works when a filter is set but not without one.
  
  Passing `null` or `[]` throws this error: `Calling unknown method: craft\\elements\\db\\EntryQuery::relatedToCategories()`
  Passing `[{}]` again only returns the entries which have any category set.
  
  **Tags:** categories, relations, relatedto, graphql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-10-10. Score: 1
  
  I figured it out. The second option (sending the whole `filter` as a variable) but instead of passing `null` or `[]` simply omitting it (or setting it to `undefined`) worked. Not sure why `null` and `undefined` act differently though.
  
  
  ---
  
  