Fork me on GitHub
#graphql
<
2021-06-03
>
fmnoise08:06:52

hi everyone, I'd like to clarify current state of custom directives support https://lacinia.readthedocs.io/en/latest/directives.html so code like this could work after upcoming changes

(defn- apply-directive [{:keys [directive-type directive-args] :as directive} value]
  (cond-> value
    (and value (= :mathInc directive-type)) (+ (:amount directive-args))))

(defn- directives-handler [field _]
  (let [{:keys [directives resolve]} field]
    (fn [ctx args entity]
      (let [value (when resolve (resolve ctx args entity))
            new-value (reduce (fn [value directive]
                                (apply-directive directive value))
                              value
                              directives)]
        new-value))))

(schema/compile {:enable-introspection? (boolean enable-introspection?)
                 :apply-field-directives directives-handler})

;; in schema

{:directive-defs
 {:mathInc
  {:locations #{:field-definition}
   :args {:amount {:type (non-null Long)}}}}
 ...
 :someType
  {:fields {:price {:type Money
                    :directives [{:directive-type :mathInc
                                  :directive-args {:amount 1}}]
                    :resolve :product/price}}}}