[PR #18012] [CLOSED] wip: Filter by custom fields in graph ql #15250

Closed
opened 2025-12-30 00:20:52 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/18012
Author: @christophbeberweil
Created: 11/14/2024
Status: Closed

Base: developHead: 7598-graphql-filtering-custom-fields


📝 Commits (1)

  • 3de7457 wip: Filter by custom fields in graph ql

📊 Changes

3 files changed (+211 additions, -116 deletions)

View changed files

📝 netbox/dcim/filtersets.py (+2 -1)
📝 netbox/netbox/filtersets.py (+140 -114)
📝 netbox/netbox/graphql/filter_mixins.py (+69 -1)

📄 Description

Works on : #7598

I worked on issue #7598 and made progress. Unfortunaltely, I hit some roadblocks and none of the maintainers were available to answer my questions. I open this merge request with my partial solution so that it might be of use for the project in the future.

I utilized autotype_decorator, which is used to communicate the filterset to graphql, in order to add CustomField entries with the same logic applied as in netbox.filtersets.NetBoxModelFilterSet. The code calls create_attribute_and_function for each custom field of the current model. This successfully adds a new filter to GraphQL, which is recognized in the backend. Unfortunately, there are still some problems:

  • The generated Filter itereates over the filter argument, so a filter by "Foo" filters by "F" OR "o" OR "o", which is not the desired functionality.
  • Additionally, filters on non-iterable datatypes such as int do not work.
  • After adding a new CustomField, the server needs to be restarted in order for GraphQL to be aware of the new field. Since GraphQL is a schema-based tool, it is not clear if the restart requirement can be avoided.
  • The lints in the GraphIQL-Frontend do not recognize the new CustomField filter. Nevertheless, applying the filter will lead to a change in the output, so it is recognized by the server. For instance, after creating a custom field a_string on Device, the following graphql statement can be used to filter the devices, but GraphIQL does not know of cf_a_string:
{
 device_list(filters: {cf_a_string: "a"}) {
  id
  name
  custom_fields
  local_context_data
  device_type {
   id
   display
   slug
  }
 }
}

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/18012 **Author:** [@christophbeberweil](https://github.com/christophbeberweil) **Created:** 11/14/2024 **Status:** ❌ Closed **Base:** `develop` ← **Head:** `7598-graphql-filtering-custom-fields` --- ### 📝 Commits (1) - [`3de7457`](https://github.com/netbox-community/netbox/commit/3de7457a3629534fd26f230575bd82c1206db258) wip: Filter by custom fields in graph ql ### 📊 Changes **3 files changed** (+211 additions, -116 deletions) <details> <summary>View changed files</summary> 📝 `netbox/dcim/filtersets.py` (+2 -1) 📝 `netbox/netbox/filtersets.py` (+140 -114) 📝 `netbox/netbox/graphql/filter_mixins.py` (+69 -1) </details> ### 📄 Description <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be approved and assigned prior to opening a pull request. This helps avoid waste time and effort on a proposed change that we might not be able to accept. IF YOUR PULL REQUEST DOES NOT REFERENCE AN ISSUE WHICH HAS BEEN ASSIGNED TO YOU, IT WILL BE CLOSED AUTOMATICALLY. Please specify your assigned issue number on the line below. --> ### Works on : #7598 <!-- Please include a summary of the proposed changes below. --> I worked on issue #7598 and made progress. Unfortunaltely, I hit some roadblocks and none of the maintainers were available to answer my questions. I open this merge request with my partial solution so that it might be of use for the project in the future. I utilized `autotype_decorator`, which is used to communicate the filterset to graphql, in order to add `CustomField` entries with the same logic applied as in `netbox.filtersets.NetBoxModelFilterSet`. The code calls `create_attribute_and_function` for each custom field of the current model. This successfully adds a new filter to GraphQL, which is recognized in the backend. Unfortunately, there are still some problems: - The generated Filter itereates over the filter argument, so a filter by "Foo" filters by "F" OR "o" OR "o", which is not the desired functionality. - Additionally, filters on non-iterable datatypes such as `int` do not work. - After adding a new CustomField, the server needs to be restarted in order for GraphQL to be aware of the new field. Since GraphQL is a schema-based tool, it is not clear if the restart requirement can be avoided. - The lints in the GraphIQL-Frontend do not recognize the new CustomField filter. Nevertheless, applying the filter will lead to a change in the output, so it is recognized by the server. For instance, after creating a custom field `a_string` on `Device`, the following graphql statement can be used to filter the devices, but GraphIQL does not know of `cf_a_string`: ```graphql { device_list(filters: {cf_a_string: "a"}) { id name custom_fields local_context_data device_type { id display slug } } } ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:20:52 +01:00
adam closed this issue 2025-12-30 00:20:53 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15250