-
Notifications
You must be signed in to change notification settings - Fork 828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Graphene with Django REST Framework authentication #249
Comments
Thanks for sharing here! |
Adding some additional steps that I had to take when following this integration: class RTGraphQLView(GraphQLView):
def parse_body(self, request):
if type(request) is rest_framework.request.Request:
return request.data
return super().parse_body(request) Graphene was expecting the |
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 |
Hi @jacobh I try to use your code but do not make anything, I can make requests without any authentication header. |
Hi @jacobh. I already worked your code, the truth has saved me an important time. I did not work before because I had the private route after the public route, so I only had to define my private route before the public route and it worked perfectly |
…anted model queryset. Add AuthenticatedGraphQLView on graphene_django_extras.views for use 'permission', 'authorization' and 'throttle' classes based on the DRF settings. Special thanks to [@jacobh](https://github.com/jacobh) for this [comment](graphql-python/graphene#249 (comment))
@travisbloom It works but it's really a hack. |
This implementation worked perfectly for me on my local development server. For some odd reason when I deploy it on AWS it gives me the following error :
|
@alshafai that is because you are calling |
This was really great thank you. I want to include an additional line I added for people using GraphQL/Graphene with the Django OAuth Toolkit:
Notice the Thanks again, this has shown me I need to read deeper into what's going on with the views. Does anyone know any good articles that go into depth? I want to really learn more about these class methods so I can figure something like this out on my own in the future |
Can you explain to me exactly what happening here and why I need to do it? It works for me but I'm not certain as to why. Originally I was also getting the |
Hi guys! I wrote a playground project with all you posted here. The customized view: Its setup: And some integration tests with GraphQL clients:
Thank you! |
We really should get this added to the official docs |
Could anyone tell me where do I get the token returned? I could not retrieve it in my mutation |
You need to create the token first |
Personally, I use https://django-graphql-jwt.domake.io/en/latest/quickstart.html |
@cutamar I used django-graphql-jwt for my login by calling the mutation as following. |
@jstacoder i did generate the token, but when i pass that token as header in my login query, it doesn't work. prompts: could not match signature
|
@Booshra I created a |
@Booshra did you pass it like the graphql-jwt docs say to? ie: so preface the token with: |
@jstacoder yes I did do that.
|
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:
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 thegraphql_token
endpoint.I created this issue in case anyone else has the same question. Also, posted the question and answer to StackOverflow.
The text was updated successfully, but these errors were encountered: