Skip to content

Commit

Permalink
wave: run graphql asynchronously on flask server
Browse files Browse the repository at this point in the history
In GraphQLView.dispatch_request pass run_sync=False to run_http_query and,
if necessary, run the result on async loop.

There is an open PR upstream to handle async natively:
graphql-python#110
  • Loading branch information
lukaspiatkowski committed Sep 14, 2023
1 parent a95e12f commit 0543426
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions graphql_server/flask/graphqlview.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import copy
from collections.abc import MutableMapping
from functools import partial
from typing import List

from flask import Response, render_template_string, request
from flask.views import View
from graphql import specified_rules
from graphql import pyutils, specified_rules
from graphql.error import GraphQLError
from graphql.type.schema import GraphQLSchema

Expand Down Expand Up @@ -115,9 +116,23 @@ def dispatch_request(self):
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
execution_context_class=self.get_execution_context_class(),
run_sync=False,
)

# This is (almost) copied from graphql_server.aiohttp.GraphQLView
# It is a bit weird as it originally calls await in a loop which
# a bit breaks the gains from doing operations asynchronously...
# But maybe it is required for correctness to execute those
# operations like that, so leaving it.
exec_res = [
ex
if ex is None or not pyutils.is_awaitable(ex)
else asyncio.run(ex)
for ex in execution_results
]

result, status_code = encode_execution_results(
execution_results,
exec_res,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty), # noqa
Expand Down

0 comments on commit 0543426

Please sign in to comment.