jaydenwindle / graphene-subscriptions

A plug-and-play GraphQL subscription implementation for Graphene + Django built using Django Channels.
MIT License
115 stars 15 forks source link

"error": "Could not connect to websocket endpoint wss://api-such.andsuch.xyz/graphql/. Please check if the endpoint url is correct." #29

Closed Cimmanuel closed 3 years ago

Cimmanuel commented 4 years ago

Hello! I recently deployed a project I'm working on to production. I use this great library and I have GraphQL Playground set up via django-graphql-playground. Everything works fine locally - there are no issues whatsoever. However, when I deployed I get the error below when I hit the Play button in Playground:

{
  "error": "Could not connect to websocket endpoint wss://api-such.andsuch.xyz/graphql/. Please check if the endpoint url is correct."
}

I use Gunicorn with a Uvicorn worker class in production. I even used the Gunicorn command locally to see if the issue could be from there but it works. One thing to note is that the application is dockerized. Could it be from there? I don't think so because it works locally. Here's what my docker-compose file looks like:

version: '3.7'

services:
  nginx:
    container_name: nginx
    image: nginx
    restart: always
    depends_on:
      - web
    volumes:
      - ./web/dev.nginx.template:/etc/nginx/conf.d/dev.nginx.template
      - ./static:/static
      - ./media:/media
    ports:
      - "8080:80"
    networks:
      - SOME_NETWORK
    command: /bin/bash -c "envsubst \"`env | awk -F = '{printf \" $$%s\", $$1}'`\" < /etc/nginx/conf.d/dev.nginx.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"

  web:
    container_name: web
    restart: always
    build: ./web
    networks:
      - SOME_NETWORK
    depends_on:
      - postgres
      - redis
    volumes:
      - ./web:/usr/src/app/
    environment:
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - GRAPHQL_ENDPOINT=https://api-such.andsuch.xyz/graphql/
    command: bash -c /start.sh

  postgres:
    container_name: postgres
    restart: always
    image: postgres:latest
    networks:
      - SOME_NETWORK
    volumes:
      - pgdata:/var/lib/postgresql/data/

  redis:
   container_name: redis
   restart: always
   image: redis:latest
   networks:
    - SOME_NETWORK
   ports:
     - "6379:6379"
   volumes:
     - redisdata:/data

volumes:
  pgdata:
  redisdata:

networks:
  SOME_NETWORK:
    name: SOME_NETWORK
    driver: bridge

settings.py

...
...
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [(os.getenv('REDIS_HOST', 'redis'), os.getenv('REDIS_PORT', 6379))],
        }
    }
}
...
...

urls.py

...
...
from graphql_playground.views import GraphQLPlaygroundView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('playground/', GraphQLPlaygroundView.as_view(
        endpoint=os.getenv('GRAPHQL_ENDPOINT'))),
]
...

What could be wrong? I'm outta ideas. Thanks!

mohammed-sehweil commented 4 years ago

Did u solve the issue? @Cimmanuel

Cimmanuel commented 3 years ago

After going through lots of articles, I discovered this one, and this section made me realize the only thing that was wrong was the fact that I didn't expose port 8000 internally to other docker services. In my docker-compose file, the web service was supposed to have...

...
...
expose:
    - 8000
...
...

... in it. I added that and it was solved.

Cimmanuel commented 3 years ago

After going through lots of articles, I discovered this one, and this section made me realize the only thing that was wrong was the fact that I didn't expose port 8000 internally to other docker services. In my docker-compose file, the web service was supposed to have...

...
...
expose:
    - 8000
...
...

... in it. I added that and it was solved.

@mohammed-sehweil check this out!