Skip to content

Commit b4758b1

Browse files
committed
Added return_promise
1 parent 89e95e6 commit b4758b1

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ after_success:
3333
matrix:
3434
include:
3535
- python: "3.5"
36+
after_install:
37+
- pip install pytest-asyncio
3638
script:
3739
- flake8
3840
- isort --check-only graphql/ -rc

graphql/execution/executor.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020

2121
def execute(schema, document_ast, root_value=None, context_value=None,
22-
variable_values=None, operation_name=None, executor=None):
22+
variable_values=None, operation_name=None, executor=None,
23+
return_promise=False):
2324
assert schema, 'Must provide schema'
2425
assert isinstance(schema, GraphQLSchema), (
2526
'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
@@ -49,9 +50,11 @@ def on_rejected(error):
4950
def on_resolve(data):
5051
return ExecutionResult(data=data, errors=context.errors)
5152

52-
p = Promise(executor).catch(on_rejected).then(on_resolve)
53+
promise = Promise(executor).catch(on_rejected).then(on_resolve)
54+
if return_promise:
55+
return promise
5356
context.executor.wait_until_finished()
54-
return p.get()
57+
return promise.get()
5558

5659

5760
def execute_operation(exe_context, operation, root_value):
@@ -318,15 +321,15 @@ def complete_abstract_value(exe_context, return_type, field_asts, info, result):
318321
runtime_type = get_default_resolve_type_fn(result, exe_context.context_value, info, return_type)
319322

320323
assert isinstance(runtime_type, GraphQLObjectType), (
321-
'Abstract type {} must resolve to an Object type at runtime ' +
322-
'for field {}.{} with value "{}", received "{}".'
323-
).format(
324-
return_type,
325-
info.parent_type,
326-
info.field_name,
327-
result,
328-
runtime_type,
329-
)
324+
'Abstract type {} must resolve to an Object type at runtime ' +
325+
'for field {}.{} with value "{}", received "{}".'
326+
).format(
327+
return_type,
328+
info.parent_type,
329+
info.field_name,
330+
result,
331+
runtime_type,
332+
)
330333

331334
if not exe_context.schema.is_possible_type(return_type, runtime_type):
332335
raise GraphQLError(

graphql/graphql.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
# possible operations. Can be omitted if requestString contains only
2828
# one operation.
2929
def graphql(schema, request_string='', root_value=None, context_value=None,
30-
variable_values=None, operation_name=None, executor=None):
30+
variable_values=None, operation_name=None, executor=None,
31+
return_promise=False):
3132
try:
3233
source = Source(request_string, 'GraphQL request')
3334
ast = parse(source)
@@ -44,7 +45,8 @@ def graphql(schema, request_string='', root_value=None, context_value=None,
4445
context_value,
4546
operation_name=operation_name,
4647
variable_values=variable_values or {},
47-
executor=executor
48+
executor=executor,
49+
return_promise=return_promise
4850
)
4951
except Exception as e:
5052
return ExecutionResult(

tests_py35/core_execution/test_asyncio_executor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ def resolver_3(context, *_):
3939
assert result.data == {'a': 'hey', 'b': 'hey2', 'c': 'hey3'}
4040

4141

42+
@pytest.mark.asyncio
43+
async def test_asyncio_py35_executor_return_promise(event_loop):
44+
ast = parse('query Example { a, b, c }')
45+
46+
async def resolver(context, *_):
47+
await asyncio.sleep(0.001)
48+
return 'hey'
49+
50+
async def resolver_2(context, *_):
51+
await asyncio.sleep(0.003)
52+
return 'hey2'
53+
54+
def resolver_3(context, *_):
55+
return 'hey3'
56+
57+
Type = GraphQLObjectType('Type', {
58+
'a': GraphQLField(GraphQLString, resolver=resolver),
59+
'b': GraphQLField(GraphQLString, resolver=resolver_2),
60+
'c': GraphQLField(GraphQLString, resolver=resolver_3)
61+
})
62+
63+
result = await execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor(loop=event_loop), return_promise=True)
64+
assert not result.errors
65+
assert result.data == {'a': 'hey', 'b': 'hey2', 'c': 'hey3'}
66+
67+
4268
def test_asyncio_py35_executor_with_error():
4369
ast = parse('query Example { a, b }')
4470

0 commit comments

Comments
 (0)