Skip to content

Commit de87573

Browse files
authored
Add information on how to deal with CSRF protection (#838)
1 parent b8a2d59 commit de87573

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

docs/installation.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,26 @@ The most basic ``schema.py`` looks like this:
6666
schema = graphene.Schema(query=Query)
6767
6868
69-
To learn how to extend the schema object for your project, read the basic tutorial.
69+
To learn how to extend the schema object for your project, read the basic tutorial.
70+
71+
CSRF exempt
72+
-----------
73+
74+
If have enabled `CSRF protection <https://docs.djangoproject.com/en/3.0/ref/csrf/>`_ in your Django app
75+
you will find that it prevents your API clients from POSTing to the ``graphql`` endpoint. You can either
76+
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``
77+
decorator:
78+
79+
.. code:: python
80+
81+
# urls.py
82+
83+
from django.urls import path
84+
from django.views.decorators.csrf import csrf_exempt
85+
86+
from graphene_django.views import GraphQLView
87+
88+
urlpatterns = [
89+
# ...
90+
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
91+
]

0 commit comments

Comments
 (0)