Email list hosting service & mailing list manager

Basic GraphQL client in 50 lines of Chicken Scheme Lassi Kortela (24 Jul 2019 14:56 UTC)
(missing)
Re: Basic GraphQL client in 50 lines of Chicken Scheme Lassi Kortela (24 Jul 2019 16:11 UTC)

Basic GraphQL client in 50 lines of Chicken Scheme Lassi Kortela 24 Jul 2019 14:55 UTC

(import (srfi 1) (matchable))
(import (json))
(import (openssl) (uri-common) (intarweb) (http-client))

(define (graphql-symbol->string symbol)
   (string-map (lambda (ch) (if (char=? ch #\-) #\_ ch))
               (symbol->string symbol)))

(define (graphql->string-inner document indent)
   (let ((ind (make-string (* 2 indent) #\space)))
     (match document
       ((query name . body)
        (string-append
         ind (graphql-symbol->string name) " {\n"
         (fold (lambda (body-part so-far)
                 (string-append
                  so-far
                  (graphql->string-inner body-part (+ indent 1))))
               "" body)
         ind "}\n"))
       (name
        (string-append ind (graphql-symbol->string name) "\n")))))

(define (graphql->string document)
   (match document
     ((query name . body)
      (string-append "{\n" (graphql->string-inner document 1) "}\n"))))

(define (graphql-query endpoint document)
   (let* ((doc (graphql->string document))
          (req (make-request
                method: 'POST
                uri: (uri-reference endpoint)
                headers: (headers '((content-type application/graphql))))))
     (with-input-from-request req doc json-read)))

;;======================================================================

(define endpoint "https://api.staging.scheme.fi/graphql")

(define document '(query reports
                          id
                          full-title
                          (query documents
                                 id
                                 original-pdf-url
                                 errata-corrected-pdf-url)))

;;(display (graphql->string document))
(write (graphql-query endpoint document))