|
| 1 | +import asyncio |
1 | 2 | import copy
|
2 | 3 | from collections.abc import MutableMapping
|
3 | 4 | from functools import partial
|
4 | 5 | from typing import List
|
5 | 6 |
|
6 |
| -from flask import Response, render_template_string, request |
| 7 | +from flask import Response, current_app, render_template_string, request |
7 | 8 | from flask.views import View
|
8 | 9 | from graphql import specified_rules
|
9 | 10 | from graphql.error import GraphQLError
|
| 11 | +from graphql.pyutils import is_awaitable |
10 | 12 | from graphql.type.schema import GraphQLSchema
|
11 | 13 |
|
12 | 14 | from graphql_server import (
|
@@ -39,6 +41,7 @@ class GraphQLView(View):
|
39 | 41 | validation_rules = None
|
40 | 42 | execution_context_class = None
|
41 | 43 | batch = False
|
| 44 | + enable_async = False |
42 | 45 | subscriptions = None
|
43 | 46 | headers = None
|
44 | 47 | default_query = None
|
@@ -105,12 +108,29 @@ def dispatch_request(self):
|
105 | 108 | batch_enabled=self.batch,
|
106 | 109 | catch=catch,
|
107 | 110 | # Execute options
|
| 111 | + run_sync=not self.enable_async, |
108 | 112 | root_value=self.get_root_value(),
|
109 | 113 | context_value=self.get_context(),
|
110 | 114 | middleware=self.get_middleware(),
|
111 | 115 | validation_rules=self.get_validation_rules(),
|
112 | 116 | execution_context_class=self.get_execution_context_class(),
|
113 | 117 | )
|
| 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 | + |
114 | 134 | result, status_code = encode_execution_results(
|
115 | 135 | execution_results,
|
116 | 136 | is_batch=isinstance(data, list),
|
|
0 commit comments