GraphQL-запрос, сделанный со страницы React к Django App, заблокирован в ответе

В настоящее время я работаю над индивидуальным проектом, который создает GraphQL запрос в приложении react и отправляет его в приложение Python django для получения ответа.

Проблема в том, что когда я запрашиваю GraphQL запрос со страницы react, я получаю такой ответ в console.log :

Access to fetch at 'http://localhost:8000/graphql/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Вот мои файлы settings.py, которые содержат приложения :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'graphene_django',
    'myapp',
    'corsheaders'
]

CORS_ORIGIN_WHITELIST = [
    "http://localhost:3000",#React page
    "http://127.0.0.1:3000" #React page with an IP address
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

ROOT_URLCONF = 'backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'backend.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR / 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

GRAPHENE = {
    'SCHEMA': 'myapp.schema.schema'
}

Когда я проверяю запрос на тестовой странице graphql в Django, он дает мне ответ После нескольких дней исследований в сети я не знаю, что делать.

Вот мой файл docker-compose.yaml:

version: "3.9"
services:
  # Frontend and backend
  myapp_backend:
    container_name: myapp_backend
    command: >
      sh -c "make make_migration ;
             make migrate ;
             make deploy"
    build: ./backend
    tty: true
    depends_on:
      - myapp_database
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=_
      - POSTGRES_USER=_
      - POSTGRES_PASSWORD=_

  myapp_frontend:
    container_name: myapp_frontend
    build: ./frontend
    command: npm start
    ports: 
      - "3000:3000"
    environment:
      - CI=true
  myapp_database:
    image: postgres
    container_name: myapp_database
    environment:
      - POSTGRES_USER=_
      - POSTGRES_PASSWORD=_
    volumes:
      - ./data/db:/var/lib/postgresql/data
Вернуться на верх