Skip to content

Commit 0143e08

Browse files
committed
Improved execute usage. Added exception logger
1 parent cd336fa commit 0143e08

15 files changed

+69
-41
lines changed

graphql/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def graphql(schema, request='', root=None, args=None, operation_name=None):
3434
)
3535
return execute(
3636
schema,
37-
root or object(),
3837
ast,
39-
operation_name,
40-
args or {},
38+
root or object(),
39+
operation_name=operation_name,
40+
variable_values=args or {},
4141
)
4242
except Exception as e:
4343
return ExecutionResult(

graphql/execution/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
2) fragment "spreads" e.g. "...c"
1919
3) inline fragment "spreads" e.g. "...on Type { a }"
2020
"""
21-
from .execute import execute as _execute
21+
from .executor import execute
2222
from .base import ExecutionResult
2323

2424

25-
def execute(schema, root, ast, operation_name='', args=None):
26-
return _execute(schema, ast, root, variable_values=args, operation_name=operation_name)
27-
2825
__all__ = ['execute', 'ExecutionResult']

graphql/execution/execute.py renamed to graphql/execution/executor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import functools
3+
import logging
34

45
from ..error import GraphQLError
56
from ..pyutils.aplus import Promise, is_thenable, promise_for_dict, promisify
@@ -13,6 +14,9 @@
1314
from .executors.sync import SyncExecutor
1415

1516

17+
logger = logging.getLogger(__name__)
18+
19+
1620
def execute(schema, document_ast, root_value=None, context_value=None,
1721
variable_values=None, operation_name=None, executor=None):
1822
assert schema, 'Must provide schema'
@@ -158,6 +162,9 @@ def resolve_or_error(resolve_fn, source, args, exe_context, info):
158162
# return resolve_fn(source, args, exe_context, info)
159163
return exe_context.executor.execute(resolve_fn, source, args, info)
160164
except Exception as e:
165+
logger.exception("An error occurred while resolving field {}.{}".format(
166+
info.parent_type.name, info.field_name
167+
))
161168
return e
162169

163170

graphql/execution/tests/test_directives.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Data(object):
2020

2121

2222
def execute_test_query(doc):
23-
return execute(schema, Data, parse(doc))
23+
return execute(schema, parse(doc), Data)
2424

2525

2626
def test_basic_query_works():

graphql/execution/tests/test_execute_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, uid, width, height):
110110

111111
# Note: this is intentionally not validating to ensure appropriate
112112
# behavior occurs when executing an invalid query.
113-
result = execute(BlogSchema, None, parse(request))
113+
result = execute(BlogSchema, parse(request))
114114
assert not result.errors
115115
assert result.data == \
116116
{

graphql/execution/tests/test_executor.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def deeper(self):
111111

112112
schema = GraphQLSchema(query=DataType)
113113

114-
result = execute(schema, Data(), ast, 'Example', {'size': 100})
114+
result = execute(schema, ast, Data(), operation_name='Example', variable_values={'size': 100})
115115
assert not result.errors
116116
assert result.data == expected
117117

@@ -142,7 +142,7 @@ def test_merges_parallel_fragments():
142142
})
143143

144144
schema = GraphQLSchema(query=Type)
145-
result = execute(schema, None, ast)
145+
result = execute(schema, ast)
146146
assert not result.errors
147147
assert result.data == \
148148
{
@@ -176,7 +176,7 @@ def resolver(context, *_):
176176
'a': GraphQLField(GraphQLString, resolver=resolver)
177177
})
178178

179-
result = execute(GraphQLSchema(Type), Data(), ast, 'Example', {})
179+
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='Example')
180180
assert not result.errors
181181
assert resolver.got_here
182182

@@ -207,7 +207,7 @@ def resolver(_, args, *_args):
207207
resolver=resolver),
208208
})
209209

210-
result = execute(GraphQLSchema(Type), None, doc_ast, 'Example', {})
210+
result = execute(GraphQLSchema(Type), doc_ast, None, operation_name='Example')
211211
assert not result.errors
212212
assert resolver.got_here
213213

@@ -233,7 +233,7 @@ def error(self):
233233
'error': GraphQLField(GraphQLString),
234234
})
235235

236-
result = execute(GraphQLSchema(Type), Data(), doc_ast)
236+
result = execute(GraphQLSchema(Type), doc_ast, Data())
237237
assert result.data == {'ok': 'ok', 'error': None}
238238
assert len(result.errors) == 1
239239
assert result.errors[0].message == 'Error getting error'
@@ -250,7 +250,7 @@ class Data(object):
250250
Type = GraphQLObjectType('Type', {
251251
'a': GraphQLField(GraphQLString)
252252
})
253-
result = execute(GraphQLSchema(Type), Data(), ast)
253+
result = execute(GraphQLSchema(Type), ast, Data())
254254
assert not result.errors
255255
assert result.data == {'a': 'b'}
256256

@@ -265,7 +265,7 @@ class Data(object):
265265
Type = GraphQLObjectType('Type', {
266266
'a': GraphQLField(GraphQLString)
267267
})
268-
result = execute(GraphQLSchema(Type), Data(), ast)
268+
result = execute(GraphQLSchema(Type), ast, Data())
269269
assert not result.errors
270270
assert result.data == {'a': 'b'}
271271

@@ -281,7 +281,7 @@ class Data(object):
281281
'a': GraphQLField(GraphQLString)
282282
})
283283
with raises(GraphQLError) as excinfo:
284-
execute(GraphQLSchema(Type), Data(), ast)
284+
execute(GraphQLSchema(Type), ast, Data())
285285
assert 'Must provide operation name if query contains multiple operations' in str(excinfo.value)
286286

287287

@@ -302,7 +302,7 @@ class Data(object):
302302
S = GraphQLObjectType('S', {
303303
'a': GraphQLField(GraphQLString)
304304
})
305-
result = execute(GraphQLSchema(Q, M, S), Data(), ast, 'Q')
305+
result = execute(GraphQLSchema(Q, M, S), ast, Data(), operation_name='Q')
306306
assert not result.errors
307307
assert result.data == {'a': 'b'}
308308

@@ -321,7 +321,7 @@ class Data(object):
321321
M = GraphQLObjectType('M', {
322322
'c': GraphQLField(GraphQLString)
323323
})
324-
result = execute(GraphQLSchema(Q, M), Data(), ast, 'M')
324+
result = execute(GraphQLSchema(Q, M), ast, Data(), operation_name='M')
325325
assert not result.errors
326326
assert result.data == {'c': 'd'}
327327

@@ -340,7 +340,7 @@ class Data(object):
340340
S = GraphQLObjectType('S', {
341341
'a': GraphQLField(GraphQLString)
342342
})
343-
result = execute(GraphQLSchema(Q, subscription=S), Data(), ast, 'S')
343+
result = execute(GraphQLSchema(Q, subscription=S), ast, Data(), operation_name='S')
344344
assert not result.errors
345345
assert result.data == {'a': 'b'}
346346

@@ -365,7 +365,7 @@ class Data(object):
365365
Type = GraphQLObjectType('Type', {
366366
'a': GraphQLField(GraphQLString)
367367
})
368-
result = execute(GraphQLSchema(Type), Data(), ast, 'Q')
368+
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='Q')
369369
assert not result.errors
370370
assert result.data == {'a': 'b'}
371371

@@ -379,7 +379,7 @@ def test_does_not_include_illegal_fields_in_output():
379379
M = GraphQLObjectType('M', {
380380
'c': GraphQLField(GraphQLString)
381381
})
382-
result = execute(GraphQLSchema(Q, M), None, ast)
382+
result = execute(GraphQLSchema(Q, M), ast)
383383
assert not result.errors
384384
assert result.data == {}
385385

@@ -403,7 +403,7 @@ def test_does_not_include_arguments_that_were_not_set():
403403
))
404404

405405
ast = parse('{ field(a: true, c: false, e: 0) }')
406-
result = execute(schema, None, ast)
406+
result = execute(schema, ast)
407407
assert result.data == {
408408
'field': '{"a":true,"c":false,"e":0}'
409409
}
@@ -445,7 +445,7 @@ def __init__(self, value):
445445
'specials': [Special('foo'), NotSpecial('bar')]
446446
}
447447

448-
result = execute(schema, value, query)
448+
result = execute(schema, query, value)
449449

450450
assert result.data == {
451451
'specials': [
@@ -474,6 +474,30 @@ def test_fails_to_execute_a_query_containing_a_type_definition():
474474
)
475475

476476
with raises(GraphQLError) as excinfo:
477-
execute(schema, None, query)
477+
execute(schema, query)
478478

479479
assert excinfo.value.message == 'GraphQL cannot execute a request containing a ObjectTypeDefinition.'
480+
481+
482+
def test_exceptions_are_reraised_if_specified(mocker):
483+
484+
logger = mocker.patch('graphql.execution.executor.logger')
485+
486+
query = parse('''
487+
{ foo }
488+
''')
489+
490+
def resolver(*_):
491+
raise Exception("UH OH!")
492+
493+
schema = GraphQLSchema(
494+
GraphQLObjectType(
495+
name='Query',
496+
fields={
497+
'foo': GraphQLField(GraphQLString, resolver=resolver)
498+
}
499+
)
500+
)
501+
502+
execute(schema, query)
503+
logger.exception.assert_called_with("An error occurred while resolving field Query.foo")

graphql/execution/tests/test_executor_gevent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import gevent
33

44
from graphql.error import format_error
5+
from graphql.execution import execute
56
from graphql.language.location import SourceLocation
67
from graphql.language.parser import parse
78
from graphql.type import (GraphQLField, GraphQLObjectType, GraphQLSchema,
89
GraphQLString)
910

10-
from ..execute import execute
1111
from ..executors.gevent import GeventExecutor
1212
from .test_mutations import assert_evaluate_mutations_serially
1313

graphql/execution/tests/test_executor_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
from graphql.error import format_error
3-
from graphql.execution.execute import execute
3+
from graphql.execution import execute
44
from graphql.language.parser import parse
55
from graphql.type import (GraphQLArgument, GraphQLField, GraphQLInt,
66
GraphQLList, GraphQLObjectType, GraphQLSchema,

graphql/execution/tests/test_lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def run_check(self):
2626
)
2727

2828
schema = GraphQLSchema(query=DataType)
29-
response = execute(schema, data, ast)
29+
response = execute(schema, ast, data)
3030

3131
if response.errors:
3232
result = {

graphql/execution/tests/test_mutations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from graphql.execution.execute import execute
1+
from graphql.execution import execute
22
from graphql.language.parser import parse
33
from graphql.type import (GraphQLArgument, GraphQLField, GraphQLInt,
44
GraphQLList, GraphQLObjectType, GraphQLSchema,

0 commit comments

Comments
 (0)