djangodockergraphqlapi-gatewaykrakend

Django GraphQL Endpoint Not Found When Requested via Krakend API Gateway


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:

  1. Verified the /graphql endpoint is defined correctly in Django.
  2. Ensured that Docker services are networked properly.
  3. Checked Krakend configurations to ensure correct routing to the Django backend.
  4. Tried direct access to the Django backend endpoint, which works as expected, confirming that the endpoint does exist and function.

Despite the above, the issue persists.

Questions:

  1. How might Krakend be mishandling or misrouting the requests such that the /graphql endpoint is not found?
  2. Could there be a configuration issue within Django that is affecting the routing of requests coming from Krakend but not direct requests?
  3. Are there any additional logging or debugging steps that might illuminate why Krakend cannot access the /graphql endpoint?

Any insights or assistance with this issue would be greatly appreciated. Thank you in advance for your time and help!


Solution

  • 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"]