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

+3-3
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

+1-4
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

+7
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

+1-1
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

+1-1
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

+40-16
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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,

graphql/execution/tests/test_nonnull.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def nonNullPromiseNest(self):
8383

8484
def check(doc, data, expected):
8585
ast = parse(doc)
86-
response = execute(schema, data, ast)
86+
response = execute(schema, ast, data)
8787

8888
if response.errors:
8989
result = {

graphql/execution/tests/test_union_interface.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_can_introspect_on_union_and_intersection_types():
106106
}
107107
}''')
108108

109-
result = execute(schema, None, ast)
109+
result = execute(schema, ast)
110110
assert result.data == {
111111
'Named': {
112112
'enumValues': None,
@@ -143,7 +143,7 @@ def test_executes_using_union_types():
143143
}
144144
}
145145
''')
146-
result = execute(schema, john, ast)
146+
result = execute(schema, ast, john)
147147
assert not result.errors
148148
assert result.data == {
149149
'__typename': 'Person',
@@ -174,7 +174,7 @@ def test_executes_union_types_with_inline_fragment():
174174
}
175175
}
176176
''')
177-
result = execute(schema, john, ast)
177+
result = execute(schema, ast, john)
178178
assert not result.errors
179179
assert result.data == {
180180
'__typename': 'Person',
@@ -200,7 +200,7 @@ def test_executes_using_interface_types():
200200
}
201201
}
202202
''')
203-
result = execute(schema, john, ast)
203+
result = execute(schema, ast, john)
204204
assert not result.errors
205205
assert result.data == {
206206
'__typename': 'Person',
@@ -230,7 +230,7 @@ def test_executes_interface_types_with_inline_fragment():
230230
}
231231
}
232232
''')
233-
result = execute(schema, john, ast)
233+
result = execute(schema, ast, john)
234234
assert not result.errors
235235
assert result.data == {
236236
'__typename': 'Person',
@@ -272,7 +272,7 @@ def test_allows_fragment_conditions_to_be_abstract_types():
272272
}
273273
}
274274
''')
275-
result = execute(schema, john, ast)
275+
result = execute(schema, ast, john)
276276
assert not result.errors
277277
assert result.data == {
278278
'__typename': 'Person',
@@ -300,7 +300,7 @@ def test_only_include_fields_from_matching_fragment_condition():
300300
}
301301
}
302302
''')
303-
result = execute(schema, john, ast)
303+
result = execute(schema, ast, john)
304304
assert not result.errors
305305
assert result.data == {
306306
'pets': [
@@ -340,7 +340,7 @@ def resolve_type(obj, info):
340340
john2 = Person('John', [], [liz])
341341
ast = parse('''{ name, friends { name } }''')
342342

343-
result = execute(schema2, john2, ast)
343+
result = execute(schema2, ast, john2)
344344
assert result.data == {
345345
'name': 'John', 'friends': [{'name': 'Liz'}]
346346
}

graphql/execution/tests/test_variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def input_to_json(obj, args, info):
9191

9292
def check(doc, expected, args=None):
9393
ast = parse(doc)
94-
response = execute(schema, None, ast, args=args)
94+
response = execute(schema, ast, variable_values=args)
9595

9696
if response.errors:
9797
result = {

graphql/type/tests/test_introspection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def test_supports_the_type_root_field():
590590
})
591591
schema = GraphQLSchema(TestType)
592592
request = '{ __type(name: "TestType") { name } }'
593-
result = execute(schema, object(), parse(request))
593+
result = execute(schema, parse(request), object())
594594
assert not result.errors
595595
assert result.data == {'__type': {'name': 'TestType'}}
596596

graphql/utils/tests/test_extend_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_cannot_be_used_for_execution():
101101
extended_schema = extend_schema(test_schema, ast)
102102
clientQuery = parse('{ newField }')
103103

104-
result = execute(extended_schema, object(), clientQuery)
104+
result = execute(extended_schema, clientQuery, object())
105105
assert result.data['newField'] is None
106106
assert str(result.errors[0]
107107
) == 'Client Schema cannot be used for execution.'

0 commit comments

Comments
 (0)