diff --git a/netbox/circuits/graphql/types.py b/netbox/circuits/graphql/types.py index f8c73cac2..83244a098 100644 --- a/netbox/circuits/graphql/types.py +++ b/netbox/circuits/graphql/types.py @@ -1,4 +1,4 @@ -from typing import List +from typing import Annotated, List import strawberry import strawberry_django @@ -59,12 +59,15 @@ class CircuitTerminationType(CustomFieldsMixin, TagsMixin, CabledObjectMixin, Ob @strawberry_django.type( models.CircuitType, - # fields='__all__', - exclude=['color',], # bug - remove color from exclude + fields='__all__', filters=CircuitTypeFilter ) class CircuitTypeType(OrganizationalObjectType): - pass + color: str + + @strawberry_django.field + def circuits(self) -> List[Annotated["CircuitType", strawberry.lazy('circuits.graphql.types')]]: + return self.circuits.all() @strawberry_django.type( diff --git a/netbox/dcim/graphql/gfk_mixins.py b/netbox/dcim/graphql/gfk_mixins.py index 2a596a6f6..547cf24bc 100644 --- a/netbox/dcim/graphql/gfk_mixins.py +++ b/netbox/dcim/graphql/gfk_mixins.py @@ -1,4 +1,7 @@ -import graphene +from typing import TYPE_CHECKING, Annotated, List, Union + +import strawberry +import strawberry_django from circuits.graphql.types import CircuitTerminationType, ProviderNetworkType from circuits.models import CircuitTermination, ProviderNetwork from dcim.graphql.types import ( diff --git a/netbox/dcim/graphql/mixins.py b/netbox/dcim/graphql/mixins.py index 3d6a9fb1a..e6ddb7c1e 100644 --- a/netbox/dcim/graphql/mixins.py +++ b/netbox/dcim/graphql/mixins.py @@ -2,6 +2,7 @@ import strawberry import strawberry_django from typing import TYPE_CHECKING, Annotated, List, Union + __all__ = ( 'CabledObjectMixin', 'PathEndpointMixin', @@ -33,8 +34,18 @@ class CabledObjectMixin: @strawberry.type class PathEndpointMixin: - pass - # @strawberry_django.field - # def connected_endpoints(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]: - # # Handle empty values - # return self.connected_endpoints or None + + @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["ProviderNetworkType", strawberry.lazy('dcim.graphql.types')], + Annotated["RearPortType", strawberry.lazy('dcim.graphql.types')], + ], strawberry.union("ConnectedEndpointType")]]: + return self.connected_endpoints or None diff --git a/netbox/dcim/graphql/types.py b/netbox/dcim/graphql/types.py index 028d12091..c7e166f07 100644 --- a/netbox/dcim/graphql/types.py +++ b/netbox/dcim/graphql/types.py @@ -124,11 +124,11 @@ class CableTerminationType(NetBoxObjectType): @strawberry_django.type( models.Cable, - # fields='__all__', - exclude=('color', ), # bug - temp + fields='__all__', filters=CableFilter ) class CableType(NetBoxObjectType): + color: str @strawberry_django.field def terminations(self) -> List[CableTerminationType]: @@ -161,6 +161,7 @@ class ConsolePortType(ComponentObjectType, CabledObjectMixin, PathEndpointMixin) filters=ConsolePortTemplateFilter ) class ConsolePortTemplateType(ComponentTemplateObjectType): + _name: str def resolve_type(self, info): return self.type or None diff --git a/netbox/extras/graphql/types.py b/netbox/extras/graphql/types.py index e89802b85..2056d01ee 100644 --- a/netbox/extras/graphql/types.py +++ b/netbox/extras/graphql/types.py @@ -1,3 +1,5 @@ +from typing import Annotated, List + import strawberry import strawberry_django @@ -35,6 +37,58 @@ __all__ = ( class ConfigContextType(ObjectType): pass + @strawberry_django.field + def roles(self) -> List[Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def device_types(self) -> List[Annotated["DeviceTypeType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def tags(self) -> List[Annotated["TagType", strawberry.lazy('extras.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def platforms(self) -> List[Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def regions(self) -> List[Annotated["RegionType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def cluster_groups(self) -> List[Annotated["ClusterGroupType", strawberry.lazy('virtualization.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def tenant_groups(self) -> List[Annotated["TenantGroupType", strawberry.lazy('tenancy.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def cluster_types(self) -> List[Annotated["ClusterTypeType", strawberry.lazy('virtualization.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def clusters(self) -> List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def locations(self) -> List[Annotated["LocationType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def tenants(self) -> List[Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def site_groups(self) -> List[Annotated["SiteGroupType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + @strawberry_django.type( models.ConfigTemplate, @@ -42,7 +96,22 @@ class ConfigContextType(ObjectType): filters=ConfigTemplateFilter ) class ConfigTemplateType(TagsMixin, ObjectType): - pass + + @strawberry_django.field + def virtualmachines(self) -> List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def platforms(self) -> List[Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def device_roles(self) -> List[Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')]]: + return self.vlan_groups.all() @strawberry_django.type( diff --git a/netbox/netbox/graphql/types.py b/netbox/netbox/graphql/types.py index 25dedd696..f1a7bcfe5 100644 --- a/netbox/netbox/graphql/types.py +++ b/netbox/netbox/graphql/types.py @@ -31,7 +31,10 @@ class BaseObjectType: @classmethod def get_queryset(cls, queryset, info, **kwargs): # Enforce object permissions on the queryset - return queryset.restrict(info.context.request.user, 'view') + if hasattr(queryset, 'restrict'): + return queryset.restrict(info.context.request.user, 'view') + else: + return queryset @strawberry_django.field def display(self) -> str: diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 082e29e35..3cc352eb8 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -743,7 +743,6 @@ if not ENABLE_LOCALIZATION: # STRAWBERRY_DJANGO = { "TYPE_DESCRIPTION_FROM_MODEL_DOCSTRING": True, - # "GENERATE_ENUMS_FROM_CHOICES": True, } # diff --git a/netbox/virtualization/graphql/types.py b/netbox/virtualization/graphql/types.py index f4c92035b..15c7766f7 100644 --- a/netbox/virtualization/graphql/types.py +++ b/netbox/virtualization/graphql/types.py @@ -1,3 +1,5 @@ +from typing import Annotated, List + import strawberry import strawberry_django @@ -24,7 +26,18 @@ __all__ = ( filters=ClusterFilter ) class ClusterType(VLANGroupsMixin, NetBoxObjectType): - pass + + @strawberry_django.field + def virtual_machines(self) -> List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]: + return self.virtual_machines.all() + + @strawberry_django.field + def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]: + return self.devices.all() @strawberry_django.type( @@ -33,7 +46,14 @@ class ClusterType(VLANGroupsMixin, NetBoxObjectType): filters=ClusterGroupFilter ) class ClusterGroupType(VLANGroupsMixin, OrganizationalObjectType): - pass + + @strawberry_django.field + def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]: + return self.vlan_groups.all() + + @strawberry_django.field + def clusters(self) -> List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]: + return self.clusters.all() @strawberry_django.type( @@ -42,7 +62,10 @@ class ClusterGroupType(VLANGroupsMixin, OrganizationalObjectType): filters=ClusterTypeFilter ) class ClusterTypeType(OrganizationalObjectType): - pass + + @strawberry_django.field + def clusters(self) -> List[ClusterType]: + return self.clusters.all() @strawberry_django.type(