Просмотр исходного кода

9856 base strawberry integration

Arthur 2 лет назад
Родитель
Сommit
3f2c21f005
5 измененных файлов с 23 добавлено и 31 удалено
  1. 5 1
      base_requirements.txt
  2. 13 26
      netbox/netbox/graphql/schema.py
  3. 2 2
      netbox/netbox/settings.py
  4. 2 2
      netbox/netbox/urls.py
  5. 1 0
      requirements.txt

+ 5 - 1
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)

+ 13 - 26
netbox/netbox/graphql/schema.py

@@ -1,31 +1,18 @@
-import graphene
+import strawberry
+from strawberry_django.optimizer import DjangoOptimizerExtension
 
-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
 
+@strawberry.type
+class User:
+    name: str
+    age: int
 
-class Query(
-    UsersQuery,
-    CircuitsQuery,
-    CoreQuery,
-    DCIMQuery,
-    ExtrasQuery,
-    IPAMQuery,
-    TenancyQuery,
-    VirtualizationQuery,
-    WirelessQuery,
-    *registry['plugins']['graphql_schemas'],  # Append plugin schemas
-    graphene.ObjectType
-):
-    pass
 
+@strawberry.type
+class Query:
+    @strawberry.field
+    def user(self) -> User:
+        return User(name="Patrick", age=100)
 
-schema = graphene.Schema(query=Query, auto_camelcase=False)
+
+schema = strawberry.Schema(query=Query)

+ 2 - 2
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',

+ 2 - 2
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/<path:path>', serve, {'document_root': settings.MEDIA_ROOT}),

+ 1 - 0
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