Django Strawberry Graphql 错误请求 'str' 对象没有属性 'execute_sync'

Django Strawberry Graphql Bad request 'str' object has no attribute 'execute_sync'

所以我有一个简单的 django 模型

class County(models.Model):
    '''County'''
    name = models.CharField(max_length=100, null=False, blank=False)
    class Meta:
        verbose_name_plural = "Counties"
        ordering = ['name']

    def __str__(self):
        return self.name

这是我的 types.py

import strawberry
from strawberry.django import auto
from . import models

@strawberry.django.type(models.County)
class County:
    id: auto
    name: auto

和我的schema.py

import strawberry
from .types import *

@strawberry.type
class Query:
    county: County = strawberry.django.field()
    counties: List[County] = strawberry.django.field()

schema = strawberry.Schema(query=Query)

url.py

from main.schema import schema
from strawberry.django.views import GraphQLView


urlpatterns = [
    path('admin/', admin.site.urls),
    path('graphql', GraphQLView.as_view(schema='schema'), name='graphql'),
]

运行 终端给我:

Unable to parse request body as JSON
Bad Request: /graphql
[08/Jan/2022 03:28:08] "GET /graphql HTTP/1.1" 400 113111

当我进入位于 http://localhost:8000/graphql 的 graphql 终端时,文档资源管理器中没有文档。如果我输入如下查询:

{
  counties{
    id
    name
  }
}

我明白了:

Internal Server Error: /graphql
Traceback (most recent call last):
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users[=18=]\Envs\eagle\lib\site-packages\strawberry\django\views.py", line 148, in dispatch
    result = self.schema.execute_sync(
AttributeError: 'str' object has no attribute 'execute_sync'
[08/Jan/2022 03:29:21] "POST /graphql HTTP/1.1" 500 16621

正如@IainShelvington 在评论中所说,GraphQLView 的参数应该是一个模式对象,您也可以在 docs:

中看到它
 path('graphql', GraphQLView.as_view(schema=schema), name='graphql'),