Skip to content

Commit 6aeda8d

Browse files
committed
Implement support for async requests
1 parent 92136cf commit 6aeda8d

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

graphql-mode.el

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
:tag "GraphQL"
5252
:group 'languages)
5353

54+
(defcustom graphql-async t
55+
"If non-nil, requests will be done asynchronously."
56+
:tag "GraphQL"
57+
:type 'boolean
58+
:safe 'booleanp
59+
:group 'graphql)
60+
5461
(defcustom graphql-indent-level 2
5562
"Number of spaces for each indentation step in `graphql-mode'."
5663
:tag "GraphQL"
@@ -146,8 +153,9 @@ Please install it and try again."))
146153
:type "POST"
147154
:data body
148155
:headers headers
149-
:parser 'json-read
150-
:sync t
156+
:parser 'buffer-string
157+
:sync (not graphql-async)
158+
:success #'graphql--success-callback
151159
:complete (lambda (&rest _)
152160
(message "%s" (if (string-equal "" operation)
153161
url
@@ -157,7 +165,8 @@ Please install it and try again."))
157165
(defun graphql-beginning-of-query ()
158166
"Move the point to the beginning of the current query."
159167
(interactive)
160-
(goto-char (syntax-ppss-toplevel-pos (syntax-ppss (point-at-bol))))
168+
(goto-char (or (syntax-ppss-toplevel-pos (syntax-ppss (point-at-bol)))
169+
(point-min)))
161170
(back-to-indentation))
162171

163172
(defun graphql-end-of-query ()
@@ -224,6 +233,21 @@ Please install it and try again."))
224233
(define-key map (kbd "q") 'quit-window)
225234
map))
226235

236+
(cl-defun graphql--success-callback (&key response &allow-other-keys)
237+
(with-current-buffer-window
238+
"*GraphQL*" 'display-buffer-pop-up-window nil
239+
(erase-buffer)
240+
(when (fboundp 'json-mode)
241+
(json-mode))
242+
(insert (request-response-data response))
243+
(json-pretty-print-buffer)
244+
(goto-char (point-max))
245+
(insert "\n\n"
246+
(propertize (request-response--raw-header response)
247+
'face 'font-lock-comment-face
248+
'font-lock-face 'font-lock-comment-face))
249+
(graphql-query-response-mode)))
250+
227251
(defun graphql-send-query ()
228252
"Send the current GraphQL query/mutation/subscription to server."
229253
(interactive)
@@ -234,27 +258,14 @@ Please install it and try again."))
234258

235259
(let* ((query (buffer-substring-no-properties (point-min) (point-max)))
236260
(operation (graphql-current-operation))
237-
(variables (graphql-current-variables var))
238-
(response (graphql--query query operation variables)))
239-
(with-current-buffer-window
240-
"*GraphQL*" 'display-buffer-pop-up-window nil
241-
(erase-buffer)
242-
(when (fboundp 'json-mode)
243-
(json-mode))
244-
(insert (json-encode (request-response-data response)))
245-
(json-pretty-print-buffer)
246-
(goto-char (point-max))
247-
(insert "\n\n"
248-
(propertize (request-response--raw-header response)
249-
'face 'font-lock-comment-face
250-
'font-lock-face 'font-lock-comment-face))
251-
(graphql-query-response-mode))))
261+
(variables (graphql-current-variables var)))
262+
(graphql--query query operation variables))
252263
;; If the query was successful, then save the value of graphql-url
253264
;; in the current buffer (instead of the introduced local
254265
;; binding).
255266
(setq graphql-url url)
256267
(setq graphql-variables-file var)
257-
nil))
268+
nil)))
258269

259270
(defvar graphql-mode-map
260271
(let ((map (make-sparse-keymap)))

0 commit comments

Comments
 (0)