-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextsever.py
152 lines (121 loc) · 4.37 KB
/
nextsever.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import json
import six
from collections import namedtuple, MutableMapping
from graphql import (
graphql,
GraphQLSchema
)
class SkipException(Exception):
pass
class HttpQueryError(Exception):
def __init__(self, status_code, message=None, is_graphql_error=False, headers=None):
self.status_code = status_code
self.message = message
self.is_graphql_error = is_graphql_error
self.headers = headers
super(HttpQueryError, self).__init__(message)
def __eq__(self, other):
return (
isinstance(other, HttpQueryError)
and other.status_code == self.status_code
and other.message == self.message
and other.headers == self.headers
)
def __hash__(self):
if self.headers:
headers_hash = tuple(self.headers.items())
else:
headers_hash = None
return hash((self.status_code, self.message, headers_hash))
GraphQLParams = namedtuple("GraphQLParams", "query,variables,operation_name")
GraphQLResponse = namedtuple("GraphQLResponse", "result,status_code")
def run_http_query(
schema, # type: GraphQLSchema
request_method, # type: str
data, # type: Union[Dict, List[Dict]]
query_data=None, # type: Optional[Dict]
batch_enabled=False, # type: bool
catch=False, # type: bool
**execute_options # type: Dict
):
if request_method not in ("get", "post"):
raise HttpQueryError(
405,
"GraphQL only supports GET and POST requests.",
headers={"Allow": "GET, POST"},
)
if catch:
catch_exc = (
HttpQueryError
) # type: Union[Type[HttpQueryError], Type[SkipException]]
else:
catch_exc = SkipException
is_batch = isinstance(data, list)
is_get_request = request_method == "get"
allow_only_query = is_get_request
if not is_batch:
if not isinstance(data, (dict, MutableMapping)):
raise HttpQueryError(
400, "GraphQL params should be a dict. Received {}.".format(
data)
)
data = [data]
elif not batch_enabled:
raise HttpQueryError(400, "Batch GraphQL requests are not enabled.")
if not data:
raise HttpQueryError(
400, "Received an empty list in the batch request.")
extra_data = {} # type: Dict[str, Any]
# If is a batch request, we don't consume the data from the query
if not is_batch:
extra_data = query_data or {}
all_params = [get_graphql_params(entry, extra_data) for entry in data]
responses = [
get_response(schema, params, catch_exc,
allow_only_query, **execute_options)
for params in all_params
]
return responses, all_params
def get_graphql_params(data, query_data):
# type: (Dict, Dict) -> GraphQLParams
query = data.get("query") or query_data.get("query")
variables = data.get("variables") or query_data.get("variables")
# document_id = data.get('documentId')
operation_name = data.get(
"operationName") or query_data.get("operationName")
return GraphQLParams(query, load_json_variables(variables), operation_name)
def get_response(
schema, # type: GraphQLSchema
params, # type: GraphQLParams
catch, # type: Type[BaseException]
allow_only_query=False, # type: bool
**kwargs # type: Dict
):
# type: (...) -> Optional[ExecutionResult]
try:
# execution_result = execute_graphql_request(
# schema, params, allow_only_query, **kwargs
# )
a = graphql(schema, query)
except catch:
return None
return execution_result
def json_encode(data, pretty=False):
# type: (Dict, bool) -> str
if not pretty:
return json.dumps(data, separators=(",", ":"))
return json.dumps(data, indent=2, separators=(",", ": "))
def load_json_variables(variables):
# type: (Optional[Union[str, Dict]]) -> Optional[Dict]
if variables and isinstance(variables, six.string_types):
try:
return json.loads(variables)
except Exception:
raise HttpQueryError(400, "Variables are invalid JSON.")
return variables # type: ignore
def load_json_body(data):
# type: (str) -> Dict
try:
return json.loads(data)
except Exception:
raise HttpQueryError(400, "POST body sent invalid JSON.")