Something went wrong. Try again.
Dagger 1.0.0 beta SDK experiments
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102(ns dagger.codegen "Generate ordinary, REPL-friendly Clojure functions from Dagger introspection." (:require [clojure.data.json :as json] [clojure.edn :as edn] [clojure.java.io :as io] [clojure.pprint :as pprint] [clojure.string :as str]))
(def banner ";; Generated by dagger.codegen. Do not edit.\n")(defn kebab [s] (-> s (str/replace #"([A-Z]+)([A-Z][a-z])" "$1-$2") (str/replace #"([a-z0-9])([A-Z])" "$1-$2") (str/replace "_" "-") str/lower-case))(defn ident [s] (let [n (kebab s)] (symbol (if (special-symbol? (symbol n)) (str n "-field") n))))(defn ref-type [t] (cond-> {:kind (keyword (str/lower-case (str/replace (:kind t) "_" "-")))} (:name t) (assoc :name (:name t)) (:ofType t) (assoc :of-type (ref-type (:ofType t)))))(defn leaf [t] (if (:ofType t) (recur (:ofType t)) t))(defn expected [value types] (or (some (fn [d] (when (= "expectedType" (:name d)) (some #(when (= "name" (:name %)) (json/read-str (:value %))) (:args d)))) (:directives value)) (let [name (:name (leaf (:type value))) base (when (str/ends-with? (or name "") "ID") (subs name 0 (- (count name) 2)))] (when (#{"OBJECT" "INTERFACE"} (:kind (get types base))) base))))(defn input [a types] (cond-> {:type (ref-type (:type a)) :default (:defaultValue a)} (expected a types) (assoc :expected (expected a types))))(defn field [f types] (cond-> {:type (ref-type (:type f)) :args (into (sorted-map) (map #(vector (:name %) (input % types)) (:args f)))} (expected f types) (assoc :expected (expected f types))))(defn pretty [value] (with-out-str (pprint/pprint value)))(defn pretty-code [value] (with-out-str (pprint/write value :dispatch pprint/code-dispatch :right-margin 100)))
(defn outputs [document] (let [schema (or (get-in document [:data :__schema]) (:__schema document) document) types (into (sorted-map) (map (juxt :name identity)) (remove #(str/starts-with? (:name %) "_") (:types schema))) known? #(contains? types (:name (leaf (:type %)))) types (into (sorted-map) (map (fn [[n t]] [n (update t :fields #(filterv known? %))])) types) query-type (get-in schema [:queryType :name]) compiled {:query-type query-type :types (into (sorted-map) (for [[name t] types] [name {:kind (:kind (ref-type t)) :fields (into (sorted-map) (map #(vector (:name %) (field % types)) (:fields t))) :values (mapv :name (:enumValues t)) :input-fields (into (sorted-map) (map #(vector (:name %) (input % types)) (:inputFields t)))}]))} generated (into {} (for [[name t] types :when (#{"OBJECT" "INTERFACE"} (:kind t)) :let [module (if (= name query-type) "client" (kebab name)) funcs (mapv (comp ident :name) (:fields t)) exclusions (sort (filter #(contains? (ns-publics 'clojure.core) %) funcs))]] [(str "src/dagger/" (str/replace module "-" "_") ".clj") (str banner (pr-str (list 'ns (symbol (str "dagger." module)) (list :refer-clojure :exclude (vec exclusions)) (list :require '[dagger.internal.runtime :as runtime]))) "\n\n" (str/join "\n\n" (for [f (:fields t) :let [required (filterv #(and (= "NON_NULL" (get-in % [:type :kind])) (nil? (:defaultValue %))) (:args f)) optional (remove (set required) (:args f)) params (mapv #(symbol (str "arg-" (kebab (:name %)))) required) argmap (into (sorted-map) (map #(vector (keyword (kebab (:name %1))) %2) required params)) invocation (list 'runtime/invoke-field 'self name (:name f) 'arguments) doc (str (:description f) (when (:isDeprecated f) (str "\nDeprecated: " (:deprecationReason f))) (when (seq optional) (str "\nOptions: " (str/join ", " (map #(str ":" (kebab (:name %))) optional)))))]] (pretty-code (if (seq optional) (list 'defn (ident (:name f)) doc (list (into ['self] params) (list* (ident (:name f)) 'self (concat params [{}]))) (list (into ['self] (conj params 'options)) (list 'when-not (list 'map? 'options) (list 'throw (list 'ex-info "Options must be a map" {:type :dagger/invalid-argument}))) (list 'let ['arguments (list 'merge 'options argmap)] invocation))) (list 'defn (ident (:name f)) doc (into ['self] params) (list 'let ['arguments argmap] invocation)))))) "\n")]))] (assoc generated "resources/dagger/schema.edn" (pretty compiled))))
(defn generate! [input-path output-dir check?] (let [files (outputs (json/read-str (slurp input-path) :key-fn keyword)) targets (map (fn [[path content]] [(io/file output-dir path) content]) files) existing (filter #(and (.isFile %) (str/ends-with? (.getName %) ".clj") (str/starts-with? (slurp %) banner)) (file-seq (io/file output-dir "src"))) paths (set (map #(.getCanonicalPath (first %)) targets))] (doseq [[path content] targets] (when (and (.exists path) (str/ends-with? (.getName path) ".clj") (not (str/starts-with? (slurp path) banner))) (throw (ex-info "Schema collides with a handwritten namespace" {:path (str path)}))) (when (and check? (or (not (.exists path)) (not= content (slurp path)))) (throw (ex-info "Generated file differs" {:path (str path)})))) (doseq [path existing :when (not (paths (.getCanonicalPath path)))] (if check? (throw (ex-info "Stale generated file" {:path (str path)})) (io/delete-file path))) (when-not check? (doseq [[path content] targets] (io/make-parents path) (spit path content))) (println (if check? "Checked" "Generated") (count files) "files")))
(defn -main [& args] (let [check? (boolean (some #{"--check"} args)) args (remove #{"--check"} args) [input output] args] (generate! (or input "schema/introspection.json") (or output ".") check?)))