Bypass csrf #1186
-
| I have the use case of a mobile client app accessing graphql resource with Apollo Client. However, the client cannot access because of CSRF validation. In web I can get the token from cookie, but there is no cookie in mobile app. How can I solve this issue? | 
Beta Was this translation helpful? Give feedback.
Replies: 19 comments
-
| Hi @nlhkh! You can do something like: from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView
urlpatterns = patterns(
    # ...
    url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
    # ...
) | 
Beta Was this translation helpful? Give feedback.
-
| Thanks @syrusakbary | 
Beta Was this translation helpful? Give feedback.
-
| @syrusakbary I had to do this after deploying my Django app to a live server to get GraphiQL to work on the web. Is that intended or should it find the CSRF token automatically? | 
Beta Was this translation helpful? Give feedback.
-
| @syrusakbary @nlhkh @Vitiell0 I have been trying to make a solution like this but my JS foo isn't strong enough. | 
Beta Was this translation helpful? Give feedback.
-
| @syrusakbary is it possible to disable CSRF for a specific resolver/mutation instead of disabling it for everything? I have a resolver that will be used by an external service (not my app), so it's not possible for it to use a CSRF token. But I don't want to disable CSRF entirely just because of that one resolver. | 
Beta Was this translation helpful? Give feedback.
-
| It would be great if the graphene-django docs could explain how to pass the CSRF token from a javascript client (preferably appollo). If I find out myself I will post it here. My latest attempt looks like this (note: doesn't work):  | 
Beta Was this translation helpful? Give feedback.
-
| @mnieber bruh  | 
Beta Was this translation helpful? Give feedback.
-
| Thanks @japrogramer, it looks a bit different from my attempt, but unfortunately it also doesn't work. | 
Beta Was this translation helpful? Give feedback.
-
| @mnieber  maybe your Cookies.get('csrftoken') call isn't working .. | 
Beta Was this translation helpful? Give feedback.
-
| @mnieber Hmm something like this should work. also try resetting your cache .. just in case. | 
Beta Was this translation helpful? Give feedback.
-
| The Cookies.get call is working (I checked it in this particular bit of code, and also, I'm using it in my jquery setup) | 
Beta Was this translation helpful? Give feedback.
-
| @mnieber  | 
Beta Was this translation helpful? Give feedback.
-
| @dspacejs you could splitting the schema and merging them, one would be csrf exempt and the combined schema wouldnt | 
Beta Was this translation helpful? Give feedback.
-
| In the end I solved my problem by using the graphql-request package. This also solved another problem where authentication headers were not included in the request. I would suggest to include this (or something similar) in the graphene-django docs, it would be a great help to get started with graphene-django.  | 
Beta Was this translation helpful? Give feedback.
-
| Strange I don't use that package at all, and my code works for me. | 
Beta Was this translation helpful? Give feedback.
-
| Many people have encountered the same problem. Usually, the solution for them is to import from 'apollo-client', but that did not work for me. Anyway, I'm happier with a simpler graphql client, so I like this solution. | 
Beta Was this translation helpful? Give feedback.
-
| It seems Graphiql cannot succesfully set correct CSRF token. Is there a forked version that fixes this? | 
Beta Was this translation helpful? Give feedback.
-
| @gotexis You can try the advice from syrusakbary, it should work (#61 (comment)) | 
Beta Was this translation helpful? Give feedback.
-
| @dspacejs You can put the query with that resolver in another scheme and provide another csrf-exempted view with that scheme. | 
Beta Was this translation helpful? Give feedback.
Hi @nlhkh!
You can do something like: