emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/graphql-mode 2371316a75 116/122: Merge pull request #42 fr


From: ELPA Syncer
Subject: [nongnu] elpa/graphql-mode 2371316a75 116/122: Merge pull request #42 from adimit/headers-buffer
Date: Sat, 29 Jan 2022 08:03:40 -0500 (EST)

branch: elpa/graphql-mode
commit 2371316a750b807de941184d49ca19d277ecadcd
Merge: 9bed568ec8 3e27ae34cd
Author: David Vázquez Púa <davazp@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #42 from adimit/headers-buffer
    
    Headers buffer
---
 README.md       | 13 ++++++++++++
 graphql-mode.el | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/README.md b/README.md
index 452adcd1bd..7fe748a758 100644
--- a/README.md
+++ b/README.md
@@ -14,3 +14,16 @@ extension will be loaded with this mode.
 
 You can optionally install `json-mode`, and it will be enabled in the
 buffer that contains the response from a GraphQL service.
+
+## Querying Endpoints
+
+To send a query to a server, you will first need the
+[`request`](https://github.com/tkf/emacs-request) package. Then use
+`graphql-send-query` (`C-c C-c`) to send a query.
+
+If you have a `.graphqlconfig` file, you can select an endpoint configuration
+with `graphql-select-endpoint` (`C-c C-l`).
+
+To send additional headers for a request, `graphql-extra-headers` must be
+set. It is automatically set by `graphql-select-endpoint`, or you can edit its
+value using JSON with `graphql-edit-headers` (`C-c e h`).
diff --git a/graphql-mode.el b/graphql-mode.el
index 68f0364be2..9b7b846556 100644
--- a/graphql-mode.el
+++ b/graphql-mode.el
@@ -261,6 +261,7 @@ Please install it and try again."))
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-c C-c") 'graphql-send-query)
     (define-key map (kbd "C-c C-l") 'graphql-select-endpoint)
+    (define-key map (kbd "C-c e h") 'graphql-edit-headers)
     map)
   "Key binding for GraphQL mode.")
 
@@ -272,6 +273,9 @@ Please install it and try again."))
     st)
   "Syntax table for GraphQL mode.")
 
+(defvar-local graphql-edit-headers--parent-buffer nil)
+(put 'graphql-edit-headers--parent-buffer 'permanent-local t)
+
 
 (defun graphql-indent-line ()
   "Indent GraphQL schema language."
@@ -388,6 +392,63 @@ This is the function to be used for the hook 
`completion-at-point-functions'."
      (1 font-lock-variable-name-face)))
   "Font Lock keywords.")
 
+;;; Edit headers functionality:
+
+(defun graphql-edit-headers ()
+  "Edit graphql request headers interactively in a dedicated buffer.
+
+Open a buffer to edit `graphql-extra-headers'.  The contents of this
+buffer take precedence over the setting in `graphql-extra-headers'
+when sending a request."
+  (interactive)
+  (unless (equal major-mode 'graphql-mode)
+    (error "Not in graphql-mode, cannot edit headers"))
+  (let ((extra-headers-buffer-name
+         (concat "*Graphql Headers for " (buffer-name) "*"))
+        (parent-buffer (current-buffer)))
+    (pop-to-buffer extra-headers-buffer-name)
+    (setq-local graphql-edit-headers--parent-buffer parent-buffer)
+    (if (and (string-empty-p (buffer-string)) graphql-extra-headers)
+        (progn
+          (insert (json-encode graphql-extra-headers))
+          (json-pretty-print (point-min) (point-max))
+          (goto-char (point-min))))
+    (when (fboundp 'json-mode)
+      (json-mode))
+    (graphql-edit-headers-mode)))
+
+(defun graphql-edit-headers-buffer-p ()
+  "Non-nil when current buffer is a header editing buffer."
+  (bound-and-true-p graphql-edit-headers-mode))
+
+(defun graphql-edit-headers-accept ()
+  "Accept buffer contents and write to `graphql-extra-headers'."
+  (interactive)
+  (unless (graphql-edit-headers-buffer-p) (error "Not in a GraphQL headers 
buffer"))
+  (let ((new-headers (json-read-from-string (buffer-string))))
+    (with-current-buffer graphql-edit-headers--parent-buffer
+      (setq-local graphql-extra-headers new-headers)))
+  (quit-window 'kill-buffer))
+
+(defun graphql-edit-headers-abort ()
+  "Kill current headers buffer and return to graphql file."
+  (interactive)
+  (unless (graphql-edit-headers-buffer-p) (error "Not in a GraphQL headers 
buffer"))
+  (quit-window 'kill-buffer))
+
+(define-minor-mode graphql-edit-headers-mode
+  "Minor mode for editing graphql extra headers.
+\\<graphql-mode-map>
+This minor mode is turned on when you edit GraphQL headers
+interactively with `\\[graphql-edit-headers]'."
+  :lighter " GQL Hdr"
+  :keymap (let ((map (make-sparse-keymap)))
+            (define-key map (kbd "C-c C-c") 'graphql-edit-headers-accept)
+            (define-key map (kbd "C-c C-k") 'graphql-edit-headers-abort)
+            map)
+  (setq header-line-format (substitute-command-keys "Edit GraphQL query 
headers.  Save with \
+`\\[graphql-edit-headers-accept]' or abort with 
`\\[graphql-edit-headers-abort]'")))
+
 
 ;;;###autoload
 (define-derived-mode graphql-mode prog-mode "GraphQL"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]