-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New GraphQL filters (AND, OR and NOT) don't work as intended #16024
Comments
I'm able to reproduce this problem on a fresh 4.0.0 install with two VMs with status "active" and two VMs with status "staged". My level of GraphQL sophistication is currently low, so I might have missed something subtle. |
Thanks for reporting a problem you've encountered in NetBox. My role in this moment is limited to triage, so you may get questions from the developers who understand these things more deeply. |
I'm not sure this filter logic is meant to be supported currently. I know we've had to do some extensive customization with the GraphQL filters to support django-filters integration. Maybe @arthanson can weigh in? |
In my opinion, we have a regression here if the new filter logic is not yet supported, because the support for the |
Dug into this, the issue here is Strawberry requires Q objects to be returned from the filter functions in order to do the AND, OR, NOT operations, but when we call django-filter we get back a queryset. I don't see any built-in way to get a Q object from django-filter. It should be possible to basically duplicate some of what django-filter is doing and take a list of query-params and create a Q object from them. That should be basically what django-filter is doing internally before it uses them to generate the queryset. |
What is the status of this now? I would like to emphasize once again that this is a regression, as it is now no longer possible to use |
As you can see from the assigned labels, this issue needs an owner. @NetaliDev would you like to volunteer to own this issue? |
Sorry, but I don't know enough about GraphQL, Strawberry and especially the netbox codebase to fix this. I just had the task to migrate our GraphQL queries at work to netbox 4.X and stumbled over this issue. |
Note: Make sure to test below syntax as well:
|
+1 here, I just found that the NOT filter doesn't work in 4.0.7 Query that works in 3.x: {device_list (status__n: "active", tenant: "tenant-name-here") {name status}} Because the AND and OR args are also broken I can't even workaround by selecting all statuses but the one I want |
The basic issue is strawberry filtering uses Q objects, django-filters returns filtersets which makes them incompatible. None of the solutions I see look very good and all would take a lot of work. Graphene interfaces with Django-filter and uses filtersets. Code is at https://github.com/graphql-python/graphene-django/tree/main/graphene_django/filter. About 8 files of code. Strawberry is all based off of Q objects. Code is at https://github.com/strawberry-graphql/strawberry-django/blob/main/strawberry_django/filters.py. Strawberry documentation on filters is at: https://strawberry.rocks/docs/django/guide/filters From what I can see, this is also tied into the GraphQL parsing framework, so if we tried to use Django-filter we would also probably have to make patches to that code as well. We are also using Strawberry Legacy Filtering which needs to be removed (https://strawberry.rocks/docs/django/guide/filters#legacy-filtering) this would need changes to the auto type decorator (https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/graphql/filter_mixins.py#L131). I can think of several different potential solutions (Option 4 might potentially be the best option?)
Support for Django-filter has been requested in Strawberry, but it doesn't look like it will be implemented (strawberry-graphql/strawberry-django#448).
|
I think we have at least a partial fix for this in PR #17638. The PR only impacts choice fields at the moment but can be extended as needed to accommodate other field types as well. It would be very helpful if users impacted by this bug could check out the PR branch and confirm whether it resolves the issues for them, as well as any outstanding issues remain with GraphQL filtering. Thanks! |
Deployment Type
Self-hosted
NetBox Version
v4.0.0
Python Version
3.11
Steps to Reproduce
Let's take the OR-Filter as an example: As described in the docs, I would like to filter for VMs that are in the
active
and in thestaged
status:Returns:
As seen, this query returns an empty result, while distinct queries for
staged
andactive
VMs return results:Returns:
And
Returns:
Or another example: the NOT-Filter: let's assume that I want to filter for all VMs that are not
active
:But for some reason, this only returns
active
VMs:Expected Behavior
That the OR-Filter returns
active
andstaged
VMs and that the NOT-Filter returns everything exceptactive
VMs.Observed Behavior
Described above.
The text was updated successfully, but these errors were encountered: