Cms: GraphQL: Checkbox and multi-select fields are not being filtered

Created on 12 Feb 2020  ·  5Comments  ·  Source: craftcms/cms

Description

If you have a checkbox or multi-select field that you need to filter for in the GraphQL API you will get no results.

For example if I run this query:

query Employees ($employeeGroups: [QueryArgument]) {
  entries (section: "employees", employeeGroups: $employeeGroups) {
    ... on employees_employees_Entry {
      title
      employeeGroups
    }
  }
}

With the following variables:

{
  "employeeGroups": "board"
}

I get the following results:

{
  "data": {
    "entries": []
  }
}

Although, if I'm not supplying a variable I get the following results (as expected):

{
  "data": {
    "entries": [
      {
        "title": "Firstname Surname",
        "employeeGroups": [
          "board"
        ]
      }
    ]
  }
}

The above result is what I'm looking to get when supplying a variable for employeeGroups with the board value.

I have also tried to supply an array as the employeeGroups variable but it makes no difference, although that's expected as the description for a QueryArgument states: It can be both an integer or a string..

Perhaps array support should be added for checkboxes and multi-select field types because these fields are multiple choice?

Steps to reproduce

  1. Create a section
  2. Create a checkbox or multi-select field and assign in to the section's field layout
  3. Create an article in this section and select one of your checkbox or multi-select values
  4. Query the GraphQL API using either the endpoint or the GraphQL explorer in the control panel and filter for your field, you will get no results even though you're filtering for the value the field contains.

Additional info

  • Craft version: Craft Pro 3.4.5
  • PHP version: 7.4.0
  • Database driver & version: MySQL 5.7.28
  • Plugins & versions: N/A
enhancement graphql

Most helpful comment

Just pushed an update to Checkboxes and Multi-select fields for the next Craft 3.4 release. The following parameter syntaxes are now supported as of 3.4.6:

| Value | Fetches elements…
| - | -
| "foo" | with a foo option selected.
| "not foo" | without a foo option selected.
| ["foo", "bar"] | with foo or bar options selected.
| ["and", "foo", "bar"] | with foo and bar options selected.

All 5 comments

As per the element query docs, you should be setting it like this:

{
  "employeeGroups": "*\"board\"*"
}

I agree, though, that it is incredibly awkward. We'll discuss how to better address this as I have a few ideas.

Great, that would be awesome 👍

Just pushed an update to Checkboxes and Multi-select fields for the next Craft 3.4 release. The following parameter syntaxes are now supported as of 3.4.6:

| Value | Fetches elements…
| - | -
| "foo" | with a foo option selected.
| "not foo" | without a foo option selected.
| ["foo", "bar"] | with foo or bar options selected.
| ["and", "foo", "bar"] | with foo and bar options selected.

Great work! This makes it infinitely more logical to use! 🥇

Craft 3.4.6 is out now with this change.

Was this page helpful?
0 / 5 - 0 ratings