diff --git a/netbox/dcim/graphql/gfk_mixins.py b/netbox/dcim/graphql/gfk_mixins.py index 2f669fb87..2a596a6f6 100644 --- a/netbox/dcim/graphql/gfk_mixins.py +++ b/netbox/dcim/graphql/gfk_mixins.py @@ -37,78 +37,6 @@ from dcim.models import ( ) -class LinkPeerType(graphene.Union): - class Meta: - types = ( - CircuitTerminationType, - ConsolePortType, - ConsoleServerPortType, - FrontPortType, - InterfaceType, - PowerFeedType, - PowerOutletType, - PowerPortType, - RearPortType, - ) - - @classmethod - def resolve_type(cls, instance, info): - if type(instance) is CircuitTermination: - return CircuitTerminationType - if type(instance) is ConsolePortType: - return ConsolePortType - if type(instance) is ConsoleServerPort: - return ConsoleServerPortType - if type(instance) is FrontPort: - return FrontPortType - if type(instance) is Interface: - return InterfaceType - if type(instance) is PowerFeed: - return PowerFeedType - if type(instance) is PowerOutlet: - return PowerOutletType - if type(instance) is PowerPort: - return PowerPortType - if type(instance) is RearPort: - return RearPortType - - -class CableTerminationTerminationType(graphene.Union): - class Meta: - types = ( - CircuitTerminationType, - ConsolePortType, - ConsoleServerPortType, - FrontPortType, - InterfaceType, - PowerFeedType, - PowerOutletType, - PowerPortType, - RearPortType, - ) - - @classmethod - def resolve_type(cls, instance, info): - if type(instance) is CircuitTermination: - return CircuitTerminationType - if type(instance) is ConsolePortType: - return ConsolePortType - if type(instance) is ConsoleServerPort: - return ConsoleServerPortType - if type(instance) is FrontPort: - return FrontPortType - if type(instance) is Interface: - return InterfaceType - if type(instance) is PowerFeed: - return PowerFeedType - if type(instance) is PowerOutlet: - return PowerOutletType - if type(instance) is PowerPort: - return PowerPortType - if type(instance) is RearPort: - return RearPortType - - class InventoryItemTemplateComponentType(graphene.Union): class Meta: types = ( diff --git a/netbox/dcim/graphql/mixins.py b/netbox/dcim/graphql/mixins.py index 8241b7de5..3d6a9fb1a 100644 --- a/netbox/dcim/graphql/mixins.py +++ b/netbox/dcim/graphql/mixins.py @@ -1,20 +1,40 @@ -import graphene +import strawberry +import strawberry_django +from typing import TYPE_CHECKING, Annotated, List, Union + +__all__ = ( + 'CabledObjectMixin', + 'PathEndpointMixin', +) +@strawberry.type class CabledObjectMixin: - link_peers = graphene.List('dcim.graphql.gfk_mixins.LinkPeerType') - def resolve_cable_end(self, info): - # Handle empty values - return self.cable_end or None + # @strawberry_django.field + # def cable_end(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]: + # # Handle empty values + # return self.cable_end or None - def resolve_link_peers(self, info): + @strawberry_django.field + def link_peers(self) -> List[Annotated[Union[ + Annotated["CircuitTerminationType", strawberry.lazy('circuits.graphql.types')], + Annotated["ConsolePortType", strawberry.lazy('dcim.graphql.types')], + Annotated["ConsoleServerPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["FrontPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerFeedType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerOutletType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["RearPortType", strawberry.lazy('dcim.graphql.types')], + ], strawberry.union("LinkPeerType")]]: return self.link_peers +@strawberry.type class PathEndpointMixin: - connected_endpoints = graphene.List('dcim.graphql.gfk_mixins.ConnectedEndpointType') - - def resolve_connected_endpoints(self, info): - # Handle empty values - return self.connected_endpoints or None + pass + # @strawberry_django.field + # def connected_endpoints(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]: + # # Handle empty values + # return self.connected_endpoints or None diff --git a/netbox/dcim/graphql/types.py b/netbox/dcim/graphql/types.py index 6f61f2139..016c1a557 100644 --- a/netbox/dcim/graphql/types.py +++ b/netbox/dcim/graphql/types.py @@ -1,12 +1,24 @@ +from typing import Annotated, List, Union + import strawberry import strawberry_django - from dcim import models from extras.graphql.mixins import ( - ChangelogMixin, ConfigContextMixin, ContactsMixin, CustomFieldsMixin, ImageAttachmentsMixin, TagsMixin, + ChangelogMixin, + ConfigContextMixin, + ContactsMixin, + CustomFieldsMixin, + ImageAttachmentsMixin, + TagsMixin, ) from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin -from netbox.graphql.types import BaseObjectType, OrganizationalObjectType, NetBoxObjectType + +from netbox.graphql.types import ( + BaseObjectType, + NetBoxObjectType, + OrganizationalObjectType, +) + from .filters import * from .mixins import CabledObjectMixin, PathEndpointMixin @@ -88,6 +100,29 @@ class ComponentTemplateObjectType( # Model types # +@strawberry_django.type( + models.CableTermination, + exclude=('termination_type', 'termination_id'), + filters=CableTerminationFilter +) +class CableTerminationType(NetBoxObjectType): + + @strawberry_django.field + def termination(self) -> List[Annotated[Union[ + Annotated["CircuitTerminationType", strawberry.lazy('circuits.graphql.types')], + Annotated["ConsolePortType", strawberry.lazy('dcim.graphql.types')], + Annotated["ConsoleServerPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["FrontPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerFeedType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerOutletType", strawberry.lazy('dcim.graphql.types')], + Annotated["PowerPortType", strawberry.lazy('dcim.graphql.types')], + Annotated["RearPortType", strawberry.lazy('dcim.graphql.types')], + + ], strawberry.union("CableTerminationTerminationType")]]: + return self.termination + + @strawberry_django.type( models.Cable, # fields='__all__', @@ -95,32 +130,20 @@ class ComponentTemplateObjectType( filters=CableFilter ) class CableType(NetBoxObjectType): - # a_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType') - # b_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType') - def resolve_type(self, info): - return self.type or None + @strawberry_django.field + def terminations(self) -> List[CableTerminationType]: + return self.terminations - def resolve_length_unit(self, info): - return self.length_unit or None - - def resolve_a_terminations(self, info): + @strawberry_django.field + def a_terminations(self) -> List[CableTerminationType]: return self.a_terminations - def resolve_b_terminations(self, info): + @strawberry_django.field + def b_terminations(self) -> List[CableTerminationType]: return self.b_terminations -@strawberry_django.type( - models.CableTermination, - exclude=('termination_type', 'termination_id'), - filters=CableTerminationFilter -) -class CableTerminationType(NetBoxObjectType): - # termination = graphene.Field('dcim.graphql.gfk_mixins.CableTerminationTerminationType') - pass - - @strawberry_django.type( models.ConsolePort, # exclude=('_path',),