[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/graphql f9dc451ae6 18/56: Simplify object encoding
From: |
ELPA Syncer |
Subject: |
[elpa] externals/graphql f9dc451ae6 18/56: Simplify object encoding |
Date: |
Sat, 29 Oct 2022 13:57:53 -0400 (EDT) |
branch: externals/graphql
commit f9dc451ae6ed82bcb790dd035c6f47ca5763476c
Author: Sean Allred <code@seanallred.com>
Commit: Sean Allred <code@seanallred.com>
Simplify object encoding
---
graphql.el | 86 ++++++++++++++++++++++++-------------------------
test/graphql.el-test.el | 12 +++++--
2 files changed, 52 insertions(+), 46 deletions(-)
diff --git a/graphql.el b/graphql.el
index e3a0220906..ac3e83c6c6 100644
--- a/graphql.el
+++ b/graphql.el
@@ -29,20 +29,17 @@
(require 'pcase)
-(defun graphql--encode-atom (g)
+(defun graphql--encode-object (obj)
(cond
- ((stringp g)
- g)
- ((symbolp g)
- (symbol-name g))
- ((numberp g)
- (number-to-string g))
- ((and (consp g)
- (not (consp (cdr g))))
- (symbol-name (car g)))))
-(defun graphql--encode-list (l)
- (when (and (consp l) (consp (car l)))
- (mapconcat #'graphql--encode l " ")))
+ ((stringp obj)
+ obj)
+ ((symbolp obj)
+ (symbol-name obj))
+ ((numberp obj)
+ (number-to-string obj))
+ ((and (consp obj)
+ (not (consp (cdr obj))))
+ (symbol-name (car obj)))))
(defun graphql--encode-argument-spec (spec)
(graphql--encode-argument (car spec) (cdr spec)))
(defun graphql--encode-argument (key value)
@@ -95,40 +92,41 @@ parameter."
"")))
(defun graphql--get-keys (g)
- (let (graph keys)
- (while g
- (if (keywordp (car g))
- (let* ((param (pop g))
- (value (pop g)))
- (push (cons param value) keys))
- (push (pop g) graph)))
- (list keys (nreverse graph))))
+ (or (and (not (consp g))
+ (list nil g))
+ (let (graph keys)
+ (while g
+ (if (keywordp (car g))
+ (let* ((param (pop g))
+ (value (pop g)))
+ (push (cons param value) keys))
+ (push (pop g) graph)))
+ (list keys (nreverse graph)))))
(defun graphql-encode (g)
"Encode G as a GraphQL string."
- (or (graphql--encode-atom g)
- (graphql--encode-list g)
- (pcase (graphql--get-keys g)
- (`(,keys ,graph)
- (let ((object (car graph))
- (name (alist-get :op-name keys))
- (params (alist-get :op-params keys))
- (arguments (alist-get :arguments keys))
- (fields (cdr graph)))
- (concat
- (symbol-name object)
- (when name
- (format " %S" name))
- (when arguments
- ;; Format arguments "key:value, ..."
- (format "(%s)"
- (mapconcat #'graphql--encode-argument-spec arguments
",")))
- (when params
- (format "(%s)"
- (mapconcat #'graphql--encode-parameter-spec params ",")))
- (when fields
- (format "{%s}"
- (mapconcat #'graphql-encode fields " ")))))))))
+ (pcase (graphql--get-keys g)
+ (`(,keys ,graph)
+ (let ((object (or (car-safe graph) graph))
+ (name (alist-get :op-name keys))
+ (params (alist-get :op-params keys))
+ (arguments (alist-get :arguments keys))
+ ;; TODO `:as' keyword for http://graphql.org/learn/queries/#aliases
+ (fields (cdr-safe graph)))
+ (concat
+ (graphql--encode-object object)
+ (when name
+ (format " %S" name))
+ (when arguments
+ ;; Format arguments "key:value, ..."
+ (format "(%s)"
+ (mapconcat #'graphql--encode-argument-spec arguments ",")))
+ (when params
+ (format "(%s)"
+ (mapconcat #'graphql--encode-parameter-spec params ",")))
+ (when fields
+ (format "{%s}"
+ (mapconcat #'graphql-encode fields " "))))))))
(defun graphql--genform-operation (args kind)
(pcase args
diff --git a/test/graphql.el-test.el b/test/graphql.el-test.el
index f692c91085..a48f18ee7e 100644
--- a/test/graphql.el-test.el
+++ b/test/graphql.el-test.el
@@ -48,9 +48,17 @@
(ert-deftest encode-query ()
(should (string= (graphql-query (test)
(repository))
- "query test { repository }"))
+ "query test{repository}"))
(should (string= (graphql-query (test ((ep Episode !)
(review ReviewInput ! . 50)))
(repository :arguments ((hello . ($ ep)))))
- "query test($ep:Episode!,$review:ReviewInput!=50) {
repository(hello:$ep) }")) )
+ "query
test($ep:Episode!,$review:ReviewInput!=50){repository(hello:$ep)}")) )
+
+(ert-deftest encode-complicated ()
+ (should (string= (graphql-query (test ((ep Episode . ((complex . params)
+ (with . "values")
+ (like . 50)))
+ (review ReviewInput ! . 50)))
+ (repository :arguments ((hello . ($ ep)))))
+ "query
test($ep:Episode={complex:params,with:\"values\",like:50},$review:ReviewInput!=50){repository(hello:$ep)}")))
;;; graphql.el-test.el ends here
- [elpa] branch externals/graphql created (now 5ca5f50b5e), ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 1026c76d30 01/56: Initial commit, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 9d4179cd4a 04/56: Simplify handling of fields, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 335ec3a9c5 08/56: More appropriate function names for argument encoding, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql a8067839b6 10/56: Rename `graphql--encode-argument-pair' to `*-spec', ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 1f4f1a61c9 11/56: Remove graphql-encode and replace it with graphql--encode, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql adf95c1d92 16/56: Compress output, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql f9dc451ae6 18/56: Simplify object encoding,
ELPA Syncer <=
- [elpa] externals/graphql 0756399fc9 22/56: Test unnamed query, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql b0bd6c5fd8 02/56: Create DSL and basic encoder, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql fcc32d3d71 03/56: Simplify handling of spacing optional elements, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 3bf43e2ad1 05/56: Use real names of things, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 24d278dd43 06/56: Add support for variables in object parameters, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 4d2837ddbe 07/56: Fix format bug for :name, ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 210fed6a12 09/56: More appropriate name for `:name', ELPA Syncer, 2022/10/29
- [elpa] externals/graphql d299be77da 12/56: Tweak handling for numbers when encoding argument values., ELPA Syncer, 2022/10/29
- [elpa] externals/graphql 08fb4fcf12 13/56: Add support for operation parameters via `:op-params', ELPA Syncer, 2022/10/29
- [elpa] externals/graphql da47d83cef 14/56: Add `graphql-{query, mutation}' macros, ELPA Syncer, 2022/10/29