如何将 Postman 与 Graphene 一起使用?

How to use Postman with Graphene?

我正在探索使用 GraphQL-Django 而不是构建大量 REST API 端点。为此,我已经成功安装 运行 'cookbook' 示例应用程序,它是 Graphene Django 软件包的一部分:https://github.com/graphql-python/graphene-django

为了更好地了解 GraphQL 技术的工作原理,我尝试使用 Postman 调用 Graphene 服务器。但是我遇到了 CSRF 错误并尝试了几种方法来解决它,例如:

但到目前为止我运气不好。有使用 Postman 和 Graphene 的权威指南吗?

罗伯特

您可能希望使用 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))),

您可以使用 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的一个包里,是一个使用失眠客户端的例子