Skip to content

Commit 7251d70

Browse files
committed
Use Executor for getting responses
Use an Executor of the same type as configured to get backend responses.This leverages the batching functionality, allowing batched requests to be processed concurrently.
1 parent c781f01 commit 7251d70

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

graphql_server/__init__.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from graphql import get_default_backend
1616
from graphql.error import format_error as default_format_error
1717
from graphql.execution import ExecutionResult
18+
from graphql.execution.executors.sync import SyncExecutor
1819
from graphql.type import GraphQLSchema
20+
from promise import Promise, is_thenable
1921

2022
from .error import HttpQueryError
2123

@@ -120,10 +122,20 @@ def run_http_query(
120122

121123
all_params = [get_graphql_params(entry, extra_data) for entry in data]
122124

123-
results = [
124-
get_response(schema, params, catch_exc, allow_only_query, **execute_options)
125+
executor = execute_options.get('executor')
126+
response_executor = executor if executor else SyncExecutor()
127+
128+
response_promises = [
129+
response_executor.execute(
130+
get_response, schema, params, catch_exc, allow_only_query, **execute_options)
125131
for params in all_params
126132
]
133+
response_executor.wait_until_finished()
134+
135+
results = [
136+
result.get() if is_thenable(result) else result
137+
for result in response_promises
138+
]
127139

128140
return ServerResults(results, all_params)
129141

tests/test_query.py

+31
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,34 @@ def test_batch_allows_post_with_operation_name():
530530
assert as_dicts(results) == [
531531
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
532532
]
533+
534+
535+
def test_get_reponses_using_executor():
536+
class TestExecutor(object):
537+
called = False
538+
waited = False
539+
cleaned = False
540+
541+
def wait_until_finished(self):
542+
TestExecutor.waited = True
543+
544+
def clean(self):
545+
TestExecutor.cleaned = True
546+
547+
def execute(self, fn, *args, **kwargs):
548+
TestExecutor.called = True
549+
return fn(*args, **kwargs)
550+
551+
query = "{test}"
552+
results, params = run_http_query(
553+
schema, "get", {},
554+
dict(query=query),
555+
executor=TestExecutor(),
556+
return_promise=True
557+
)
558+
559+
assert as_dicts(results) == [{"data": {"test": "Hello World"}}]
560+
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
561+
assert TestExecutor.called
562+
assert TestExecutor.waited
563+
assert TestExecutor.cleaned

0 commit comments

Comments
 (0)