gpt4 book ai didi

python - 如何将 Graphene GraphQL 框架与 Django REST 框架身份验证一起使用

转载 作者:IT老高 更新时间:2023-10-28 21:02:24 27 4
gpt4 key购买 nike

我在 Django 中有一些 REST API 端点,我想使用 the same authentication对于 Graphite 烯。 documentation不提供任何指导。

最佳答案

例如,如果您在 API View 中使用 authentication_classes = (TokenAuthentication,),则可以将端点添加到以这种方式装饰的 GraphQLView:

urls.py:

# ...
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes, permission_classes, api_view

def graphql_token_view():
view = GraphQLView.as_view(schema=schema)
view = permission_classes((IsAuthenticated,))(view)
view = authentication_classes((TokenAuthentication,))(view)
view = api_view(['GET', 'POST'])(view)
return view

urlpatterns = [
# ...
url(r'^graphql_token', graphql_token_view()),
url(r'^graphql', csrf_exempt(GraphQLView.as_view(schema=schema))),
url(r'^graphiql', include('django_graphiql.urls')),
# ...

请注意,我们添加了一个新的 ^graphql_token 端点,并保留了 GraphiQL 工具使用的原始 ^graphql

然后,您应该在 GraphQL 客户端中设置 Authorization header 并指向 graphql_token 端点。


更新:见 this GitHub issue人们提出了替代解决方案和完整的工作示例。

关于python - 如何将 Graphene GraphQL 框架与 Django REST 框架身份验证一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39026831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com