Skip to content

Commit 9cc1593

Browse files
committed
Add async schema support to flask
1 parent 162398a commit 9cc1593

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

graphql_server/flask/graphqlview.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import asyncio
12
import copy
23
from collections.abc import MutableMapping
34
from functools import partial
45
from typing import List
56

6-
from flask import Response, render_template_string, request
7+
from flask import Response, current_app, render_template_string, request
78
from flask.views import View
89
from graphql import specified_rules
910
from graphql.error import GraphQLError
11+
from graphql.pyutils import is_awaitable
1012
from graphql.type.schema import GraphQLSchema
1113

1214
from graphql_server import (
@@ -39,6 +41,7 @@ class GraphQLView(View):
3941
validation_rules = None
4042
execution_context_class = None
4143
batch = False
44+
enable_async = False
4245
subscriptions = None
4346
headers = None
4447
default_query = None
@@ -105,12 +108,29 @@ def dispatch_request(self):
105108
batch_enabled=self.batch,
106109
catch=catch,
107110
# Execute options
111+
run_sync=not self.enable_async,
108112
root_value=self.get_root_value(),
109113
context_value=self.get_context(),
110114
middleware=self.get_middleware(),
111115
validation_rules=self.get_validation_rules(),
112116
execution_context_class=self.get_execution_context_class(),
113117
)
118+
119+
async def get_async_execution_results():
120+
return await asyncio.gather(
121+
*(
122+
ex
123+
if ex is not None and is_awaitable(ex)
124+
else asyncio.coroutine(lambda: ex)()
125+
for ex in execution_results
126+
)
127+
)
128+
129+
if self.enable_async:
130+
execution_results = current_app.ensure_sync(
131+
get_async_execution_results
132+
)()
133+
114134
result, status_code = encode_execution_results(
115135
execution_results,
116136
is_batch=isinstance(data, list),

0 commit comments

Comments
 (0)