netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.07k stars 2.58k forks source link

Add assigned_object models in GraphQL API similar to the REST API #9817

Closed ryanmerolle closed 2 years ago

ryanmerolle commented 2 years ago

NetBox version

v3.3-beta

Feature type

Change to existing functionality

Proposed functionality

As an example, currently you can only query:

{
  l2vpn_termination_list {
    assigned_object_type {
      model
    }
    assigned_object_id
  }
}

and it returns:

{
  "data": {
    "l2vpn_termination_list": [
      {
        "assigned_object_type": {
          "model": "interface"
        },
        "assigned_object_id": 1170
      }
    ]
  }
}

Where the rest api allows you to query the same model and get the assigned_object back:

{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 1,
      "url": "https://beta-demo.netbox.dev/api/ipam/l2vpn-terminations/1/",
      "display": "et-2/0/49 <> Test",
      "l2vpn": {
        "id": 1,
        "url": "https://beta-demo.netbox.dev/api/ipam/l2vpns/1/",
        "display": "Test",
        "identifier": null,
        "name": "Test",
        "slug": "test",
        "type": "vxlan-evpn"
      },
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": 1170,
      "assigned_object": {
        "id": 1170,
        "url": "https://beta-demo.netbox.dev/api/dcim/interfaces/1170/",
        "display": "et-2/0/49",
        "device": {
          "id": 99,
          "url": "https://beta-demo.netbox.dev/api/dcim/devices/99/",
          "display": "r105-torsw:2 (99)",
          "name": null
        },
        "name": "et-2/0/49",
        "cable": null,
        "_occupied": false
      },
      "tags": [],
      "custom_fields": {},
      "created": "2022-07-21T09:45:29.104279Z",
      "last_updated": "2022-07-21T09:45:29.104304Z"
    }
  ]

It would be ideal that all usage of any assigned_object/assigned_object_type approach would update the graphql api to return the assigned_object.

Use case

rest and graphql feature parity.

Database changes

No response

External dependencies

No response

ryanmerolle commented 2 years ago

Unfortunately it looks like it is not supported as is with graphen-django

This looks like a valid approach: https://stackoverflow.com/questions/56146966/is-there-a-way-to-get-graphene-to-work-with-django-genericrelation-field/69482585#69482585

jeremystretch commented 2 years ago

Unfortunately it looks like it is not supported as is with graphene-django

Given that graphene-django is no longer maintained, we'll likely be moving to an alternate implementation in a future release anyway.

jeremystretch commented 2 years ago

Blocked by #9856

arthanson commented 2 years ago

Using a UnionType looks like the preferred method - The Graphene docs has it in their test cases but not very clear, this https://wagtail.org/blog/graphql-with-streamfield/ is a fairly good example. Will implement with this pattern.