I'm working on a project using Django as backend, Vue as frontend, and trying to implement Apollo/Graphene/GraphQL as data transfer layer. Most of it works, but I don't get my head around the CORS/CSRF settings.
(Had really much research here. here, and here and here.
Does anyone know how to solve securing the graphql/graphene API via a CSRF token? On the django log terminal, I get:
Forbidden (CSRF token missing or incorrect.): /graphql/
...while on the Vue/Js Console I see
Cross-Origin Request Blocked: The Same Origin Policy disallows
reading the remote resource at http://localhost:8080/sockjs-node/
info?t=1558447812102.
You can see (and checkout, it's open source) this project here.
http://localhost:8000, http://localhost:8000/admin and http://localhost:8000/ work nicely. The query query{menuItems{id, title, slug, disabled}}
works well in graphiql.
settings.py:
INSTALLED_APPS = [
# ...
'corsheaders',
'rest_framework',
'webpack_loader',
'graphene_django',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', # new
# ...
]
CORS_ORIGIN_ALLOW_ALL = DEBUG # (=True)
The problem is here:
* yarn serve
is running on http://localhost:8080
* ./manage.py runserver
is running on http://localhost:8000, and proxies through webpack the Vue frontend dev server.
vue.config.js:
module.exports = {
// The base URL your application bundle will be deployed at
publicPath: 'http://localhost:8080',
// ...
chainWebpack: config => {
// ...
config.devServer
.public('http://localhost:8080')
// ...
vue-apollo.js:
const httpEndpoint = process.env.VUE_APP_GRAPHQL_HTTP || 'http://localhost:8000/graphql/'
EDIT: If I wrap the graphql/
api urlpath with csrf_exempt
, it works:
urlpatterns = [ # ...
path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True, schema=schema))),
]
But this is a BadIdea(TM) securitywise. How can I get that token into the frontend using Vue with Django and webpack_loader?