9856 cleanup graphene

This commit is contained in:
Arthur 2024-03-11 07:31:38 -07:00
parent 7fa36cada5
commit ccc81e73d1
7 changed files with 32 additions and 157 deletions

View File

@ -507,10 +507,6 @@ class InterfaceType(IPAddressesMixin, ModularComponentType, CabledObjectMixin, P
def child_interfaces(self) -> List[Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')]]: def child_interfaces(self) -> List[Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')]]:
return self.child_interfaces.all() return self.child_interfaces.all()
@strawberry_django.field
def ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]:
return self.ip_addresses.all()
@strawberry_django.type( @strawberry_django.type(
models.InterfaceTemplate, models.InterfaceTemplate,
@ -596,10 +592,6 @@ class LocationType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, Organi
def racks(self) -> List[Annotated["RackType", strawberry.lazy('dcim.graphql.types')]]: def racks(self) -> List[Annotated["RackType", strawberry.lazy('dcim.graphql.types')]]:
return self.racks.all() return self.racks.all()
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.field @strawberry_django.field
def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]: def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]:
return self.devices.all() return self.devices.all()
@ -853,10 +845,6 @@ class RackType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, NetBoxObje
def cabletermination_set(self) -> List[Annotated["CableTerminationType", strawberry.lazy('dcim.graphql.types')]]: def cabletermination_set(self) -> List[Annotated["CableTerminationType", strawberry.lazy('dcim.graphql.types')]]:
return self.cabletermination_set.all() return self.cabletermination_set.all()
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.type( @strawberry_django.type(
models.RackReservation, models.RackReservation,
@ -922,10 +910,6 @@ class RegionType(VLANGroupsMixin, ContactsMixin, OrganizationalObjectType):
def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]: def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]:
return self.sites.all() return self.sites.all()
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.field @strawberry_django.field
def parent(self) -> Annotated["RegionType", strawberry.lazy('dcim.graphql.types')] | None: def parent(self) -> Annotated["RegionType", strawberry.lazy('dcim.graphql.types')] | None:
return self.parent return self.parent
@ -991,10 +975,6 @@ class SiteType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, NetBoxObje
def vlans(self) -> List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]: def vlans(self) -> List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlans.all() return self.vlans.all()
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.type( @strawberry_django.type(
models.SiteGroup, models.SiteGroup,
@ -1008,10 +988,6 @@ class SiteGroupType(VLANGroupsMixin, ContactsMixin, OrganizationalObjectType):
def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]: def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]:
return self.sites.all() return self.sites.all()
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.field @strawberry_django.field
def parent(self) -> Annotated["SiteGroupType", strawberry.lazy('dcim.graphql.types')] | None: def parent(self) -> Annotated["SiteGroupType", strawberry.lazy('dcim.graphql.types')] | None:
return self.parent return self.parent

View File

@ -1,95 +0,0 @@
import graphene
from dcim.graphql.types import (
InterfaceType,
LocationType,
RackType,
RegionType,
SiteGroupType,
SiteType,
)
from dcim.models import Interface, Location, Rack, Region, Site, SiteGroup
from ipam.graphql.types import FHRPGroupType, VLANType
from ipam.models import VLAN, FHRPGroup
from virtualization.graphql.types import ClusterGroupType, ClusterType, VMInterfaceType
from virtualization.models import Cluster, ClusterGroup, VMInterface
class IPAddressAssignmentType(graphene.Union):
class Meta:
types = (
InterfaceType,
FHRPGroupType,
VMInterfaceType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is Interface:
return InterfaceType
if type(instance) is FHRPGroup:
return FHRPGroupType
if type(instance) is VMInterface:
return VMInterfaceType
class L2VPNAssignmentType(graphene.Union):
class Meta:
types = (
InterfaceType,
VLANType,
VMInterfaceType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is Interface:
return InterfaceType
if type(instance) is VLAN:
return VLANType
if type(instance) is VMInterface:
return VMInterfaceType
class FHRPGroupInterfaceType(graphene.Union):
class Meta:
types = (
InterfaceType,
VMInterfaceType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is Interface:
return InterfaceType
if type(instance) is VMInterface:
return VMInterfaceType
class VLANGroupScopeType(graphene.Union):
class Meta:
types = (
ClusterType,
ClusterGroupType,
LocationType,
RackType,
RegionType,
SiteType,
SiteGroupType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is Cluster:
return ClusterType
if type(instance) is ClusterGroup:
return ClusterGroupType
if type(instance) is Location:
return LocationType
if type(instance) is Rack:
return RackType
if type(instance) is Region:
return RegionType
if type(instance) is Site:
return SiteType
if type(instance) is SiteGroup:
return SiteGroupType

View File

@ -1,4 +1,6 @@
import graphene import strawberry
import strawberry_django
from typing import TYPE_CHECKING, Annotated, List, Union
__all__ = ( __all__ = (
'IPAddressesMixin', 'IPAddressesMixin',
@ -6,15 +8,15 @@ __all__ = (
) )
@strawberry.type
class IPAddressesMixin: class IPAddressesMixin:
ip_addresses = graphene.List('ipam.graphql.types.IPAddressType') @strawberry_django.field
def ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]:
def resolve_ip_addresses(self, info): return self.ip_addresses.all()
return self.ip_addresses.restrict(info.context.request.user, 'view')
@strawberry.type
class VLANGroupsMixin: class VLANGroupsMixin:
vlan_groups = graphene.List('ipam.graphql.types.VLANGroupType') @strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
def resolve_vlan_groups(self, info): return self.vlan_groups.all()
return self.vlan_groups.restrict(info.context.request.user, 'view')

View File

@ -14,6 +14,7 @@ class DummyModelType:
pass pass
"""
@strawberry.type @strawberry.type
class DummyQuery: class DummyQuery:
@strawberry.field @strawberry.field
@ -21,8 +22,18 @@ class DummyQuery:
return None return None
dummymodel_list: List[DummyModelType] = strawberry_django.field() dummymodel_list: List[DummyModelType] = strawberry_django.field()
# bug - temp - FIXME! schema = strawberry.Schema(
# schema = strawberry.Schema( query=DummyQuery,
# query=DummyQuery, # config=StrawberryConfig(auto_camel_case=False),
# config=StrawberryConfig(auto_camel_case=False), )
# ) """
@strawberry.type
class Query:
fruits: list[int] = strawberry_django.field()
schema2 = strawberry.Schema(
query=Query,
)

View File

@ -18,11 +18,12 @@ __all__ = (
) )
@strawberry.type
class ContactAssignmentsMixin: class ContactAssignmentsMixin:
# assignments = graphene.List('tenancy.graphql.types.ContactAssignmentType')
def resolve_assignments(self, info): @strawberry_django.field
return self.assignments.restrict(info.context.user, 'view') def assignments(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
return self.assignments.all()
# #
@ -159,10 +160,6 @@ class TenantGroupType(OrganizationalObjectType):
class ContactType(ContactAssignmentsMixin, NetBoxObjectType): class ContactType(ContactAssignmentsMixin, NetBoxObjectType):
group: Annotated["ContactGroupType", strawberry.lazy('tenancy.graphql.types')] | None group: Annotated["ContactGroupType", strawberry.lazy('tenancy.graphql.types')] | None
@strawberry_django.field
def assignments(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
return self.assignments.all()
@strawberry_django.type( @strawberry_django.type(
models.ContactRole, models.ContactRole,
@ -170,10 +167,7 @@ class ContactType(ContactAssignmentsMixin, NetBoxObjectType):
filters=ContactRoleFilter filters=ContactRoleFilter
) )
class ContactRoleType(ContactAssignmentsMixin, OrganizationalObjectType): class ContactRoleType(ContactAssignmentsMixin, OrganizationalObjectType):
pass
@strawberry_django.field
def assignments(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
return self.assignments.all()
@strawberry_django.type( @strawberry_django.type(

View File

@ -7,7 +7,6 @@ from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.urls import reverse from django.urls import reverse
from django.test import override_settings from django.test import override_settings
# from graphene.types import Dynamic as GQLDynamic, List as GQLList, Union as GQLUnion, String as GQLString, NonNull as GQLNonNull
from rest_framework import status from rest_framework import status
from rest_framework.test import APIClient from rest_framework.test import APIClient

View File

@ -44,10 +44,6 @@ class ClusterType(VLANGroupsMixin, NetBoxObjectType):
def virtual_machines(self) -> List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]: def virtual_machines(self) -> List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]:
return self.virtual_machines.all() 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 @strawberry_django.field
def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]: def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]:
return self.devices.all() return self.devices.all()
@ -60,10 +56,6 @@ class ClusterType(VLANGroupsMixin, NetBoxObjectType):
) )
class ClusterGroupType(VLANGroupsMixin, OrganizationalObjectType): class ClusterGroupType(VLANGroupsMixin, OrganizationalObjectType):
@strawberry_django.field
def vlan_groups(self) -> List[Annotated["VLANGroupType", strawberry.lazy('ipam.graphql.types')]]:
return self.vlan_groups.all()
@strawberry_django.field @strawberry_django.field
def clusters(self) -> List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]: def clusters(self) -> List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]:
return self.clusters.all() return self.clusters.all()
@ -125,10 +117,6 @@ class VMInterfaceType(IPAddressesMixin, ComponentType):
untagged_vlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None untagged_vlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None
vrf: Annotated["VRFType", strawberry.lazy('ipam.graphql.types')] | None vrf: Annotated["VRFType", strawberry.lazy('ipam.graphql.types')] | None
@strawberry_django.field
def ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]:
return self.ip_addresses.all()
@strawberry_django.field @strawberry_django.field
def tagged_vlans(self) -> List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]: def tagged_vlans(self) -> List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]:
return self.tagged_vlans.all() return self.tagged_vlans.all()