Skip to content

Commit 1abee56

Browse files
wave: run graphql asynchronously on flask server
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
1 parent a95e12f commit 1abee56

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Diff for: graphql_server/flask/graphqlview.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import asyncio
12
import copy
23
from collections.abc import MutableMapping
34
from functools import partial
45
from typing import List
56

67
from flask import Response, render_template_string, request
78
from flask.views import View
8-
from graphql import specified_rules
9+
from graphql import pyutils, specified_rules
910
from graphql.error import GraphQLError
1011
from graphql.type.schema import GraphQLSchema
1112

@@ -115,9 +116,23 @@ def dispatch_request(self):
115116
middleware=self.get_middleware(),
116117
validation_rules=self.get_validation_rules(),
117118
execution_context_class=self.get_execution_context_class(),
119+
run_sync=False,
118120
)
121+
122+
# This is (almost) copied from graphql_server.aiohttp.GraphQLView
123+
# It is a bit weird as it originally calls await in a loop which
124+
# a bit breaks the gains from doing operations asynchronously...
125+
# But maybe it is required for correctness to execute those
126+
# operations like that, so leaving it.
127+
exec_res = [
128+
ex
129+
if ex is None or not pyutils.is_awaitable(ex)
130+
else asyncio.run(ex)
131+
for ex in execution_results
132+
]
133+
119134
result, status_code = encode_execution_results(
120-
execution_results,
135+
exec_res,
121136
is_batch=isinstance(data, list),
122137
format_error=self.format_error,
123138
encode=partial(self.encode, pretty=pretty), # noqa

0 commit comments

Comments
 (0)