mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-21 19:18:44 -06:00
9856 Replace graphene with Strawberry (#15141)
* 9856 base strawberry integration * 9856 user and group * 9856 user and circuits base * 9856 extras and mixins * 9856 fk * 9856 update strawberry version * 9856 update imports * 9856 compatability fixes * 9856 compatability fixes * 9856 update strawberry types * 9856 update strawberry types * 9856 core schema * 9856 dcim schema * 9856 extras schema * 9856 ipam and tenant schema * 9856 virtualization, vpn, wireless schema * 9856 fix old decorator * 9856 cleanup * 9856 cleanup * 9856 fixes to circuits type specifiers * 9856 fixes to circuits type specifiers * 9856 update types * 9856 GFK working * 9856 GFK working * 9856 _name * 9856 misc fixes * 9856 type updates * 9856 _name to types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 update types * 9856 GraphQLView * 9856 GraphQLView * 9856 fix OrganizationalObjectType * 9856 single item query for schema * 9856 circuits graphql tests working * 9856 test fixes * 9856 test fixes * 9856 test fixes * 9856 test fix vpn * 9856 test fixes * 9856 test fixes * 9856 test fixes * 9856 circuits test sans DjangoModelType * 9856 core test sans DjangoModelType * 9856 temp checkin * 9856 fix extas FK * 9856 fix tenancy FK * 9856 fix virtualization FK * 9856 fix vpn FK * 9856 fix wireless FK * 9856 fix ipam FK * 9856 fix partial dcim FK * 9856 fix dcim FK * 9856 fix virtualization FK * 9856 fix tests / remove debug code * 9856 fix test imagefield * 9856 cleanup graphene * 9856 fix plugin schema * 9856 fix requirements * 9856 fix requirements * 9856 fix docs * 9856 fix docs * 9856 temp fix tests * 9856 first filterset * 9856 first filterset * 9856 fix tests * 9856 fix tests * 9856 working auto filter generation * 9856 filter types * 9856 filter types * 9856 filter types * 9856 fix graphiql test * 9856 fix counter fields and merge feature * 9856 temp fix tests * 9856 fix tests * 9856 fix tenancy, ipam filter definitions * 9856 cleanup * 9856 cleanup * 9856 cleanup * 9856 review changes * 9856 review changes * 9856 review changes * 9856 fix base-requirements * 9856 add wrapper to graphiql * 9856 remove old graphiql debug toolbar * 9856 review changes * 9856 update strawberry * 9856 remove superfluous check --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
@@ -127,11 +127,10 @@ class ContactAssignmentFilterSet(NetBoxModelFilterSet):
|
||||
to_field_name='slug',
|
||||
label=_('Contact role (slug)'),
|
||||
)
|
||||
tag = TagFilter()
|
||||
|
||||
class Meta:
|
||||
model = ContactAssignment
|
||||
fields = ('id', 'object_type_id', 'object_id', 'priority', 'tag')
|
||||
fields = ('id', 'object_type_id', 'object_id', 'priority')
|
||||
|
||||
def search(self, queryset, name, value):
|
||||
if not value.strip():
|
||||
|
||||
49
netbox/tenancy/graphql/filters.py
Normal file
49
netbox/tenancy/graphql/filters.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import strawberry_django
|
||||
|
||||
from netbox.graphql.filter_mixins import autotype_decorator, BaseFilterMixin
|
||||
from tenancy import filtersets, models
|
||||
|
||||
__all__ = (
|
||||
'TenantFilter',
|
||||
'TenantGroupFilter',
|
||||
'ContactFilter',
|
||||
'ContactRoleFilter',
|
||||
'ContactGroupFilter',
|
||||
'ContactAssignmentFilter',
|
||||
)
|
||||
|
||||
|
||||
@strawberry_django.filter(models.Tenant, lookups=True)
|
||||
@autotype_decorator(filtersets.TenantFilterSet)
|
||||
class TenantFilter(BaseFilterMixin):
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.filter(models.TenantGroup, lookups=True)
|
||||
@autotype_decorator(filtersets.TenantGroupFilterSet)
|
||||
class TenantGroupFilter(BaseFilterMixin):
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.filter(models.Contact, lookups=True)
|
||||
@autotype_decorator(filtersets.ContactFilterSet)
|
||||
class ContactFilter(BaseFilterMixin):
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.filter(models.ContactRole, lookups=True)
|
||||
@autotype_decorator(filtersets.ContactRoleFilterSet)
|
||||
class ContactRoleFilter(BaseFilterMixin):
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.filter(models.ContactGroup, lookups=True)
|
||||
@autotype_decorator(filtersets.ContactGroupFilterSet)
|
||||
class ContactGroupFilter(BaseFilterMixin):
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.filter(models.ContactAssignment, lookups=True)
|
||||
@autotype_decorator(filtersets.ContactAssignmentFilterSet)
|
||||
class ContactAssignmentFilter(BaseFilterMixin):
|
||||
pass
|
||||
17
netbox/tenancy/graphql/mixins.py
Normal file
17
netbox/tenancy/graphql/mixins.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from typing import Annotated, List
|
||||
|
||||
import strawberry
|
||||
import strawberry_django
|
||||
|
||||
|
||||
__all__ = (
|
||||
'ContactAssignmentsMixin',
|
||||
)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class ContactAssignmentsMixin:
|
||||
|
||||
@strawberry_django.field
|
||||
def assignments(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
|
||||
return self.assignments.all()
|
||||
@@ -1,44 +1,40 @@
|
||||
import graphene
|
||||
from typing import List
|
||||
|
||||
import strawberry
|
||||
import strawberry_django
|
||||
|
||||
from netbox.graphql.fields import ObjectField, ObjectListField
|
||||
from tenancy import models
|
||||
from .types import *
|
||||
from utilities.graphql_optimizer import gql_query_optimizer
|
||||
|
||||
|
||||
class TenancyQuery(graphene.ObjectType):
|
||||
tenant = ObjectField(TenantType)
|
||||
tenant_list = ObjectListField(TenantType)
|
||||
@strawberry.type
|
||||
class TenancyQuery:
|
||||
@strawberry.field
|
||||
def tenant(self, id: int) -> TenantType:
|
||||
return models.Tenant.objects.get(pk=id)
|
||||
tenant_list: List[TenantType] = strawberry_django.field()
|
||||
|
||||
def resolve_tenant_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.Tenant.objects.all(), info)
|
||||
@strawberry.field
|
||||
def tenant_group(self, id: int) -> TenantGroupType:
|
||||
return models.TenantGroup.objects.get(pk=id)
|
||||
tenant_group_list: List[TenantGroupType] = strawberry_django.field()
|
||||
|
||||
tenant_group = ObjectField(TenantGroupType)
|
||||
tenant_group_list = ObjectListField(TenantGroupType)
|
||||
@strawberry.field
|
||||
def contact(self, id: int) -> ContactType:
|
||||
return models.Contact.objects.get(pk=id)
|
||||
contact_list: List[ContactType] = strawberry_django.field()
|
||||
|
||||
def resolve_tenant_group_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.TenantGroup.objects.all(), info)
|
||||
@strawberry.field
|
||||
def contact_role(self, id: int) -> ContactRoleType:
|
||||
return models.ContactRole.objects.get(pk=id)
|
||||
contact_role_list: List[ContactRoleType] = strawberry_django.field()
|
||||
|
||||
contact = ObjectField(ContactType)
|
||||
contact_list = ObjectListField(ContactType)
|
||||
@strawberry.field
|
||||
def contact_group(self, id: int) -> ContactGroupType:
|
||||
return models.ContactGroup.objects.get(pk=id)
|
||||
contact_group_list: List[ContactGroupType] = strawberry_django.field()
|
||||
|
||||
def resolve_contact_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.Contact.objects.all(), info)
|
||||
|
||||
contact_role = ObjectField(ContactRoleType)
|
||||
contact_role_list = ObjectListField(ContactRoleType)
|
||||
|
||||
def resolve_contact_role_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.ContactRole.objects.all(), info)
|
||||
|
||||
contact_group = ObjectField(ContactGroupType)
|
||||
contact_group_list = ObjectListField(ContactGroupType)
|
||||
|
||||
def resolve_contact_group_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.ContactGroup.objects.all(), info)
|
||||
|
||||
contact_assignment = ObjectField(ContactAssignmentType)
|
||||
contact_assignment_list = ObjectListField(ContactAssignmentType)
|
||||
|
||||
def resolve_contact_assignment_list(root, info, **kwargs):
|
||||
return gql_query_optimizer(models.ContactAssignment.objects.all(), info)
|
||||
@strawberry.field
|
||||
def contact_assignment(self, id: int) -> ContactAssignmentType:
|
||||
return models.ContactAssignment.objects.get(pk=id)
|
||||
contact_assignment_list: List[ContactAssignmentType] = strawberry_django.field()
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import graphene
|
||||
from typing import Annotated, List
|
||||
|
||||
import strawberry
|
||||
import strawberry_django
|
||||
|
||||
from extras.graphql.mixins import CustomFieldsMixin, TagsMixin
|
||||
from tenancy import filtersets, models
|
||||
from netbox.graphql.types import BaseObjectType, OrganizationalObjectType, NetBoxObjectType
|
||||
from tenancy import models
|
||||
from .mixins import ContactAssignmentsMixin
|
||||
from .filters import *
|
||||
|
||||
__all__ = (
|
||||
'ContactAssignmentType',
|
||||
@@ -14,64 +19,169 @@ __all__ = (
|
||||
)
|
||||
|
||||
|
||||
class ContactAssignmentsMixin:
|
||||
assignments = graphene.List('tenancy.graphql.types.ContactAssignmentType')
|
||||
|
||||
def resolve_assignments(self, info):
|
||||
return self.assignments.restrict(info.context.user, 'view')
|
||||
|
||||
|
||||
#
|
||||
# Tenants
|
||||
#
|
||||
|
||||
@strawberry_django.type(
|
||||
models.Tenant,
|
||||
fields='__all__',
|
||||
filters=TenantFilter
|
||||
)
|
||||
class TenantType(NetBoxObjectType):
|
||||
group: Annotated["TenantGroupType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
|
||||
class Meta:
|
||||
model = models.Tenant
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.TenantFilterSet
|
||||
@strawberry_django.field
|
||||
def asns(self) -> List[Annotated["ASNType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.asns.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def circuits(self) -> List[Annotated["CircuitType", strawberry.lazy('circuits.graphql.types')]]:
|
||||
return self.circuits.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def sites(self) -> List[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.sites.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def vlans(self) -> List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.vlans.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def wireless_lans(self) -> List[Annotated["WirelessLANType", strawberry.lazy('wireless.graphql.types')]]:
|
||||
return self.wireless_lans.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def route_targets(self) -> List[Annotated["RouteTargetType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.route_targets.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def locations(self) -> List[Annotated["LocationType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.locations.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def ip_ranges(self) -> List[Annotated["IPRangeType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.ip_ranges.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def rackreservations(self) -> List[Annotated["RackReservationType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.rackreservations.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def racks(self) -> List[Annotated["RackType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.racks.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def vdcs(self) -> List[Annotated["VirtualDeviceContextType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.vdcs.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.prefixes.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def cables(self) -> List[Annotated["CableType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.cables.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def virtual_machines(self) -> List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]:
|
||||
return self.virtual_machines.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def vrfs(self) -> List[Annotated["VRFType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.vrfs.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def asn_ranges(self) -> List[Annotated["ASNRangeType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.asn_ranges.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def wireless_links(self) -> List[Annotated["WirelessLinkType", strawberry.lazy('wireless.graphql.types')]]:
|
||||
return self.wireless_links.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def aggregates(self) -> List[Annotated["AggregateType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.aggregates.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def power_feeds(self) -> List[Annotated["PowerFeedType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.power_feeds.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def devices(self) -> List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]:
|
||||
return self.devices.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def tunnels(self) -> List[Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]]:
|
||||
return self.tunnels.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]:
|
||||
return self.ip_addresses.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def clusters(self) -> List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]:
|
||||
return self.clusters.all()
|
||||
|
||||
@strawberry_django.field
|
||||
def l2vpns(self) -> List[Annotated["L2VPNType", strawberry.lazy('vpn.graphql.types')]]:
|
||||
return self.l2vpns.all()
|
||||
|
||||
|
||||
@strawberry_django.type(
|
||||
models.TenantGroup,
|
||||
fields='__all__',
|
||||
filters=TenantGroupFilter
|
||||
)
|
||||
class TenantGroupType(OrganizationalObjectType):
|
||||
parent: Annotated["TenantGroupType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
|
||||
class Meta:
|
||||
model = models.TenantGroup
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.TenantGroupFilterSet
|
||||
@strawberry_django.field
|
||||
def tenants(self) -> List[TenantType]:
|
||||
return self.tenants.all()
|
||||
|
||||
|
||||
#
|
||||
# Contacts
|
||||
#
|
||||
|
||||
@strawberry_django.type(
|
||||
models.Contact,
|
||||
fields='__all__',
|
||||
filters=ContactFilter
|
||||
)
|
||||
class ContactType(ContactAssignmentsMixin, NetBoxObjectType):
|
||||
|
||||
class Meta:
|
||||
model = models.Contact
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.ContactFilterSet
|
||||
group: Annotated["ContactGroupType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
|
||||
|
||||
@strawberry_django.type(
|
||||
models.ContactRole,
|
||||
fields='__all__',
|
||||
filters=ContactRoleFilter
|
||||
)
|
||||
class ContactRoleType(ContactAssignmentsMixin, OrganizationalObjectType):
|
||||
|
||||
class Meta:
|
||||
model = models.ContactRole
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.ContactRoleFilterSet
|
||||
pass
|
||||
|
||||
|
||||
@strawberry_django.type(
|
||||
models.ContactGroup,
|
||||
fields='__all__',
|
||||
filters=ContactGroupFilter
|
||||
)
|
||||
class ContactGroupType(OrganizationalObjectType):
|
||||
parent: Annotated["ContactGroupType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
|
||||
class Meta:
|
||||
model = models.ContactGroup
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.ContactGroupFilterSet
|
||||
@strawberry_django.field
|
||||
def contacts(self) -> List[ContactType]:
|
||||
return self.contacts.all()
|
||||
|
||||
|
||||
@strawberry_django.type(
|
||||
models.ContactAssignment,
|
||||
fields='__all__',
|
||||
filters=ContactAssignmentFilter
|
||||
)
|
||||
class ContactAssignmentType(CustomFieldsMixin, TagsMixin, BaseObjectType):
|
||||
|
||||
class Meta:
|
||||
model = models.ContactAssignment
|
||||
fields = '__all__'
|
||||
filterset_class = filtersets.ContactAssignmentFilterSet
|
||||
object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
|
||||
contact: Annotated["ContactType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
role: Annotated["ContactRoleType", strawberry.lazy('tenancy.graphql.types')] | None
|
||||
|
||||
Reference in New Issue
Block a user