branch: externals/graphql commit da47d83cefe763d6dc5198bee4c56a32f4334116 Author: Sean Allred <c...@seanallred.com> Commit: Sean Allred <c...@seanallred.com>
Add `graphql-{query,mutation}' macros --- graphql.el | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/graphql.el-test.el | 5 +++++ 2 files changed, 49 insertions(+) diff --git a/graphql.el b/graphql.el index 3b40ff9e53..06e88e04ba 100644 --- a/graphql.el +++ b/graphql.el @@ -126,6 +126,50 @@ parameter." (format " { %s }" (mapconcat #'graphql-encode fields " "))))))))) +(defun graphql--genform-operation (args kind) + (pcase args + (`(,graph) + `(graphql-encode '(,kind ,graph))) + + (`((,name) ,graph) + `(graphql-encode '(,kind :op-name ,name + ,graph))) + (`((,name ,parameters) ,graph) + `(graphql-encode '(,kind :op-name ,name + :op-params ,parameters + ,graph))) + + (_ (error "bad form")))) + +(defmacro graphql-query (&rest args) + "Construct a Query object. +Calling pattern: + + (fn GRAPH) := Just encode GRAPH as a Query. + + (fn (NAME) GRAPH) := Give the Query a NAME. + + (fn (NAME PARAMETERS) GRAPH) := Give the Query PARAMETERS; + see below. + +Parameters are formatted as defined by +`graphql--encode-parameter-spec'." + (graphql--genform-operation args 'query)) + +(defmacro graphql-mutation (&rest args) + "Construct a Mutation object. +Calling pattern: + + (fn GRAPH) := Just encode GRAPH as a Mutation. + + (fn (NAME) GRAPH) := Give the Mutation a NAME. + + (fn (NAME PARAMETERS) GRAPH) := Give the Mutation PARAMETERS; + see below. + +Parameters are formatted as defined by +`graphql--encode-parameter-spec'." + (graphql--genform-operation args 'query)) (provide 'graphql) ;;; graphql.el ends here diff --git a/test/graphql.el-test.el b/test/graphql.el-test.el index 457bb7f5f2..743e86d313 100644 --- a/test/graphql.el-test.el +++ b/test/graphql.el-test.el @@ -45,4 +45,9 @@ (content . HOORAY)))))) "addReaction(input:{subjectId:\"MDU6SXNzdWUxNzc2MzA3Mjk=\",content:HOORAY})"))) +(ert-deftest encode-query () + (string= (graphql-query (test ((ep Episode !) + (review ReviewInput ! . "fifty"))) + (repository)) + "query test($ep: Episode!,$review: ReviewInput! = \"fifty\") { repository }")) ;;; graphql.el-test.el ends here