-
Hi everyone. I have a question. I've created Flask app with ariadne to test GraphQL. Everything works fine except Subscriptions. When I run app and setn subscription request I've got in response "error": "Could not connect to websocket endpoint ws://127.0.0.1:5000/graphql. Please check if the endpoint url is correct." like on the screen below. My schema looks like this schema {
query: Query
mutation: Mutation
subscription: Subscription
}
...
type Query {
...
}
type Mutation {
...
}
type Subscription {
subscribeActors: Actor!
} And part of source code: web = Blueprint('gql', __name__)
config = AppConfig.read_config()
type_defs = load_schema_from_path(config.schema_path)
schema = make_executable_schema(type_defs, snake_case_fallback_resolvers, *types)
@web.route('/graphql', methods=['GET'])
def graphql_playground():
return PLAYGROUND_HTML, 200
@web.route('/graphql', methods=['POST'])
def graphql_server():
data = request.get_json()
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=config.debug,
)
return json.dumps(result), 200 if success else 400 Resolvers @convert_kwargs_to_snake_case
async def actors_source() -> AsyncGenerator[Dict[str, Any], None]:
while True:
actor = await queue.get()
queue.task_done()
yield actor
@convert_kwargs_to_snake_case
async def actor_field(actor, info) -> Dict[str, Any]:
del info
return actor
ResolverRegistry.register_subscription_resolver(
SubscriptionResolverRecord('subscribeActors', actors_source, actor_field)
) Registering types ...
subscription = SubscriptionType()
for record in ResolverRegistry.get_subscription_resolvers():
subscription.set_field(record.name, record.field)
subscription.set_source(record.name, record.source)
...
types = (
mutation,
query,
subscription,
search_result,
) and creating flask app ...
app = Flask(config.app_name)
CORS(app)
app.register_blueprint(web)
app.run(
host=config.host,
port=config.port,
debug=config.debug,
) I saw few examples/tutorial with subscriptions (including this one https://ariadnegraphql.org/docs/0.5.0/subscriptions) but none of them was working with Flask. Any help will be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Key take-away from subscriptions documentation is in big yellow box at its beginning:
Flask is synchronous and WSGI based, which means it won't support subscriptions. |
Beta Was this translation helpful? Give feedback.
Key take-away from subscriptions documentation is in big yellow box at its beginning:
Flask is synchronous and WSGI based, which means it won't support subscriptions.
graphql_sync()
doesn't support them, and so does@web.route
because subscriptions are made over the websocket protocol, which is different from regular HTTP.