diff --git a/base_requirements.txt b/base_requirements.txt index 4b75b1313..f66a4c4c2 100644 --- a/base_requirements.txt +++ b/base_requirements.txt @@ -132,8 +132,12 @@ social-auth-core # https://github.com/python-social-auth/social-app-django/blob/master/CHANGELOG.md social-auth-app-django +# Enhanced Strawberry GraphQL integration with Django +# https://github.com/blb-ventures/strawberry-django-plus/blob/main/CHANGELOG.md +strawberry-graphql-django + # SVG image rendering (used for rack elevations) -# hhttps://github.com/mozman/svgwrite/blob/master/NEWS.rst +# https://github.com/mozman/svgwrite/blob/master/NEWS.rst svgwrite # Tabular dataset library (for table-based exports) diff --git a/netbox/netbox/graphql/schema.py b/netbox/netbox/graphql/schema.py index 7224f3c38..8f81efd0d 100644 --- a/netbox/netbox/graphql/schema.py +++ b/netbox/netbox/graphql/schema.py @@ -1,31 +1,18 @@ -import graphene - -from circuits.graphql.schema import CircuitsQuery -from core.graphql.schema import CoreQuery -from dcim.graphql.schema import DCIMQuery -from extras.graphql.schema import ExtrasQuery -from ipam.graphql.schema import IPAMQuery -from netbox.registry import registry -from tenancy.graphql.schema import TenancyQuery -from users.graphql.schema import UsersQuery -from virtualization.graphql.schema import VirtualizationQuery -from wireless.graphql.schema import WirelessQuery +import strawberry +from strawberry_django.optimizer import DjangoOptimizerExtension -class Query( - UsersQuery, - CircuitsQuery, - CoreQuery, - DCIMQuery, - ExtrasQuery, - IPAMQuery, - TenancyQuery, - VirtualizationQuery, - WirelessQuery, - *registry['plugins']['graphql_schemas'], # Append plugin schemas - graphene.ObjectType -): - pass +@strawberry.type +class User: + name: str + age: int -schema = graphene.Schema(query=Query, auto_camelcase=False) +@strawberry.type +class Query: + @strawberry.field + def user(self) -> User: + return User(name="Patrick", age=100) + + +schema = strawberry.Schema(query=Query) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 69ef8f52d..f28449b12 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -361,7 +361,7 @@ INSTALLED_APPS = [ 'django_filters', 'django_tables2', 'django_prometheus', - 'graphene_django', + 'strawberry_django', 'mptt', 'rest_framework', 'social_django', @@ -385,7 +385,7 @@ INSTALLED_APPS = [ # Middleware MIDDLEWARE = [ - 'graphiql_debug_toolbar.middleware.DebugToolbarMiddleware', + "strawberry_django.middlewares.debug_toolbar.DebugToolbarMiddleware", 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/netbox/netbox/urls.py b/netbox/netbox/urls.py index 595a9001f..4ed180535 100644 --- a/netbox/netbox/urls.py +++ b/netbox/netbox/urls.py @@ -9,8 +9,8 @@ from account.views import LoginView, LogoutView from extras.plugins.urls import plugin_admin_patterns, plugin_patterns, plugin_api_patterns from netbox.api.views import APIRootView, StatusView from netbox.graphql.schema import schema -from netbox.graphql.views import GraphQLView from netbox.views import HomeView, StaticMediaFailureView, SearchView, htmx +from strawberry.django.views import GraphQLView from .admin import admin_site _patterns = [ @@ -59,7 +59,7 @@ _patterns = [ path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='api_redocs'), # GraphQL - path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True, schema=schema)), name='graphql'), + path('graphql/', GraphQLView.as_view(schema=schema), name='graphql'), # Serving static media in Django to pipe it through LoginRequiredMiddleware path('media/', serve, {'document_root': settings.MEDIA_ROOT}), diff --git a/requirements.txt b/requirements.txt index 7a2d0f9c8..7580af9cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,6 +30,7 @@ PyYAML==6.0.1 sentry-sdk==1.29.2 social-auth-app-django==5.2.0 social-auth-core[openidconnect]==4.4.2 +strawberry-graphql-django==0.16.0 svgwrite==1.4.3 tablib==3.5.0 tzdata==2023.3