Coverage for netbox/graphql/schema.py: 94%
32 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from collections.abc import Callable
3import strawberry
4from django.conf import settings
5from strawberry.extensions import MaxAliasesLimiter, QueryDepthLimiter, SchemaExtension
6from strawberry.schema.config import StrawberryConfig
7from strawberry_django.optimizer import DjangoOptimizerExtension
9from circuits.graphql.schema import CircuitsQuery
10from core.graphql.schema import CoreQuery
11from dcim.graphql.schema import DCIMQuery
12from extras.graphql.schema import ExtrasQuery
13from ipam.graphql.schema import IPAMQuery
14from netbox.plugins import _load_plugin_graphql_schemas
15from netbox.registry import registry
16from tenancy.graphql.schema import TenancyQuery
17from users.graphql.schema import UsersQuery
18from virtualization.graphql.schema import VirtualizationQuery
19from vpn.graphql.schema import VPNQuery
20from wireless.graphql.schema import WirelessQuery
22from .scalars import BigInt, BigIntScalar
24SchemaExtensionFactory = type[SchemaExtension] | Callable[[], SchemaExtension]
27# Must run before Query is defined, since its bases consume the registered plugin schemas.
28_load_plugin_graphql_schemas()
31@strawberry.type
32class Query(
33 UsersQuery,
34 CircuitsQuery,
35 CoreQuery,
36 DCIMQuery,
37 ExtrasQuery,
38 IPAMQuery,
39 TenancyQuery,
40 VirtualizationQuery,
41 VPNQuery,
42 WirelessQuery,
43 *registry['plugins']['graphql_schemas'], # Append plugin schemas
44):
45 pass
48def get_schema_extensions() -> list[SchemaExtensionFactory]:
49 max_aliases = settings.GRAPHQL_MAX_ALIASES
50 max_depth = settings.GRAPHQL_MAX_QUERY_DEPTH
52 extensions: list[SchemaExtensionFactory] = [
53 lambda: DjangoOptimizerExtension(prefetch_custom_queryset=True),
54 lambda: MaxAliasesLimiter(max_alias_count=max_aliases),
55 ]
56 if max_depth and max_depth > 0: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true
57 extensions.append(lambda: QueryDepthLimiter(max_depth=max_depth))
58 return extensions
61schema = strawberry.Schema(
62 query=Query,
63 config=StrawberryConfig(
64 auto_camel_case=False,
65 scalar_map={
66 BigInt: BigIntScalar,
67 },
68 ),
69 extensions=get_schema_extensions(),
70)