question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Graphene with Django REST Framework authentication

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:33
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

66reactions
jacobhcommented, May 9, 2017

For my use I rolled the two above examples up into a single view class:

from graphene_django.views import GraphQLView

import rest_framework
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes, permission_classes, api_view
from rest_framework.settings import api_settings


class DRFAuthenticatedGraphQLView(GraphQLView):
    def parse_body(self, request):
        if isinstance(request, rest_framework.request.Request):
            return request.data
        return super(APGraphQLView, self).parse_body(request)

    @classmethod
    def as_view(cls, *args, **kwargs):
        view = super(APGraphQLView, cls).as_view(*args, **kwargs)
        view = permission_classes((IsAuthenticated,))(view)
        view = authentication_classes(api_settings.DEFAULT_AUTHENTICATION_CLASSES)(view)
        view = api_view(['GET', 'POST'])(view)
        return view
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found