CORS Issue in FastApi + Graphene: No Response Beyond GQL Playground

43 Views Asked by At

CORS Issue in Semi-Production Environment with FastApi + Graphene

Problem Description:

I'm facing an issue with configuring CORS on a FastApi server using Graphene (Starlette). In a semi-production environment, when the server is hosted by uvicorn/gunicorn and accessible from the entire local network (using the --host 0.0.0.0 flag), I don't receive any response other than a CORS error. Even Postman (from the local network via VPN) is unable to fetch the schema from the server.

Previous Attempts to Solve the Issue:

  1. Setting allow_orgins to "*" - no effect, and according to the documentation, this option prohibits sending credentials, tokens, etc.
  2. Setting allow_origins to the computer's address with Postman - no effect.
  3. Setting allow_origins to localhost or 127.0.0.1 - no effect.
  4. Setting allow_origins to http://localhost or http://127.0.0.1 - no effect.
  5. Setting allow_origins to http://localhost:80 or http://127.0.0.1:80 - no effect.

Specifications:

  • Python 3.11

  • Ubuntu 23.04 Server

  • Hosting by uvicorn and gunicorn

  • Framework: FastAPI

  • Added middleware for CORS handling:

    app.add_middleware(
        CORSMiddleware,
        allow_origins=["http://localhost:5173"],  # Specify the exact source
        allow_credentials=True,
        allow_methods=["*", "OPTIONS", "GET", "POST"],
        allow_headers=["*"],
        expose_headers=["*"],
    )
    
  • GraphQL schema available at /graphql.

Additional Information:

  • Frontend is on the same server as the backend.
  • In Postman and the frontend, I provide the server's local IP:8000.
  • Port 8000 is open in ufw, and the backend is running on it.

I kindly request assistance in resolving this CORS issue. Thank you in advance for any suggestions and guidance!

0

There are 0 best solutions below