我正在探索使用GraphQL-Django而不是构建大量REST API端点。为此,我已成功安装并正在运行'cookbook'示例应用程序,这是Graphene Django软件包的一部分:https://github.com/graphql-python/graphene-django
为了更好地理解GraphQL技术的工作原理,我试图用Postman调用Graphene服务器。但是我收到了CSRF错误,并尝试了几个方法来解决它,例如: Django returns 403 error on POST request with Fetch
但到目前为止,我没有运气。是否有使用邮差与石墨烯的权威指南?
罗伯特
答案 0 :(得分:2)
你可能想要使用graphiql而不是postman。但是,如果您遇到CSRF问题(并且希望URL免除CSRF ......请认真考虑),您可以将视图包装在csrf免除中。在urls.py
from django.views.decorators.csrf import csrf_exempt
url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True, schema=schema))),
答案 1 :(得分:0)
您可以使用insomnia代替邮递员。与graphql一起很棒。
但是正如@styryx回答的那样,您应该使用csrf_exempt:
from django.urls import path
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView
urlpatterns = [
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
在这个tutorial的我的包裹中,是一个使用失眠客户端的示例