-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraphql_playground.py
317 lines (229 loc) · 9.05 KB
/
graphql_playground.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import sublime
import sublime_plugin
import requests
import os.path
import re
from .src.graphql_view_manager import GraphqlViewManager
from .graphql_config import readGraphqlConfig
from random import randint
CONNID = None
PATTERN = r'^ *(?:query|mutation) +(\w*) *'
def getQueryVariablesFile(view):
filePath = view.file_name()
if filePath is None:
return None
fileName = os.path.splitext(
os.path.basename(filePath)
)
filename = "%s.var.json" % (fileName[0])
return os.path.join(os.path.dirname(filePath), filename)
class GraphqlRunQueryCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
global CONNID
data = {
"operationName": args['operationName'] if args['operationName'] != "" else None,
"query": args['query'],
"variables": args['variables'],
}
if "config" not in args:
return
CONNID = randint(1, 100)
sublime.set_timeout_async(lambda: self.sendRequest(data, args, CONNID))
def sendRequest(self, data, args, conn):
string = "// Error occurred"
resp = None
settings = sublime.load_settings("graphql_playground.sublime-settings")
if settings.get('debug'):
_data = sublime.encode_value(data)
print("-- Grapqhl Playground debug::start --")
print("\t%s" % (_data))
print("-- Grapqhl Playground debug::end --")
try:
endpoint = args['config']['schema']
headers = {}
if type(args['config']['schema']) is list:
if len(args['config']['schema']) <= 0:
raise Exception("Invalid graphql config")
# Use the first endpoint only
c = args['config']['schema'][0]
endpoint, config = list(c.items())[0]
if "headers" in config:
headers = config['headers']
resp = requests.post(endpoint, headers=headers, json=data)
string = resp.text
except Exception as e:
print("Graphql Playground error:", e)
try:
if resp is not None:
string = sublime.encode_value(resp.json(), True)
except Exception as e:
print("Graphql Playground error:", e)
self.view.run_command("graphql_print_response", { "content": string, "connection": conn })
class GraphqlPrintResponseCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
global CONNID
if "content" not in args:
return
if args["connection"] != CONNID:
return
self.view.replace(
edit,
sublime.Region(0, self.view.size()),
args['content']
)
class GraphqlPrepareViewCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
operationName = args['operationName']
if args['operationName'] is None:
operationName = "Query"
if self.view.size() <= 0:
self.view.replace(edit, sublime.Region(0, self.view.size()), "// Running %s..." % (operationName))
window = self.view.window()
if window is not None:
window.status_message("❄ Running %s..." % (operationName))
self.view.run_command("graphql_run_query", args)
def is_visible(self):
return False
class GraphqlOpenViewCommand(sublime_plugin.WindowCommand):
def run(self, **args):
view = next(v for v in self.window.views() if v.id() == args['view'])
fileTitle = "GraphQL: %s" % args['operationName']
if args['operationName'] is None:
fileTitle = "GraphQL"
view.run_command("graphql_open_response_view", { "force": True })
responseView = GraphqlViewManager.get(view.id())
if responseView is None:
return
sheets = [view.sheet(), None]
variables = {}
variablesFile = getQueryVariablesFile(view)
if variablesFile is not None:
vview = self.window.find_open_file(variablesFile)
if vview:
view.run_command("graphql_open_query_variables", { "force": False })
variables = sublime.decode_value(
vview.substr(
sublime.Region(0, vview.size())
)
)
if variables == {} and os.path.exists(variablesFile):
fh = open(variablesFile, 'r')
variables = sublime.decode_value(fh.read())
fh.close()
responseView.set_name(fileTitle)
sheets = sheets + [responseView.sheet()]
query = view.substr(sublime.Region(0, view.size()))
graphqlConfig = readGraphqlConfig(view)
responseView.run_command("graphql_prepare_view", {
"operationName": args['operationName'],
"query": query,
"variables": variables,
"config": graphqlConfig
})
sheets = list(set(sheets + self.window.selected_sheets()))
self.window.select_sheets(filter(None, sheets))
self.window.focus_view(view)
def is_visible(self):
return False
class GraphqlBuildAnnotationsCommand(sublime_plugin.TextCommand):
ANNOTATIONS_KEY = 'graphql_runner_annotations'
def __init__(self, view):
super().__init__(view)
self.keys = []
def run(self, edit, **args):
syntax = self.view.syntax()
if not syntax or syntax.name != "GraphQL":
return
for k in self.keys:
self.view.erase_regions(k)
self.keys = []
sublime.set_timeout(lambda: self._build_annotations(), 1000)
def _build_annotations(self):
matches = []
regions = self.view.find_all(PATTERN, sublime.IGNORECASE, "$1", matches)
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
annotation_styles = self.view.style_for_scope("region.purplish")
html = """
<style>
html, div {{
padding: 0;
margin: 0;
border: 0;
}}
.gql-run-label {{
color: var(--accent);
}}
</style>
<body id='graphql-run-box'>
<a class=\"gql-run-label\" href='{cmd}'>Run Query</a>
</body>
"""
for [index, operationName] in enumerate(matches):
key = "%s_%s" % (self.ANNOTATIONS_KEY, index)
self.keys.append(key)
if operationName == "":
operationName = None
query = sublime.encode_value({
"operationName": operationName,
"view": self.view.id()
})
cmd = "subl:graphql_open_view %s" % (query)
annotations = html.format(cmd=cmd)
self.view.add_regions(key, [regions[index]], "", "", flags, [annotations], annotation_styles["foreground"])
class GraphqlQuickRunQueryCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
if len(selection) <= 0:
return
line = self.view.line(selection[0])
match = re.match(PATTERN, self.view.substr(line))
upperLines = self.view.lines(sublime.Region(0, line.a))
upperLines.reverse()
if match is None:
for u in upperLines:
match = re.match(PATTERN, self.view.substr(u))
if match is not None:
break
if match is None:
return
w = self.view.window()
if w is None:
return
w.run_command("graphql_open_view", {
"operationName": match.group(1),
"view": self.view.id()
})
class GraphqlPlaygroundViewListener(sublime_plugin.ViewEventListener):
@classmethod
def applies_to_primary_view_only(cls):
return True
def _build_annotations(self):
syntax = self.view.syntax()
if not syntax or syntax.name != "GraphQL":
return
graphqlConfig = readGraphqlConfig(self.view)
if graphqlConfig is None:
print("GraphqlPlayground: No graphql config found")
return
self.view.run_command("graphql_build_annotations")
def on_load(self):
self._build_annotations()
def on_modified(self):
self._build_annotations()
def on_activated(self):
syntax = self.view.syntax()
if not syntax or syntax.name != "GraphQL":
return
self.view.run_command("graphql_open_response_view", { "force": False })
self.view.run_command("graphql_open_query_variables", { "force": False })
def on_query_context(self, key, operator, operand, match_all):
if not key.startswith("graphql_playground."):
return None
syntax = self.view.syntax()
if syntax and operator == sublime.OP_EQUAL:
return (syntax.name == "GraphQL") == operand
if syntax and operator == sublime.OP_NOT_EQUAL:
return (syntax.name == "GraphQL") == operand
return False
def on_close(self):
GraphqlViewManager.removeView(self.view)