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

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

[elpa] externals/graphql 03d5cc4c3c 27/56: New function graphql-simplify


From: ELPA Syncer
Subject: [elpa] externals/graphql 03d5cc4c3c 27/56: New function graphql-simplify-response-edges
Date: Sat, 29 Oct 2022 13:57:56 -0400 (EDT)

branch: externals/graphql
commit 03d5cc4c3c834f69288f49b1b97130e73ea94d00
Author: Sean Allred <code@seanallred.com>
Commit: Sean Allred <code@seanallred.com>

    New function graphql-simplify-response-edges
    
    GraphQL APIs will return networked objects in terms of 'edges' and
    'nodes'.  This function detects those relationships in a response data
    structure and collapses them into just the value of each 'node'.
    
    For instance, if I had a data structure like this:
    
        (some_object (edges ((node (prop . "value")))
                            ((node (prop . "other")))))
    
    the simplification would look like this:
    
        (some_object ((prop . "value"))
                     ((prop . "other"))
    
    Of course, this will work recursively and descend into fields and
    subfields to simplify all edges it finds.
    
    This does have the side-effect of removing pagination utilities like
    the 'cursor' field of an edge, but that is a complex problem all its
    own and it's probably best if this information is retrieved by other
    means.
---
 graphql.el | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/graphql.el b/graphql.el
index 5c2ef15881..3fb34191bf 100644
--- a/graphql.el
+++ b/graphql.el
@@ -134,6 +134,24 @@ parameter."
           (format "{%s}"
                   (mapconcat #'graphql-encode fields " "))))))))
 
+(defun graphql-simplify-response-edges (data)
+  "Simplify DATA to collapse edges into their nodes."
+  (pcase data
+    ;; When we encounter a collection of edges, simplify those edges
+    ;; into their nodes
+    (`(,object (edges . ,edges))
+     (cons object (mapcar #'graphql-simplify-response-edges
+                          (mapcar (lambda (edge) (alist-get 'node edge))
+                                  edges))))
+    ;; When we encounter a plain cons cell (not a list), let it pass
+    (`(,key . ,(and value (guard (not (consp value)))))
+     data)
+    ;; symbols should pass unaltered
+    (`,(and symbol (guard (symbolp symbol)))
+     data)
+    ;; everything else should be mapped
+    (_ (mapcar #'graphql-simplify-response-edges data))))
+
 (defun graphql--genform-operation (args kind)
   (pcase args
     (`(,graph)



reply via email to

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