1
1
from graphene .types .schema import Schema
2
2
3
- from graphql .error import format_error
3
+ from graphql .error import format_error as format_graphql_error
4
+ from graphql .error import GraphQLError
4
5
5
6
6
- def format_execution_result (execution_result ):
7
+ def default_format_error (error ):
8
+ if isinstance (error , GraphQLError ):
9
+ return format_graphql_error (error )
10
+
11
+ return {'message' : six .text_type (error )}
12
+
13
+
14
+ def format_execution_result (execution_result , format_error ):
7
15
if execution_result :
8
16
response = {}
9
17
@@ -17,12 +25,14 @@ def format_execution_result(execution_result):
17
25
18
26
19
27
class Client (object ):
20
- def __init__ (self , schema , ** execute_options ):
28
+ def __init__ (self , schema , format_error = None , ** execute_options ):
21
29
assert isinstance (schema , Schema )
22
30
self .schema = schema
23
31
self .execute_options = execute_options
32
+ self .format_error = format_error or default_format_error
24
33
25
34
def execute (self , * args , ** kwargs ):
26
35
return format_execution_result (
27
- self .schema .execute (* args , ** dict (self .execute_options , ** kwargs ))
36
+ self .schema .execute (* args , ** dict (self .execute_options , ** kwargs )),
37
+ self .format_error
28
38
)
0 commit comments