platformOS Community

How do I filter by empty arrays in GraphQL?

Omid - Wysi Jan 27 2021 at 10:14

I am trying to write a query to only return records that have empty arrays.
Using the now deprecated Customizations, this could be achieved using;

  customizations(
      name: "some_data_store", 
      properties: {
        name: "my_array", exists: false
      }

This behavior seems to have changed when using Records, and I can no longer filter empty arrays.

Does anyone have a solution to this using Records?

Pawel Jan 27 2021 at 13:57

I think this should work (array_overlaps):

query GetRecords {
  records(per_page: 10, filter: {
    properties: { 
      name: "tags",
      not_array_overlaps: []
    }
  }) {
    results {
      id
      tags: property(name: "tags")
    }
  }
}

Of course there is also opposite of overlaps: not_array_overlaps which can be useful sometimes.

When you are in GraphiQL and you want to explore what are your options press cmd+space (or ctrl+space). More info on that: https://documentation.platformos.com/developer-guide/pos-cli/developing-graphql-queries-using-pos-cli-gui#content

  • Omid - Wysi Jan 28 2021 at 08:12
    Thank you Pawel, but this doesn't seem to return the expected results. In a scenario with both empty arrays [], and populated arrays ["10","20","30"], filtering properties by not_array_overlaps returns matches with both empty and populated arrays, filtering properties by array_overlaps returns no matched results. Any other ideas or suggestions?
SR Jan 28 2021 at 19:40

Try an empty array with value_in

query GetRecords {
  records(per_page: 10, filter: {
    properties: { 
      name: "tags",
      value_in: []
    }
  }) {
    results {
      id
      tags: property_array(name: "tags")
    }
  }
}
  • Rich - One Orange Cow Feb 01 2021 at 22:46
    I could be wrong but that would also return arrays with items that are blank. Its essentially a value_in is string search over an array (simular to an SQL like): eg. Would return: ["", "somedata", "otherdata"] and [""] Also line breaks in comments would be a good idea Maciek
  • Omid - Wysi Feb 02 2021 at 11:11
    I suspect Rich is right, this would also return empty string values within the array, however for my use case, this is not a concern, so this solution works for me. Thank you SR, this has enabled me to migrate a customization query to records. Much appreciated!
  • SR Feb 05 2021 at 19:32
    I updated my answer to address Rich's comment. I replaced "" with []. It should now only return records with an empty array.
Please sign in or fill up your profile to answer a question