Hello StackOverflow Community,
I am currently experiencing an issue with a Django project using GraphQL, specifically when attempting to access the GraphQL endpoint through the Krakend API Gateway.
Environment:
Problem:
When I send a request directly to the Django backend's /graphql
endpoint, it works as expected. However, when I attempt to access the same endpoint via the Krakend API Gateway, I receive a 404 Not Found error.
Error Log:
Here is the error message received in the Docker logs:
backend_1 | Not Found: /graphql
backend_1 | [14/Oct/2023 23:32:52] "POST /graphql HTTP/1.1" 404 2961
This indicates that when the request is routed through Krakend to the Django backend, the /graphql
endpoint cannot be found.
Code:
In my urls.py
, I have the /graphql
endpoint defined as:
from django.urls import path, re_path
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True, name='graphql'))),
# ... other paths ...
]
My settings (pertinent to the URLs and middleware) in settings.py
include:
ALLOWED_HOSTS = ['backend', 'frontend', 'localhost', ...]
INSTALLED_APPS = ['graphene_django', ...]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_WHITELIST = ["http://localhost:3000"]
Attempted Solutions:
/graphql
endpoint is defined correctly in Django.Despite the above, the issue persists.
Questions:
/graphql
endpoint is not found?/graphql
endpoint?Any insights or assistance with this issue would be greatly appreciated. Thank you in advance for your time and help!
It looks like your backend is actually receiving the request, so I am wondering what is in Django that makes it throw a 404.
I am not familiar with Django, but in my experience, missing headers make backends have this kind of behavior.
You don't lose anything by trying to add the following to your endpoint configuration to see if it works:
"input_headers": ["*"]
If it does, find which is the header that is needed and avoid having a wildcard, so you end-up having something like this (whatever are the headers needed):
"input_headers": ["Accept", "Content-Type"]