Skip to content

Commit

Permalink
Merge pull request #191 from joshuajcoronado/graphqlv2
Browse files Browse the repository at this point in the history
Add netbox v4 graphql support
  • Loading branch information
matejv authored Oct 4, 2024
2 parents df1a8f0 + 7b08829 commit a3d2f35
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 80 deletions.
80 changes: 0 additions & 80 deletions netbox_inventory/graphql.py

This file was deleted.

17 changes: 17 additions & 0 deletions netbox_inventory/graphql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .schema import (
AssetQuery,
SupplierQuery,
PurchaseQuery,
DeliveryQuery,
InventoryItemTypeQuery,
InventoryItemGroupQuery,
)

schema = [
AssetQuery,
SupplierQuery,
PurchaseQuery,
DeliveryQuery,
InventoryItemTypeQuery,
InventoryItemGroupQuery,
]
50 changes: 50 additions & 0 deletions netbox_inventory/graphql/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import strawberry_django

from netbox.graphql.filter_mixins import autotype_decorator, BaseFilterMixin

from netbox_inventory import filtersets, models

__all__ = (
"AssetFilter",
"SupplierFilter",
"PurchaseFilter",
"DeliveryFilter",
"InventoryItemTypeFilter",
"InventoryItemGroupFilter",
)


@strawberry_django.filter(models.Asset, lookups=True)
@autotype_decorator(filtersets.AssetFilterSet)
class AssetFilter(BaseFilterMixin):
pass


@strawberry_django.filter(models.Supplier, lookups=True)
@autotype_decorator(filtersets.SupplierFilterSet)
class SupplierFilter(BaseFilterMixin):
pass


@strawberry_django.filter(models.Purchase, lookups=True)
@autotype_decorator(filtersets.PurchaseFilterSet)
class PurchaseFilter(BaseFilterMixin):
pass


@strawberry_django.filter(models.Delivery, lookups=True)
@autotype_decorator(filtersets.DeliveryFilterSet)
class DeliveryFilter(BaseFilterMixin):
pass


@strawberry_django.filter(models.InventoryItemType, lookups=True)
@autotype_decorator(filtersets.InventoryItemTypeFilterSet)
class InventoryItemTypeFilter(BaseFilterMixin):
pass


@strawberry_django.filter(models.InventoryItemGroup, lookups=True)
@autotype_decorator(filtersets.InventoryItemGroupFilterSet)
class InventoryItemGroupFilter(BaseFilterMixin):
pass
73 changes: 73 additions & 0 deletions netbox_inventory/graphql/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import strawberry
import strawberry_django

from netbox_inventory.models import (
Asset,
Supplier,
Purchase,
Delivery,
InventoryItemType,
InventoryItemGroup,
)
from .types import (
AssetType,
SupplierType,
PurchaseType,
DeliveryType,
InventoryItemTypeType,
InventoryItemGroupType,
)


@strawberry.type
class AssetQuery:
@strawberry.field
def asset(self, id: int) -> AssetType:
return Asset.objects.get(pk=id)

asset_list: list[AssetType] = strawberry_django.field()


@strawberry.type
class SupplierQuery:
@strawberry.field
def supplier(self, id: int) -> SupplierType:
return Supplier.objects.get(pk=id)

supplier_list: list[SupplierType] = strawberry_django.field()


@strawberry.type
class PurchaseQuery:
@strawberry.field
def purchase(self, id: int) -> PurchaseType:
return Purchase.objects.get(pk=id)

purchase_list: list[PurchaseType] = strawberry_django.field()


@strawberry.type
class DeliveryQuery:
@strawberry.field
def delivery(self, id: int) -> DeliveryType:
return Delivery.objects.get(pk=id)

delivery_list: list[DeliveryType] = strawberry_django.field()


@strawberry.type
class InventoryItemTypeQuery:
@strawberry.field
def inventory_item_type(self, id: int) -> InventoryItemTypeType:
return InventoryItemType.objects.get(pk=id)

inventory_item_type_list: list[InventoryItemTypeType] = strawberry_django.field()


@strawberry.type
class InventoryItemGroupQuery:
@strawberry.field
def inventory_item_group(self, id: int) -> InventoryItemGroupType:
return InventoryItemGroup.objects.get(pk=id)

inventory_item_group_list: list[InventoryItemGroupType] = strawberry_django.field()
127 changes: 127 additions & 0 deletions netbox_inventory/graphql/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import strawberry
import strawberry_django
from typing import Annotated
from extras.graphql.mixins import ContactsMixin, ImageAttachmentsMixin
from netbox.graphql.types import (
NetBoxObjectType,
OrganizationalObjectType,
)
from netbox_inventory.models import (
Asset,
Supplier,
Purchase,
Delivery,
InventoryItemType,
InventoryItemGroup,
)
from .filters import (
AssetFilter,
SupplierFilter,
PurchaseFilter,
DeliveryFilter,
InventoryItemTypeFilter,
InventoryItemGroupFilter,
)


@strawberry_django.type(Asset, fields="__all__", filters=AssetFilter)
class AssetType(ImageAttachmentsMixin, NetBoxObjectType):
device_type: (
Annotated["DeviceTypeType", strawberry.lazy("dcim.graphql.types")] | None
)
module_type: (
Annotated["ModuleTypeType", strawberry.lazy("dcim.graphql.types")] | None
)
inventoryitem_type: (
Annotated[
"InventoryItemTypeType", strawberry.lazy("netbox_inventory.graphql.types")
]
| None
)
tenant: Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] | None
device: Annotated["DeviceType", strawberry.lazy("dcim.graphql.types")] | None
module: Annotated["ModuleType", strawberry.lazy("dcim.graphql.types")] | None
contact: Annotated["ContactType", strawberry.lazy("tenancy.graphql.types")] | None
inventoryitem: (
Annotated["InventoryItemType", strawberry.lazy("dcim.graphql.types")] | None
)
storage_location: (
Annotated["LocationType", strawberry.lazy("dcim.graphql.types")] | None
)
owner: Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] | None
delivery: (
Annotated["DeliveryType", strawberry.lazy("netbox_inventory.graphql.types")]
| None
)
purchase: (
Annotated["PurchaseType", strawberry.lazy("netbox_inventory.graphql.types")]
| None
)


@strawberry_django.type(Supplier, fields="__all__", filters=SupplierFilter)
class SupplierType(ContactsMixin, NetBoxObjectType):
purchases: list[
Annotated["PurchaseType", strawberry.lazy("netbox_inventory.graphql.types")]
]


@strawberry_django.type(Purchase, fields="__all__", filters=PurchaseFilter)
class PurchaseType(NetBoxObjectType):
supplier: Annotated[
"SupplierType", strawberry.lazy("netbox_inventory.graphql.types")
]
assets: list[
Annotated["AssetType", strawberry.lazy("netbox_inventory.graphql.types")]
]
orders: list[
Annotated["DeliveryType", strawberry.lazy("netbox_inventory.graphql.types")]
]


@strawberry_django.type(Delivery, fields="__all__", filters=DeliveryFilter)
class DeliveryType(NetBoxObjectType):
purchase: Annotated[
"PurchaseType", strawberry.lazy("netbox_inventory.graphql.types")
]
receiving_contact: (
Annotated["ContactType", strawberry.lazy("tenancy.graphql.types")] | None
)
assets: list[
Annotated["AssetType", strawberry.lazy("netbox_inventory.graphql.types")]
]


@strawberry_django.type(
InventoryItemType, fields="__all__", filters=InventoryItemTypeFilter
)
class InventoryItemTypeType(ImageAttachmentsMixin, NetBoxObjectType):
manufacturer: Annotated["ManufacturerType", strawberry.lazy("dcim.graphql.types")]
inventoryitem_group: (
Annotated[
"InventoryItemGroupType", strawberry.lazy("netbox_inventory.graphql.types")
]
| None
)


@strawberry_django.type(
InventoryItemGroup, fields="__all__", filters=InventoryItemGroupFilter
)
class InventoryItemGroupType(OrganizationalObjectType):
parent: (
Annotated[
"InventoryItemGroupType", strawberry.lazy("netbox_inventory.graphql.types")
]
| None
)
inventoryitem_types: list[
Annotated[
"InventoryItemTypeType", strawberry.lazy("netbox_inventory.graphql.types")
]
]
children: list[
Annotated[
"InventoryItemGroupType", strawberry.lazy("netbox_inventory.graphql.types")
]
]

0 comments on commit a3d2f35

Please sign in to comment.