graphile-contrib / postgraphile-plugin-connection-filter

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

ConnectionFilterLogicalOperator 'OR' doesn't behave as expected #174

Closed tobiasjeckel closed 2 years ago

tobiasjeckel commented 2 years ago

Hello, I am attempting to use the logical operator 'OR 'to chain several filters. Please view following example:

query MyQuery {
  allVehicles {
    edges {
      node {
        vehicleName
        vehicleNumber
      }
    }
  }
}

gives me following result:

{
  "data": {
    "allVehicles": {
      "edges": [
        {
          "node": {
            "vehicleName": "Toyota",
            "vehicleNumber": "123"
          }
        },
        {
          "node": {
            "vehicleName": "VW",
            "vehicleNumber": "456"
          }
        },
        {
          "node": {
            "vehicleName": "Mercedes",
            "vehicleNumber": "789"
          }
        }
      ]
    }
  }
}

I then wish to filter my query:

query MyQuery {
  allVehicles(
    filter: {vehicleNumber: {includesInsensitive: "123"}, or: {vehicleName: {includesInsensitive: "merc"}}}
  ) {
    edges {
      node {
        vehicleName
        vehicleNumber
      }
    }
  }
}

My expected result in this case would be to see the 'mercedes' and 'toyota' vehicle. However in this case I receive an empty result:

{
  "data": {
    "allVehicles": {
      "edges": []
    }
  }
}

I have found that the 'OR' operator essentially behaves like an 'AND' operator and not like expected.

Am I using the filter correctly or is there a bug?

Thanks

jwrascoe commented 2 years ago

@tobiasjeckel, for your situation you will need to implement OR like this.

filter: { or: [ {vehicleNumber: {includesInsensitive: "123"} {vehicleName: {includesInsensitive: "merc"}} ] }

tobiasjeckel commented 2 years ago

Thank you so much 💯 it works like this 😃

It seems I used the wrong syntax without an array as this was the syntax that was suggested to me by using the /graphiql explorer. Perhaps this could be improved in the documentation or the /graphiql explorer?

Thanks

jwrascoe commented 2 years ago

@tobiasjeckel, complex filtering can be difficult to understand at times.

This is a great resource to understand what is possible so you can implement your own situation.

https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/blob/master/docs/examples.md

pianomanfrazier commented 2 years ago

I had the same issue. The default way that graphiql presents the queries does not work. Glad I found this thread.