Before going much further it’s probably a good idea to start sketching out how our static site build system will work as a whole:
Things we’ll keep out of scope for now:
I’m not sure about the final server upload step, I’ve used a million different tools and services for hosting static sites in the past and I’m still terrible at it. Eh, we’ll deal with that later.
What’s the best way to build a blog for Elisa and I? Somehow the answer to that question last year was to use Wordpress as a CMS and Gatsby to consume it and spit out a ‘static’ website. The resulting lightsandclockwork.xyz worked for the most part but every time I had to step into javascript-land to work in it I started pulling my hair out. In the end I essentially stopped working on it entirely and felt guilty that I’d failed myself and Elisa in building us a real web presence.
But today is a new day and I’m going to make the website using Clojure like I should have done in the first place 😄
So we have a Wordpress site and I’ve installed these plugins:
/graphql
endpoint to our wordpress siteThen let’s use venia to construct a GraphQL query and clj-http-lite to send a POST request:
(ns site.fetch
(:require
[clj-http.lite.client :as client]
[venia.core :as v]))
(def url-q
(v/graphql-query {:venia/queries [[:generalSettings
[:url]]]}))
(client/post "https://<<OURWPSITE>>/graphql"
{:content-type :json
:accept :json
:body (str "{\"query\": \"" url-q "\"}")})
And we got back a 200 response with the body "{\"data\":{\"generalSettings\":{\"url\":\"https:\\/\\/<<OURWPSITE>>\"}}}"
, success!
Actually, venia doesn’t seem to be helping. We don’t need dynamic GraphQL queries (yet?) so we can just play around in our GraphiQL IDE, copy the query into CyberChef to strip the whitespace, and paste the string into our code for now.
(ns site.fetch
(:require
[clj-http.lite.client :as http]
[clojure.data.json :as json]))
(def all-posts-query "{posts{edges{node{id,modified,modifiedGmt,title,status,slug,uri,date}}}}")
(def posts-response
(http/post "https://<<OURWPSITE>>/graphql"
{:content-type :json
:accept :json
:body (str "{\"query\": \"" all-posts-query "\"}")}))
(def post-responses
(-> posts-response
:body
(json/read-str :key-fn keyword)
:data
:posts
:edges))
(defn process-post-response
[{:keys [node]}]
{:post/id (:id node)
:published-date (:date node)
:last-modified-date-gmt (:modifiedGmt node)
:uri (:uri node)
:slug (:slug node)
:title (:title node)})
#_(doseq [post (map process-post-response post-responses)]
(spit (str "data/posts/" (:id post))
post))
It works! We’ve got a folder full of post metadata from Wordpress! Next step, pulling down post content if its been modified since our last pull.