1.Installation安装

Graphene-Django takes a few seconds to install and set up.

Graphene-Django需要几秒钟的时间来安装和设置。

Requirements 需求

Graphene-Django currently supports the following versions of Django:

Graphene-Django当前支持以下版本的Django:

  • >= Django 1.11

Installation 安装

pip install graphene-django

We strongly recommend pinning against a specific version of Graphene-Django because new versions could introduce breaking changes to your project.

我们强烈建议使用Graphene-Django的特定版本,因为新版本可能会给您的项目带来重大变化

Add graphene_django to the INSTALLED_APPS in the settings.py file of your Django project:

settings.py 文件中将 graphene_django 添加到 INSTALLED_APPS 中:

INSTALLED_APPS = [
    ...
    "django.contrib.staticfiles", # Required for GraphiQL / GraphiQL所需
    "graphene_django"
]

We need to add a graphql URL to the urls.py of your Django project:

我们需要添加 graphql 的URL到 你的Django项目的urls.py 文件中:

For Django 1.11:

from django.conf.urls import url
from graphene_django.views import GraphQLView

urlpatterns = [
    # ...
    url(r"graphql", GraphQLView.as_view(graphiql=True)),
]

For Django 2.0 and above:

from django.urls import path
from graphene_django.views import GraphQLView

urlpatterns = [
    # ...
    path("graphql", GraphQLView.as_view(graphiql=True)),
]

(Change graphiql=True to graphiql=False if you do not want to use the GraphiQL API browser.)

(如果不想使用graphiql API浏览程序,请将graphiql=True更改为graphiql=False。)

Finally, define the schema location for Graphene in the settings.py file of your Django project:

最后,在Django项目的settings.py文件中定义Graphene的模式位置:

GRAPHENE = {
    "SCHEMA": "django_root.schema.schema"
}

Where path.schema.schema is the location of the Schema object in your Django project.

The most basic schema.py looks like this:

在哪里path.schema.schema是Django项目中Schema对象的位置。

最基本的schema.py看起来像这样:

import graphene

class Query(graphene.ObjectType):
    hello = graphene.String(default_value="Hi!")

schema = graphene.Schema(query=Query)

To learn how to extend the schema object for your project, read the basic tutorial.

要了解如何为项目扩展schema对象,请阅读基本教程。

CSRF exempt CSRF豁免

If you have enabled CSRF protection in your Django app you will find that it prevents your API clients from POSTing to the graphql endpoint. You can either update your API client to pass the CSRF token with each request (the Django docs have a guide on how to do that: https://docs.djangoproject.com/en/3.0/ref/csrf/#ajax) or you can exempt your Graphql endpoint from CSRF protection by wrapping the GraphQLView with the csrf_exempt decorator:

如果您在Django应用程序中启用了CSRF protection,您会发现它会阻止您的API客户端发布到graphql端点。您可以更新API客户机,以便在每个请求中传递CSRF令牌(Django文档中有一个关于如何执行此操作的指南: https://docs.djangoproject.com/en/3.0/ref/csrf/#ajax) 或者,您可以使用CSRF_exempt修饰符包装GraphQLView,使Graphql端点免于CSRF保护:

# urls.py

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))),
]
posted @ 2020-09-25 07:02  双葫  阅读(267)  评论(0编辑  收藏  举报