How to use Graphene with Django REST Framework authentication
See original GitHub issueI got some REST API endpoints in Django and I wanted to use the same authentication for Graphene. The documentation does not provides any guidance.
I have authentication_classes = (TokenAuthentication,)
in my API views. This was my solution:
urls.py:
# ...
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes, permission_classes, api_view
def graphql_token_view():
view = GraphQLView.as_view(schema=schema)
view = permission_classes((IsAuthenticated,))(view)
view = authentication_classes((TokenAuthentication,))(view)
view = api_view(['GET', 'POST'])(view)
return view
urlpatterns = [
# ...
url(r'^graphql_token', graphql_token_view()),
url(r'^graphql', csrf_exempt(GraphQLView.as_view(schema=schema))),
url(r'^graphiql', include('django_graphiql.urls')),
# ...
Note that I added a new ^graphql_token
endpoint and kept the original ^graphql
which is used by the GraphiQL tool.
Then, I set the Authorization
header in my GraphQL client and point to the graphql_token
endpoint.
I created this issue in case anyone else has the same question. Also, posted the question and answer to StackOverflow.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:33
- Comments:20 (1 by maintainers)
Top Results From Across the Web
How to use Graphene GraphQL framework with Django REST ...
I got some REST API endpoints in Django and I'd like to use the same authentication for Graphene. The documentation does not provide...
Read more >How to Setup Authentication with Django Graphene ... - Hasura
In this walkthrough, create a simple GraphQL authentication service using Django Graphene, mesh it into GraphQL, and create a few sample ...
Read more >Quickstart - Django GraphQL Auth
We are going to use insomnia API client to send request with authorization header. It is really easy to setup, simple follow the...
Read more >Authorization in Django - Graphene-Python
In order to add authorization to id-based node access, we need to add a method to your DjangoObjectType . from graphene_django.types import DjangoObjectType...
Read more >How to use Graphene GraphQL framework with Django REST ...
Django : How to use Graphene GraphQL framework with Django REST Framework authentication [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For my use I rolled the two above examples up into a single view class:
Hi guys! I wrote a playground project with all you posted here.
The customized view:
https://github.com/willianantunes/django-graphql-playground/blob/151b59c29c648ce053937ea70ad07af0d7db74d9/api/graphql/views.py#L10
Its setup:
https://github.com/willianantunes/django-graphql-playground/blob/151b59c29c648ce053937ea70ad07af0d7db74d9/django_graphql_playground/urls.py#L29
And some integration tests with GraphQL clients:
Thank you!