如何将Graphene GraphQL框架与Django REST框架一起使用

如何将Graphene GraphQL框架与Django REST框架一起使用,第1张

如何将Graphene GraphQL框架与Django REST框架一起使用

例如,如果

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

urls.py:

# ...from rest_framework.authentication import TokenAuthenticationfrom rest_framework.permissions import IsAuthenticatedfrom rest_framework.decorators import authentication_classes, permission_classes, api_viewdef 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 viewurlpatterns = [# ...    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
端点,并保留
^graphql
了GraphiQL工具使用的原始端点。

然后,您应该

Authorization
在GraphQL客户端中设置标头并指向
graphql_token
端点。


更新:请参阅此GitHub问题,其中人们提出了替代解决方案和完整的工作示例。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5675114.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存