Fork me on GitHub
#graphql
<
2017-12-11
>
andrewtropin08:12:32

Hi, guys. Can someone provide an example how to use spec for custom scalars?

andrewtropin09:12:37

I need to write {:parse-scalar1 #(s/conform ::my-spec %)} or exist some more convenient way in lacinia?

guy09:12:34

hopefully that is helpful

andrewtropin09:12:18

Yep, I already read it, but it looks strange to write something like (schema/as-conformer #(s/conform ::my-spec %))

guy10:12:48

ah sorry

guy10:12:26

I thought you just supply your own function?

guy10:12:49

well could you do it in your conform function?

guy10:12:53

it depends on what u want to check

guy10:12:56

(schema/as-conformer #(.parse date-formatter %))

guy10:12:02

thats a custom scalar one right

guy10:12:25

so then you could just assert that it passes your spec maybe, then do the transformation logic after?

guy10:12:29

if that makes sense?

andrewtropin14:12:42

Look. I already have a spec. And I can easily use it for conforming #(s/conform ::my-spec %) It also returns invalid-blabla in case it can't parse %. If lacinia requires conformers maybe exist some simple way to use a spec instead of conformer, because it looks so stupid to do (schema/as-conformer #(s/conform ::my-spec %))

hlship18:12:18

(defn as-conformer
  "Creates a clojure.spec/conformer as a wrapper around the supplied function.

  The function is only invoked if the value to be conformed is non-nil.

  Any exception thrown by the function is silently caught and the returned conformer
  will return :clojure.spec/invalid or a [[coercion-failure]]."
  [f]
  (s/conformer
    (fn [x]
      (try
        (when (some? x)
          (f x))
        (catch Exception e
          (if-some [message (.getMessage e)]
            (coercion-failure message (ex-data e))
            ::s/invalid))))))
I could imagine a wrap-conformer that could accomplish the same work, but invoke (s/conform spec x) instead of (f x).

andrewtropin23:12:21

Ok, thanks for information, will write it on my own.