mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-12 09:30:35 +03:00
Merge pull request #506 from ccsv/patch-1
Update authorization docs to Graphene 2.0
This commit is contained in:
commit
f6dba3942c
|
@ -20,7 +20,7 @@ Let's use a simple example model.
|
||||||
Limiting Field Access
|
Limiting Field Access
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
This is easy, simply use the ``only_fields`` meta attribute.
|
To limit fields in a GraphQL query simply use the ``only_fields`` meta attribute.
|
||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
|
@ -63,8 +63,9 @@ define a resolve method for that field and return the desired queryset.
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
all_posts = DjangoFilterConnectionField(PostNode)
|
all_posts = DjangoFilterConnectionField(PostNode)
|
||||||
|
|
||||||
def resolve_all_posts(self, args, info):
|
def resolve_all_posts(self, info):
|
||||||
return Post.objects.filter(published=True)
|
return Post.objects.filter(published=True)
|
||||||
|
|
||||||
|
|
||||||
User-based Queryset Filtering
|
User-based Queryset Filtering
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
@ -95,7 +96,7 @@ schema is simple.
|
||||||
|
|
||||||
result = schema.execute(query, context_value=request)
|
result = schema.execute(query, context_value=request)
|
||||||
|
|
||||||
Filtering ID-based node access
|
Filtering ID-based Node Access
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
In order to add authorization to id-based node access, we need to add a
|
In order to add authorization to id-based node access, we need to add a
|
||||||
|
@ -113,22 +114,24 @@ method to your ``DjangoObjectType``.
|
||||||
interfaces = (relay.Node, )
|
interfaces = (relay.Node, )
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_node(cls, id, context, info):
|
def get_node(cls, id, info):
|
||||||
try:
|
try:
|
||||||
post = cls._meta.model.objects.get(id=id)
|
post = cls._meta.model.objects.get(id=id)
|
||||||
except cls._meta.model.DoesNotExist:
|
except cls._meta.model.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if post.published or context.user == post.owner:
|
if post.published or info.context.user == post.owner:
|
||||||
return post
|
return post
|
||||||
return None
|
return None
|
||||||
|
|
||||||
Adding login required
|
|
||||||
|
Adding Login Required
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
If you want to use the standard Django LoginRequiredMixin_ you can create your own view, which includes the ``LoginRequiredMixin`` and subclasses the ``GraphQLView``:
|
To restrict users from accessing the GraphQL API page the standard Django LoginRequiredMixin_ can be used to create your own standard Django Class Based View, which includes the ``LoginRequiredMixin`` and subclasses the ``GraphQLView``.:
|
||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
#views.py
|
||||||
|
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from graphene_django.views import GraphQLView
|
from graphene_django.views import GraphQLView
|
||||||
|
@ -137,7 +140,9 @@ If you want to use the standard Django LoginRequiredMixin_ you can create your o
|
||||||
class PrivateGraphQLView(LoginRequiredMixin, GraphQLView):
|
class PrivateGraphQLView(LoginRequiredMixin, GraphQLView):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
After this, you can use the new ``PrivateGraphQLView`` in ``urls.py``:
|
After this, you can use the new ``PrivateGraphQLView`` in the project's URL Configuration file ``url.py``:
|
||||||
|
|
||||||
|
For Django 1.9 and below:
|
||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
|
@ -146,4 +151,13 @@ After this, you can use the new ``PrivateGraphQLView`` in ``urls.py``:
|
||||||
url(r'^graphql', PrivateGraphQLView.as_view(graphiql=True, schema=schema)),
|
url(r'^graphql', PrivateGraphQLView.as_view(graphiql=True, schema=schema)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
For Django 2.0 and above:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
# some other urls
|
||||||
|
path('graphql', PrivateGraphQLView.as_view(graphiql=True, schema=schema)),
|
||||||
|
]
|
||||||
|
|
||||||
.. _LoginRequiredMixin: https://docs.djangoproject.com/en/1.10/topics/auth/default/#the-loginrequired-mixin
|
.. _LoginRequiredMixin: https://docs.djangoproject.com/en/1.10/topics/auth/default/#the-loginrequired-mixin
|
||||||
|
|
Loading…
Reference in New Issue
Block a user