anish
anish

Reputation: 1033

Clojure: How to add scalar type in lacinia Graphql schema in edn format?

I want to define scalar type such as Token

if I define Token in following format it fails to compile

:scalars
  {:Token }

According to https://lacinia.readthedocs.io/en/latest/custom-scalars.html , I need to give parse and seririase functions

So I have modified code below,

 :scalars
   {:Token 
     {:parse #(str %)
      :serialize #(str %)
     }
   }

Now I am getting error as " No dispatch macro for: ("

How I can define scalar type token as "scalar Token"?

Upvotes: 1

Views: 315

Answers (3)

Jing Wang
Jing Wang

Reputation: 31

As per this PR :parse and :serialize are no longer clojure.spec conformers. They are simple functions now (version 0.37.0-alpha-1)

https://github.com/walmartlabs/lacinia/pull/246/files/204030750a8f5c77a460147d8a376a4039d4ff0e#diff-8b1c3fd0d4a6765c16dfd18509182f9dR22

Upvotes: 1

anish
anish

Reputation: 1033

Thanks @madstap for answer,in the comment code is not properly highlighted.

In transforms I found that it needs to return spec, following code worked

(def token-parser
  (s/conformer
   (fn [^String v]
     (str v))))

(def token-serializer
  (s/conformer 
   (fn [^String v]
     (str v))))

(util/attach-scalar-transformers
        {:token-parser token-parser
         :token-serializer token-serializer
         })

Upvotes: 1

madstap
madstap

Reputation: 1552

Like nha says in the comments, you can't have function literals in edn files. If you move this to a .clj file it'll work.

If you do want to keep the schema as an .edn file though, there's the function com.walmartlabs.lacinia.util/attach-scalar-transformers for that.

schema.edn:

{,,,
 :scalars
 {:Token {:parse :token-parser
          :serialize :token-serializer}}}

schema.clj

(ns foo.schema
  (:require
   [??? :refer [slurp-edn]]
   [com.walmartlabs.lacinia.schema :as schema]
   [com.walmartlabs.lacinia.util :as util]))

(def transforms
  {:token-parser #(,,,)
   :token-serializer #(,,,)})

(defn schema []
  (-> (slurp-edn "schema.edn")
      (util/attach-scalar-transformers transforms)
      ;; ... attach resolvers, compile schema, etc
      ))

Upvotes: 1

Related Questions