Skip to content

Commit 6f72b86

Browse files
committed
First iteration of async execution
1 parent 0d4f952 commit 6f72b86

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

graphql/error/format_error.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
from .base import GraphQLError
2+
3+
14
def format_error(error):
25
formatted_error = {
3-
'message': error.message,
6+
'message': str(error),
47
}
5-
if error.locations is not None:
6-
formatted_error['locations'] = [
7-
{'line': loc.line, 'column': loc.column}
8-
for loc in error.locations
9-
]
8+
if isinstance(error, GraphQLError):
9+
if error.locations is not None:
10+
formatted_error['locations'] = [
11+
{'line': loc.line, 'column': loc.column}
12+
for loc in error.locations
13+
]
1014

1115
return formatted_error

graphql/execution/executor.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424

2525
def execute(schema, document_ast, root_value=None, context_value=None,
26-
variable_values=None, operation_name=None, executor=None,
27-
return_promise=False, middleware=None):
26+
variable_values=None, operation_name=None, executor=None, middleware=None):
2827
assert schema, 'Must provide schema'
2928
assert isinstance(schema, GraphQLSchema), (
3029
'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
@@ -65,11 +64,7 @@ def on_resolve(data):
6564
return ExecutionResult(data=data)
6665
return ExecutionResult(data=data, errors=context.errors)
6766

68-
promise = Promise.resolve(None).then(executor).catch(on_rejected).then(on_resolve)
69-
if return_promise:
70-
return promise
71-
context.executor.wait_until_finished()
72-
return promise.get()
67+
return Promise.resolve(None).then(executor).catch(on_rejected).then(on_resolve)
7368

7469

7570
def execute_operation(exe_context, operation, root_value):
@@ -122,7 +117,8 @@ def execute_fields(exe_context, parent_type, source_value, fields):
122117
final_results = OrderedDict()
123118

124119
for response_name, field_asts in fields.items():
125-
result = resolve_field(exe_context, parent_type, source_value, field_asts)
120+
result = resolve_field(exe_context, parent_type,
121+
source_value, field_asts)
126122
if result is Undefined:
127123
continue
128124

@@ -175,7 +171,8 @@ def resolve_field(exe_context, parent_type, source, field_asts):
175171
)
176172

177173
executor = exe_context.executor
178-
result = resolve_or_error(resolve_fn_middleware, source, info, args, executor)
174+
result = resolve_or_error(resolve_fn_middleware,
175+
source, info, args, executor)
179176

180177
return complete_value_catching_error(
181178
exe_context,
@@ -206,7 +203,8 @@ def complete_value_catching_error(exe_context, return_type, field_asts, info, re
206203
# Otherwise, error protection is applied, logging the error and
207204
# resolving a null value for this field if one is encountered.
208205
try:
209-
completed = complete_value(exe_context, return_type, field_asts, info, result)
206+
completed = complete_value(
207+
exe_context, return_type, field_asts, info, result)
210208
if is_thenable(completed):
211209
def handle_error(error):
212210
traceback = completed._traceback
@@ -241,7 +239,8 @@ def complete_value(exe_context, return_type, field_asts, info, result):
241239
Otherwise, the field type expects a sub-selection set, and will complete the value by evaluating all
242240
sub-selections.
243241
"""
244-
# If field type is NonNull, complete for inner type, and throw field error if result is null.
242+
# If field type is NonNull, complete for inner type, and throw field error
243+
# if result is null.
245244

246245
if is_thenable(result):
247246
return Promise.resolve(result).then(
@@ -252,7 +251,8 @@ def complete_value(exe_context, return_type, field_asts, info, result):
252251
info,
253252
resolved
254253
),
255-
lambda error: Promise.rejected(GraphQLLocatedError(field_asts, original_error=error))
254+
lambda error: Promise.rejected(
255+
GraphQLLocatedError(field_asts, original_error=error))
256256
)
257257

258258
# print return_type, type(result)
@@ -270,7 +270,8 @@ def complete_value(exe_context, return_type, field_asts, info, result):
270270
if isinstance(return_type, GraphQLList):
271271
return complete_list_value(exe_context, return_type, field_asts, info, result)
272272

273-
# If field type is Scalar or Enum, serialize to a valid value, returning null if coercion is not possible.
273+
# If field type is Scalar or Enum, serialize to a valid value, returning
274+
# null if coercion is not possible.
274275
if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)):
275276
return complete_leaf_value(return_type, result)
276277

@@ -280,7 +281,8 @@ def complete_value(exe_context, return_type, field_asts, info, result):
280281
if isinstance(return_type, GraphQLObjectType):
281282
return complete_object_value(exe_context, return_type, field_asts, info, result)
282283

283-
assert False, u'Cannot complete value of unexpected type "{}".'.format(return_type)
284+
assert False, u'Cannot complete value of unexpected type "{}".'.format(
285+
return_type)
284286

285287

286288
def complete_list_value(exe_context, return_type, field_asts, info, result):
@@ -295,7 +297,8 @@ def complete_list_value(exe_context, return_type, field_asts, info, result):
295297
completed_results = []
296298
contains_promise = False
297299
for item in result:
298-
completed_item = complete_value_catching_error(exe_context, item_type, field_asts, info, item)
300+
completed_item = complete_value_catching_error(
301+
exe_context, item_type, field_asts, info, item)
299302
if not contains_promise and is_thenable(completed_item):
300303
contains_promise = True
301304

@@ -326,7 +329,8 @@ def complete_abstract_value(exe_context, return_type, field_asts, info, result):
326329
if return_type.resolve_type:
327330
runtime_type = return_type.resolve_type(result, info)
328331
else:
329-
runtime_type = get_default_resolve_type_fn(result, info, return_type)
332+
runtime_type = get_default_resolve_type_fn(
333+
result, info, return_type)
330334

331335
if isinstance(runtime_type, string_types):
332336
runtime_type = info.schema.get_type(runtime_type)
@@ -340,13 +344,14 @@ def complete_abstract_value(exe_context, return_type, field_asts, info, result):
340344
info.field_name,
341345
result,
342346
runtime_type,
343-
),
347+
),
344348
field_asts
345349
)
346350

347351
if not exe_context.schema.is_possible_type(return_type, runtime_type):
348352
raise GraphQLError(
349-
u'Runtime Object type "{}" is not a possible type for "{}".'.format(runtime_type, return_type),
353+
u'Runtime Object type "{}" is not a possible type for "{}".'.format(
354+
runtime_type, return_type),
350355
field_asts
351356
)
352357

@@ -366,7 +371,8 @@ def complete_object_value(exe_context, return_type, field_asts, info, result):
366371
"""
367372
if return_type.is_type_of and not return_type.is_type_of(result, info):
368373
raise GraphQLError(
369-
u'Expected value of type "{}" but got: {}.'.format(return_type, type(result).__name__),
374+
u'Expected value of type "{}" but got: {}.'.format(
375+
return_type, type(result).__name__),
370376
field_asts
371377
)
372378

@@ -384,7 +390,8 @@ def complete_nonnull_value(exe_context, return_type, field_asts, info, result):
384390
)
385391
if completed is None:
386392
raise GraphQLError(
387-
'Cannot return null for non-nullable field {}.{}.'.format(info.parent_type, info.field_name),
393+
'Cannot return null for non-nullable field {}.{}.'.format(
394+
info.parent_type, info.field_name),
388395
field_asts
389396
)
390397

graphql/graphql.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .language.source import Source
55
from .validation import validate
66

7+
from promise import promisify
78

89
# This is the primary entry point function for fulfilling GraphQL operations
910
# by parsing, validating, and executing a GraphQL document along side a
@@ -27,9 +28,12 @@
2728
# The name of the operation to use if requestString contains multiple
2829
# possible operations. Can be omitted if requestString contains only
2930
# one operation.
31+
32+
33+
@promisify
3034
def graphql(schema, request_string='', root_value=None, context_value=None,
3135
variable_values=None, operation_name=None, executor=None,
32-
return_promise=False, middleware=None):
36+
middleware=None):
3337
try:
3438
if isinstance(request_string, Document):
3539
ast = request_string
@@ -50,7 +54,6 @@ def graphql(schema, request_string='', root_value=None, context_value=None,
5054
operation_name=operation_name,
5155
variable_values=variable_values or {},
5256
executor=executor,
53-
return_promise=return_promise,
5457
middleware=middleware,
5558
)
5659
except Exception as e:

0 commit comments

Comments
 (0)