Arthur 2 лет назад
Родитель
Сommit
1aa5b0d5a1
3 измененных файлов с 76 добавлено и 105 удалено
  1. 0 72
      netbox/dcim/graphql/gfk_mixins.py
  2. 31 11
      netbox/dcim/graphql/mixins.py
  3. 45 22
      netbox/dcim/graphql/types.py

+ 0 - 72
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 = (

+ 31 - 11
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

+ 45 - 22
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
 
-    def resolve_length_unit(self, info):
-        return self.length_unit or None
+    @strawberry_django.field
+    def terminations(self) -> List[CableTerminationType]:
+        return self.terminations
 
-    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',),