graphile-contrib / postgraphile-plugin-connection-filter

Filtering on PostGraphile connections
MIT License
283 stars 32 forks source link

Are Empty Nested Results Expected? #172

Closed git-blame closed 2 years ago

git-blame commented 2 years ago

We wrote views, functions, etc. in postgresql to represent relations/joins for postgraphile. But now we are exploring whether it is simply more robust/efficient to express them in graphql. I'm looking at the nested example and it seems to do the job. But when I try with something for example:

query pdf($file: FileScanFilter) {
  allFiles(first: 50, filter: {type: {equalTo: "document"}}) {
    totalCount
    nodes {
      rowId
      latestFileScanByFileid(
        filter: $file) {
        nodes {
          resultsuri
        }
      }
    }
  }
}
{
  "file": { "resultsuri": { "endsWith": "pdf" } }
}

Instead of just getting results that match the filters on the 2 tables, namely "document" files that have "pdf" extensions. I do get those. But I get also empty results for:

For example:

{
  "data": {
    "allFiles": {
      "totalCount": 425,
      "nodes": [
        {
          "rowId": "121144",
          "latestFileScanByFileid": {
            "nodes": []
          }
        },
        {
          "rowId": "121189",
          "latestFileScanByFileid": {
            "nodes": [
              {
                "artifactid": "121189",
                "resultsuri": "file:///work/documents/test.pdf"
              },
...

My question is:

git-blame commented 2 years ago

Answering my own questions. Yes, nested filtering can result in empty nodes. If you want to remove non-matching results completely you will have to:

For my own example, it would be something like:

query pdf  {
  allFiles(first: 50, filter:
    and: [
      {type: {equalTo: "document"},
      {latestFileScanByFileid: {
        some: {
            resultsuri: { endsWith: "pdf" }
        }
      }},
    ]
  }) {
    totalCount
    nodes {
      rowId
      latestFileScanByFileid {
        nodes {
          resultsuri
        }
      }
    }
  }
}