8
8
9
9
from .error import HttpQueryError
10
10
11
+ if False :
12
+ from typing import List , Dict , Optional , Tuple , Any , Union , Callable , Type
13
+ from graphql import GraphQLSchema , GraphQLBackend
14
+
11
15
12
16
class SkipException (Exception ):
13
17
pass
14
18
15
19
16
- GraphQLParams = namedtuple (' GraphQLParams' , ' query,variables,operation_name' )
17
- GraphQLResponse = namedtuple (' GraphQLResponse' , ' result,status_code' )
20
+ GraphQLParams = namedtuple (" GraphQLParams" , " query,variables,operation_name" )
21
+ GraphQLResponse = namedtuple (" GraphQLResponse" , " result,status_code" )
18
22
19
23
20
- def run_http_query (schema , request_method , data , query_data = None , batch_enabled = False , catch = False , ** execute_options ):
21
- if request_method not in ('get' , 'post' ):
24
+ def run_http_query (
25
+ schema , # type: GraphQLSchema
26
+ request_method , # type: str
27
+ data , # type: Union[Dict, List[Dict]]
28
+ query_data = None , # type: Optional[Dict]
29
+ batch_enabled = False , # type: bool
30
+ catch = False , # type: bool
31
+ ** execute_options # type: Dict
32
+ ):
33
+ if request_method not in ("get" , "post" ):
22
34
raise HttpQueryError (
23
35
405 ,
24
- 'GraphQL only supports GET and POST requests.' ,
25
- headers = {
26
- 'Allow' : 'GET, POST'
27
- }
36
+ "GraphQL only supports GET and POST requests." ,
37
+ headers = {"Allow" : "GET, POST" },
28
38
)
29
39
if catch :
30
- catch = HttpQueryError
40
+ catch_exc = (
41
+ HttpQueryError
42
+ ) # type: Union[Type[HttpQueryError], Type[SkipException]]
31
43
else :
32
- catch = SkipException
44
+ catch_exc = SkipException
33
45
is_batch = isinstance (data , list )
34
46
35
- is_get_request = request_method == ' get'
47
+ is_get_request = request_method == " get"
36
48
allow_only_query = is_get_request
37
49
38
50
if not is_batch :
39
51
if not isinstance (data , (dict , MutableMapping )):
40
52
raise HttpQueryError (
41
- 400 ,
42
- 'GraphQL params should be a dict. Received {}.' .format (data )
53
+ 400 , "GraphQL params should be a dict. Received {}." .format (data )
43
54
)
44
55
data = [data ]
45
56
elif not batch_enabled :
46
- raise HttpQueryError (
47
- 400 ,
48
- 'Batch GraphQL requests are not enabled.'
49
- )
57
+ raise HttpQueryError (400 , "Batch GraphQL requests are not enabled." )
50
58
51
59
if not data :
52
- raise HttpQueryError (
53
- 400 ,
54
- 'Received an empty list in the batch request.'
55
- )
60
+ raise HttpQueryError (400 , "Received an empty list in the batch request." )
56
61
57
- extra_data = {}
62
+ extra_data = {} # type: Dict[str, Any]
58
63
# If is a batch request, we don't consume the data from the query
59
64
if not is_batch :
60
65
extra_data = query_data or {}
61
66
62
67
all_params = [get_graphql_params (entry , extra_data ) for entry in data ]
63
68
64
- responses = [get_response (
65
- schema ,
66
- params ,
67
- catch ,
68
- allow_only_query ,
69
- ** execute_options
70
- ) for params in all_params ]
69
+ responses = [
70
+ get_response (schema , params , catch_exc , allow_only_query , ** execute_options )
71
+ for params in all_params
72
+ ]
71
73
72
74
return responses , all_params
73
75
74
76
75
- def encode_execution_results (execution_results , format_error , is_batch , encode ):
77
+ def encode_execution_results (
78
+ execution_results , # type: List[Optional[ExecutionResult]]
79
+ format_error , # type: Callable[[Exception], Dict]
80
+ is_batch , # type: bool
81
+ encode , # type: Callable[[Dict], Any]
82
+ ):
83
+ # type: (...) -> Tuple[Any, int]
76
84
responses = [
77
85
format_execution_result (execution_result , format_error )
78
86
for execution_result in execution_results
@@ -87,72 +95,77 @@ def encode_execution_results(execution_results, format_error, is_batch, encode):
87
95
88
96
89
97
def json_encode (data , pretty = False ):
98
+ # type: (Dict, bool) -> str
90
99
if not pretty :
91
- return json .dumps (data , separators = (',' , ':' ))
100
+ return json .dumps (data , separators = ("," , ":" ))
92
101
93
- return json .dumps (
94
- data ,
95
- indent = 2 ,
96
- separators = (',' , ': ' )
97
- )
102
+ return json .dumps (data , indent = 2 , separators = ("," , ": " ))
98
103
99
104
100
105
def load_json_variables (variables ):
106
+ # type: (Optional[Union[str, Dict]]) -> Optional[Dict]
101
107
if variables and isinstance (variables , six .string_types ):
102
108
try :
103
109
return json .loads (variables )
104
110
except Exception :
105
- raise HttpQueryError (400 , ' Variables are invalid JSON.' )
106
- return variables
111
+ raise HttpQueryError (400 , " Variables are invalid JSON." )
112
+ return variables # type: ignore
107
113
108
114
109
115
def get_graphql_params (data , query_data ):
110
- query = data .get ('query' ) or query_data .get ('query' )
111
- variables = data .get ('variables' ) or query_data .get ('variables' )
116
+ # type: (Dict, Dict) -> GraphQLParams
117
+ query = data .get ("query" ) or query_data .get ("query" )
118
+ variables = data .get ("variables" ) or query_data .get ("variables" )
112
119
# document_id = data.get('documentId')
113
- operation_name = data .get (' operationName' ) or query_data .get (' operationName' )
120
+ operation_name = data .get (" operationName" ) or query_data .get (" operationName" )
114
121
115
122
return GraphQLParams (query , load_json_variables (variables ), operation_name )
116
123
117
124
118
- def get_response (schema , params , catch = None , allow_only_query = False , ** kwargs ):
125
+ def get_response (
126
+ schema , # type: GraphQLSchema
127
+ params , # type: GraphQLParams
128
+ catch , # type: Type[BaseException]
129
+ allow_only_query = False , # type: bool
130
+ ** kwargs # type: Dict
131
+ ):
132
+ # type: (...) -> Optional[ExecutionResult]
119
133
try :
120
134
execution_result = execute_graphql_request (
121
- schema ,
122
- params ,
123
- allow_only_query ,
124
- ** kwargs
135
+ schema , params , allow_only_query , ** kwargs
125
136
)
126
137
except catch :
127
138
return None
128
139
129
140
return execution_result
130
141
131
142
132
- def format_execution_result (execution_result , format_error ):
143
+ def format_execution_result (
144
+ execution_result , # type: Optional[ExecutionResult]
145
+ format_error , # type: Optional[Callable[[Exception], Dict]]
146
+ ):
147
+ # type: (...) -> GraphQLResponse
133
148
status_code = 200
134
149
135
150
if execution_result :
136
- response = {}
137
-
138
- if execution_result .errors :
139
- response ['errors' ] = [format_error (e ) for e in execution_result .errors ]
140
-
141
151
if execution_result .invalid :
142
152
status_code = 400
143
- else :
144
- status_code = 200
145
- response ['data' ] = execution_result .data
146
-
153
+ response = execution_result .to_dict (format_error = format_error )
147
154
else :
148
155
response = None
149
156
150
157
return GraphQLResponse (response , status_code )
151
158
152
159
153
- def execute_graphql_request (schema , params , allow_only_query = False , backend = None , ** kwargs ):
160
+ def execute_graphql_request (
161
+ schema , # type: GraphQLSchema
162
+ params , # type: GraphQLParams
163
+ allow_only_query = False , # type: bool
164
+ backend = None , # type: GraphQLBackend
165
+ ** kwargs # type: Dict
166
+ ):
154
167
if not params .query :
155
- raise HttpQueryError (400 , ' Must provide query string.' )
168
+ raise HttpQueryError (400 , " Must provide query string." )
156
169
157
170
try :
158
171
if not backend :
@@ -163,45 +176,41 @@ def execute_graphql_request(schema, params, allow_only_query=False, backend=None
163
176
164
177
if allow_only_query :
165
178
operation_type = document .get_operation_type (params .operation_name )
166
- if operation_type and operation_type != ' query' :
179
+ if operation_type and operation_type != " query" :
167
180
raise HttpQueryError (
168
181
405 ,
169
- ' Can only perform a {} operation from a POST request.' .format (operation_type ),
170
- headers = {
171
- 'Allow' : 'POST' ,
172
- }
182
+ " Can only perform a {} operation from a POST request." .format (
183
+ operation_type
184
+ ) ,
185
+ headers = { "Allow" : "POST" },
173
186
)
174
187
175
188
try :
176
189
return document .execute (
177
- operation_name = params .operation_name ,
178
- variables = params .variables ,
179
- ** kwargs
190
+ operation_name = params .operation_name , variables = params .variables , ** kwargs
180
191
)
181
192
except Exception as e :
182
193
return ExecutionResult (errors = [e ], invalid = True )
183
194
184
195
185
196
def load_json_body (data ):
197
+ # type: (str) -> Dict
186
198
try :
187
199
return json .loads (data )
188
200
except Exception :
189
- raise HttpQueryError (
190
- 400 ,
191
- 'POST body sent invalid JSON.'
192
- )
201
+ raise HttpQueryError (400 , "POST body sent invalid JSON." )
193
202
194
203
195
204
__all__ = [
196
- ' default_format_error' ,
197
- ' SkipException' ,
198
- ' run_http_query' ,
199
- ' encode_execution_results' ,
200
- ' json_encode' ,
201
- ' load_json_variables' ,
202
- ' get_graphql_params' ,
203
- ' get_response' ,
204
- ' format_execution_result' ,
205
- ' execute_graphql_request' ,
206
- ' load_json_body'
205
+ " default_format_error" ,
206
+ " SkipException" ,
207
+ " run_http_query" ,
208
+ " encode_execution_results" ,
209
+ " json_encode" ,
210
+ " load_json_variables" ,
211
+ " get_graphql_params" ,
212
+ " get_response" ,
213
+ " format_execution_result" ,
214
+ " execute_graphql_request" ,
215
+ " load_json_body" ,
207
216
]
0 commit comments