|
3 | 3 | from graphql.core.execution import Executor
|
4 | 4 | from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
|
5 | 5 | from graphql.core.pyutils.defer import succeed, Deferred, fail
|
6 |
| -from graphql.core.language.parser import parse |
7 | 6 | from graphql.core.type import (GraphQLSchema, GraphQLObjectType, GraphQLField,
|
8 | 7 | GraphQLArgument, GraphQLList, GraphQLInt, GraphQLString)
|
9 | 8 | from graphql.core.type.definition import GraphQLNonNull
|
10 |
| - |
11 | 9 | from .utils import raise_callback_results
|
12 | 10 |
|
13 | 11 |
|
@@ -69,7 +67,6 @@ def deeper(self):
|
69 | 67 | }
|
70 | 68 | '''
|
71 | 69 |
|
72 |
| - ast = parse(doc) |
73 | 70 | expected = {
|
74 | 71 | 'a': 'Apple',
|
75 | 72 | 'b': 'Banana',
|
@@ -236,6 +233,92 @@ def promise(self):
|
236 | 233 | assert not result.errors
|
237 | 234 |
|
238 | 235 |
|
| 236 | +def test_synchronous_error_nulls_out_error_subtrees(): |
| 237 | + doc = ''' |
| 238 | + { |
| 239 | + sync |
| 240 | + syncError |
| 241 | + syncReturnError |
| 242 | + syncReturnErrorList |
| 243 | + async |
| 244 | + asyncReject |
| 245 | + asyncEmptyReject |
| 246 | + asyncReturnError |
| 247 | + } |
| 248 | + ''' |
| 249 | + |
| 250 | + class Data: |
| 251 | + def sync(self): |
| 252 | + return 'sync' |
| 253 | + |
| 254 | + def syncError(self): |
| 255 | + raise Exception('Error getting syncError') |
| 256 | + |
| 257 | + def syncReturnError(self): |
| 258 | + return Exception("Error getting syncReturnError") |
| 259 | + |
| 260 | + def syncReturnErrorList(self): |
| 261 | + return [ |
| 262 | + 'sync0', |
| 263 | + Exception('Error getting syncReturnErrorList1'), |
| 264 | + 'sync2', |
| 265 | + Exception('Error getting syncReturnErrorList3') |
| 266 | + ] |
| 267 | + |
| 268 | + def async(self): |
| 269 | + return succeed('async') |
| 270 | + |
| 271 | + def asyncReject(self): |
| 272 | + return fail(Exception('Error getting asyncReject')) |
| 273 | + |
| 274 | + def asyncEmptyReject(self): |
| 275 | + return fail() |
| 276 | + |
| 277 | + def asyncReturnError(self): |
| 278 | + return succeed(Exception('Error getting asyncReturnError')) |
| 279 | + |
| 280 | + schema = GraphQLSchema( |
| 281 | + query=GraphQLObjectType( |
| 282 | + name='Type', |
| 283 | + fields={ |
| 284 | + 'sync': GraphQLField(GraphQLString), |
| 285 | + 'syncError': GraphQLField(GraphQLString), |
| 286 | + 'syncReturnError': GraphQLField(GraphQLString), |
| 287 | + 'syncReturnErrorList': GraphQLField(GraphQLList(GraphQLString)), |
| 288 | + 'async': GraphQLField(GraphQLString), |
| 289 | + 'asyncReject': GraphQLField(GraphQLString), |
| 290 | + 'asyncEmptyReject': GraphQLField(GraphQLString), |
| 291 | + 'asyncReturnError': GraphQLField(GraphQLString), |
| 292 | + } |
| 293 | + ) |
| 294 | + ) |
| 295 | + |
| 296 | + executor = Executor(map_type=OrderedDict) |
| 297 | + |
| 298 | + def handle_results(result): |
| 299 | + assert result.data == { |
| 300 | + 'async': 'async', |
| 301 | + 'asyncEmptyReject': None, |
| 302 | + 'asyncReject': None, |
| 303 | + 'asyncReturnError': None, |
| 304 | + 'sync': 'sync', |
| 305 | + 'syncError': None, |
| 306 | + 'syncReturnError': None, |
| 307 | + 'syncReturnErrorList': ['sync0', None, 'sync2', None] |
| 308 | + } |
| 309 | + assert list(map(format_error, result.errors)) == [ |
| 310 | + {'locations': [{'line': 4, 'column': 9}], 'message': 'Error getting syncError'}, |
| 311 | + {'locations': [{'line': 5, 'column': 9}], 'message': 'Error getting syncReturnError'}, |
| 312 | + {'locations': [{'line': 6, 'column': 9}], 'message': 'Error getting syncReturnErrorList1'}, |
| 313 | + {'locations': [{'line': 6, 'column': 9}], 'message': 'Error getting syncReturnErrorList3'}, |
| 314 | + {'locations': [{'line': 8, 'column': 9}], 'message': 'Error getting asyncReject'}, |
| 315 | + {'locations': [{'line': 9, 'column': 9}], 'message': 'An unknown error occurred.'}, |
| 316 | + {'locations': [{'line': 10, 'column': 9}], 'message': 'Error getting asyncReturnError'} |
| 317 | + ] |
| 318 | + |
| 319 | + raise_callback_results(executor.execute(schema, doc, Data()), handle_results) |
| 320 | + |
| 321 | + |
239 | 322 | def test_executor_can_enforce_strict_ordering():
|
240 | 323 | Type = GraphQLObjectType('Type', lambda: {
|
241 | 324 | 'a': GraphQLField(GraphQLString,
|
|
0 commit comments