-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
graphql.py
64 lines (48 loc) · 1.65 KB
/
graphql.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
import json
from typing import Any
from mitmproxy.contentviews import base
from mitmproxy.contentviews.json import PARSE_ERROR
from mitmproxy.contentviews.json import parse_json
def format_graphql(data):
query = data["query"]
header_data = data.copy()
header_data["query"] = "..."
return """{header}
---
{query}
""".format(header=json.dumps(header_data, indent=2), query=query)
def format_query_list(data: list[Any]):
num_queries = len(data) - 1
result = ""
for i, op in enumerate(data):
result += f"--- {i}/{num_queries}\n"
result += format_graphql(op)
return result
def is_graphql_query(data):
return isinstance(data, dict) and "query" in data and "\n" in data["query"]
def is_graphql_batch_query(data):
return (
isinstance(data, list)
and len(data) > 0
and isinstance(data[0], dict)
and "query" in data[0]
)
class ViewGraphQL(base.View):
name = "GraphQL"
def __call__(self, data, **metadata):
data = parse_json(data)
if data is not PARSE_ERROR:
if is_graphql_query(data):
return "GraphQL", base.format_text(format_graphql(data))
elif is_graphql_batch_query(data):
return "GraphQL", base.format_text(format_query_list(data))
def render_priority(
self, data: bytes, *, content_type: str | None = None, **metadata
) -> float:
if content_type != "application/json" or not data:
return 0
data = parse_json(data)
if data is not PARSE_ERROR:
if is_graphql_query(data) or is_graphql_batch_query(data):
return 2
return 0