Cms: Problem querying lightswitch fields via GraphQL

Created on 16 Apr 2020  Â·  12Comments  Â·  Source: craftcms/cms

Description

When querying entries via GraphQL using a lightswitch field as a condition, Craft will only and always return entries where the field is set to 1/true.

Note: The same query in Twig runs just fine.

Steps to reproduce

  1. Create a section with a lightswitch field
  2. Switch some entries to true, some to false
  3. Try to query for entries where lightswitchField: "0"

Complete query:

query {
  entries(
    section: "yourSection"
    lightswitchField: "0"
  ) {
    id,
    title
    ... on yourSection_yourEntryType_Entry {
      lightswitchField
    }
  }
}

Additional info

  • Craft version: 3.4.15
bug

All 12 comments

It's definitely a bug in parsing the arguments. However, if implemented haphazardly in 3.4, it might not play nicely with mutations that are coming in 3.5, so I feel much more comfortable adding that for Craft 3.5 alongside mutations.

Hi Andris,

Great news to know a fix is coming in 3.5. Do you have an estimated due date for 3.5?

We have a project which is being effected by this and it is supposed to be launching pretty soonish. The only work around we have atm is to remove the field from the query and then filter out the entries based on the value on the frontend, which we would prefer to avoid on the production site.

Knowing we could let the client know the issue will be resolved before we go live would be great.

Cheers,
Harry

I'll see if we have a date or an estimate we can share and I'll get back to you!

Also, this has been fixed for 3.5

Hey @harrymee123, we’re going to get a 3.5 Beta out early next week.

That’s great. Thanks for letting me know @brandonkelly !

@brandonkelly any updates when 3.5 will be released? we are having a project launch next week and this is one of the last issues we need to solve before the launch.

@brandonkelly is there no way to publish a workaround that can be implemented in 3.4 ?
we have a project that needs this feature so we are able to launch, and I can't use a beta version for production.

While it's definitely in the realm of possible, fixing this bug involved some architectural changes, so it would make more sense for it to be in 3.5.

@denisyilmaz what's your timeline for this project? 3.5 GA could be closer than you think :)

We are currently in final bug fixing stage, besides this bug we only have one other bug related to angular that keeps us from publishing. So we aim to go live in the next 10 days or so…

Good to hear 3.5 is coming closer. 🎉

To get around this issue on a production build, we swapped the Lightswitch field for a Radio Buttons field with the values 1 & 0 or Yes & No.

This required minimal changes within the GraphQL variables, then also should require minimal changes for when 3.5 has been released.

@harrymee123 thanks for that tip, I actually did not think about that. but I just tested it on my local installation. i can successfully query for multiple sections with that field in it ("1", "not 0", ":empty" are all working correctly).

Here is my query:

{
entries(
    type: ["project", "blogEntry"]
    orderBy: "postDate desc"
    limit: 10
    offset: 0
    showInBlog_temp: [":empty:", "not 0"]
  ) {
    title
    typeHandle
    ... on projects_project_Entry {
      id
      showInBlog_temp
    }
    ... on blogEntry_blogEntry_Entry {
      id
    }
  }
}

thanks again @harrymee123 for the tip!

@denisyilmaz glad you got a decent working solution without any hacks involved!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leigeber picture leigeber  Â·  3Comments

bitboxfw picture bitboxfw  Â·  3Comments

timkelty picture timkelty  Â·  3Comments

angrybrad picture angrybrad  Â·  3Comments

Mosnar picture Mosnar  Â·  3Comments